在学习zuul的过程中,打算搭建一套网关服务,服务中心使用eureka,网关架构使用zuul,操作流程如下:
1.在使用euerka的过程中,需要使用eureka-server和eureka-client
搭建eureka-server 服务
(1)先创建一个module 项目,可以基于spring-boot 创建
(2)项目基于maven或gradle 进行管理
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
(3)项目启动类中添加注解
(4) 配置文件
server: port: 8761
服务启动成功
2. 搭建zuul 服务
(1) 创建module项目,可以基于springboot创建
(2) 基于maven管理,引入需要的jar包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
(3) 启动类增加相关注解
(4) 配置文件
spring:
application:
name: gateway
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
server:
port: 8089
zuul:
# 隐藏所有微服务名称(即使用微服务名称无法访问到服务)
#ignored-services: "*"
# 服务前缀名,想要访问项目之前要加上此路径
#prefix: /fengyuntec
routes:
# 想要被替换的服务名称(con. 自己取得名字)
#con.serviceId: eureka-consumer
# 替换后访问的名称
#con.path: /con/*
api-a:
path: /api-a/**
url: http://localhost:8080/
api-b:
path: /api-b/**
service-id: customer-center
zuuls:
filter:
root: filter
interval: 5
3. 创建消费者服务 customer-center
(1) 创建module项目,可以基于springboot创建
(2) 基于maven进行项目管理
<!-- 集成 hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
(3) 启动脚本增加相关的注解
(4) 增加相关配置
spring:
application:
name: customer-center
profiles:
active: local
main:
allow-bean-definition-overriding: true
cloud:
compatibility-verifier: false
logging:
level:
com.xlpay.process: debug
file:
path: /home/ywuser/log
# eureka server register
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
注意事项:
eureka的配置不可以乱加,特别是fetch-registry和register-with-eureka,代表的是服务是否可以被发现和是否可以
eureka:
client:
#该客户端是否可用
enabled: true
#实例是否在eureka服务器上注册自己的信息以供其他服务发现,默认为true
register-with-eureka: false
#此客户端是否获取eureka服务器注册表上的注册信息,默认为true
fetch-registry: false
#是否过滤掉,非UP的实例。默认为true
filter-only-up-instances: true
#与Eureka注册服务中心的通信zone和url地址
service-url:
defaultZone: http://e u r e k a . i n s t a n c e . h o s t n a m e : {eureka.instance.hostname}:eureka.instance.hostname:{server.port}/eureka/
#client连接Eureka服务端后的空闲等待时间,默认为30 秒
eureka-connection-idle-timeout-seconds: 30
#client连接eureka服务端的连接超时时间,默认为5秒
eureka-server-connect-timeout-seconds: 5
#client对服务端的读超时时长
eureka-server-read-timeout-seconds: 8
#client连接all eureka服务端的总连接数,默认200
eureka-server-total-connections: 200
#client连接eureka服务端的单机连接数量,默认50
eureka-server-total-connections-per-host: 50
#执行程序指数回退刷新的相关属性,是重试延迟的最大倍数值,默认为10
cache-refresh-executor-exponential-back-off-bound: 10
#执行程序缓存刷新线程池的大小,默认为5
cache-refresh-executor-thread-pool-size: 2
#心跳执行程序回退相关的属性,是重试延迟的最大倍数值,默认为10
heartbeat-executor-exponential-back-off-bound: 10
#心跳执行程序线程池的大小,默认为5
heartbeat-executor-thread-pool-size: 5