Spring Cloud負載均衡:使用zuul做服務器端負載均衡

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列表,便可實現任意的負載均衡(再也不貼效果圖),優缺點和上面相反。

相關文章
相關標籤/搜索