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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM