背景:在分布式架构中,有时候一个服务的并发量达到上限,怎么办呢?以前传统的更多的是增加服务器用Nginx转发来实现,因此呢spring出了一个组件叫做gateway,它和zuul是大同小异,这里就不介绍区别了,接下来我们进入主题。
一.老规矩新建一个module是maven格式的。
引入jar
jar版本
<properties> <lcn.version>5.0.2.RELEASE</lcn.version> <config.version>2.1.1.RELEASE</config.version> <jdbc.version>5.2.2.RELEASE</jdbc.version> <mysql.version>5.1.6</mysql.version> <eureka.version>2.1.1.RELEASE</eureka.version> <gateway.version>2.1.5.RELEASE</gateway.version> <hystrix.version>2.1.5.RELEASE</hystrix.version> <redis.version>2.1.9.RELEASE</redis.version> </properties>
<dependencies> <!-- config start --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> <version>${config.version}</version> </dependency> <!--config end--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>${eureka.version}</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> <version>${gateway.version}</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> <version>${hystrix.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis-reactive</artifactId> <version>${redis.version}</version> </dependency>
<!--这里是为了剔除tomcat包,不然会报启动错误--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
二.配置文件如上一篇文章所写
spring: application: name: serviceConfig profiles: active: git cloud: config: label: master #这里我的gateway配置文件命名为gateway-master profile: gateway-master uri: http://localhost:9527/serviceConfig discovery: enabled: true service-id: fool-cloud-config eureka: instance: prefer-ip-address: true client: serviceUrl: defaultZone: http://admin:[email protected]:9526/eureka/eureka enabled: true 三.远程文件放到前面文章所写的github地址里面内容如下
server:
#端口号
port: 8105
##数据库连接信息
spring:
# profiles:
# active: dev
cloud:
gateway:
discovery:
locator:
enabled: true
# 服务名小写
lower-case-service-id: true
routes:
# =====================================
- id: SERVICEBASIC
# lb代表从注册中心获取服务,且已负载均衡方式转发
#遇到下面断言转发到什么服务
uri: lb://FOOL-CLOUD-EXAMPLE
predicates:
#断言转发碰到什么路径开始转发
- Path=/example/**
filters:
- StripPrefix=0
#====================================
application:
name: inter-hsp-gateway
redis:
host: 10.10.10.242
#redis端口号
port: 6379
#redis 密码
password:
eureka:
instance:
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://admin:[email protected]:9526/eureka/eureka/
enabled: true
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
logging:
level:
org:
springframework:
cloud:
gateway: debug
四.新建启动类只需要两个注解
@SpringBootApplication @EnableDiscoveryClient
五.启动主类,见到gateway的配置文件端口即成功。下一篇文章我们将讲解gateway的转发知识点
更多技术交流请加入QQ群:260292706
Owen