1.目的:web
本文简述Spring Cloud负载均衡之服务器负载均衡模式,使用组件为zuul。spring
zuul做为Spring Cloud中的网关组件,负责路由转发、身份验证、请求过滤等等功能,那么咱们能够利用其路由转发功能,实现负载均衡中的服务器负载均衡这一模式。bash
2.所需组件:服务器
client组件(client1,client2,client4代码相同,只是运行端口不一样):app
client1,端口8001,appname:AUTH-SERVICE负载均衡
client2,端口8002,appname:AUTH-SERVICEsocket
client4,端口8004,appname:AUTH-SERVICE1(用于验证相同路由路径,不一样serviceId状况) spring-boot
client组件均为spring boot组件,注册到eureka上,结果以下图所示:url
zuul组件正确的配置文件以下:spa
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
3.详细配置
zuul使用eureka负载均衡
server: port: 8003 eureka: instance: prefer-ip-address: true hostname: localhost client: service-url: defaultZone: http://admin:123456@localhost:8888/eureka/ spring: application: name: GATE-WAY path: zuul: host: connect-timeout-millis: 5000 socket-timeout-millis: 20000 routes: AUTH-SERVICE: path: /index/** serviceId: AUTH-SERVICE stripPrefix: false #AUTH-SERVICE: # ribbon: # listOfServers: http://localhost:8001,http://localhost:8002
启动类的配置以下:
@EnableDiscoveryClient @EnableZuulProxy @SpringBootApplication public class ZuullApplication { public static void main(String[] args) { SpringApplication.run(ZuullApplication.class, args); } }
配置完成后,访问http://localhost:8003/index/hello,便可见以下结果:
此时,若是将配置信息修改成:
server: port: 8003 eureka: instance: prefer-ip-address: true hostname: localhost client: service-url: defaultZone: http://admin:123456@localhost:8888/eureka/ spring: application: name: GATE-WAY path: zuul: host: connect-timeout-millis: 5000 socket-timeout-millis: 20000 routes: AUTH-SERVICE11: path: /index/** serviceId: AUTH-SERVICE stripPrefix: false
即将routes的名称修改一下,所得结果与上面一致,说明能够任意命名路由名称。
若是去掉:stripPrefix: false配置,则须要在路径中添加对应的path前缀,便可实现与上面同样的调用结果。
再将配置信息修改以下:
server:
port: 8003
eureka:
instance:
prefer-ip-address: true
hostname: localhost
client:
service-url:
defaultZone: http://admin:123456@localhost:8888/eureka/
spring:
application:
name: GATE-WAY
path:
zuul:
host:
connect-timeout-millis: 5000
socket-timeout-millis: 20000
routes:
AUTH-SERVICE:
path: /index/**
serviceId: AUTH-SERVICE
stripPrefix: false
AUTH-SERVICE1:
path: /index/**
serviceId: AUTH-SERVICE1
stripPrefix: false
#AUTH-SERVICE:
# ribbon:
# listOfServers: http://localhost:8001,http://localhost:8002
#ribbon:
# eureka:
# enabled: false
即再添加serviceId为AUTH-SERVICE1路由规则,则结果以下:
说明在相同的路由规则下,若是同一个路径有多个serviceId,则前面的serviceId配置会被覆盖,最后一个serviceId起做用。
zuul独立负载均衡
让zuul本身管理负载均衡,则须要添加:ribbon.eureka.enabled: false配置,详细
配置代码以下:
server: port: 8003 eureka: instance: prefer-ip-address: true hostname: localhost client: service-url: defaultZone: http://admin:123456@localhost:8888/eureka/ spring: application: name: GATE-WAY path: zuul: host: connect-timeout-millis: 5000 socket-timeout-millis: 20000 routes: AUTH-SERVICE: path: /index/** serviceId: AUTH-SERVICE111 stripPrefix: false AUTH-SERVICE111: ribbon: listOfServers: http://localhost:8001,http://localhost:8002,http://localhost:8004 ribbon: eureka: enabled: false
总结:
使用zuul组件进行负载均衡,分2种状况,其一是使用eureka组件自己维护的配置,核心在于以serviceId为一组,按默认策略(轮询)进行负载均衡,此时只须要配置路径便可,优势是配置简单省事,缺点是灵活性不强,不方便自定义调用的服务提供者列表。
若是让zuul本身管理负载均衡,则要灵活得多,主要体如今:serviceId能够任意命名,不用考虑是否在eureka中存在,另外,本身维护listOfServers列表,便可实现任意的负载均衡(再也不贴效果图),优缺点和上面相反。