Spring Cloud负载均衡:使用zuul作服务器端负载均衡

1.目的:

本文简述Spring Cloud负载均衡之服务器负载均衡模式,使用组件为zuul。

zuul作为Spring Cloud中的网关组件,负责路由转发、身份验证、请求过滤等等功能,那么我们可以利用其路由转发功能,实现负载均衡中的服务器负载均衡这一模式。

2.所需组件:

client组件(client1,client2,client4代码相同,只是运行端口不同):

client1,端口8001,appname:AUTH-SERVICE

client2,端口8002,appname:AUTH-SERVICE

client4,端口8004,appname:AUTH-SERVICE1(用于验证相同路由路径,不同serviceId情况) 

client组件均为spring boot组件,注册到eureka上,结果如下图所示:

zuul组件正确的配置文件如下:

 <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列表,即可实现任意的负载均衡(不再贴效果图),优缺点和上面相反。

 

posted @ 2019-08-29 20:24  Shapley  阅读(6583)  评论(0编辑  收藏  举报