1. 程式人生 > >springcloud中zuul與consul整合進行負載均衡

springcloud中zuul與consul整合進行負載均衡

第一步:向consul註冊服務

配置檔案application.yml為

server:
  port: 8080
spring:
  application:
    name: zuulserver
  cloud:
    consul:
      discovery:
        instance-id: ${spring.application.name}:${server.port}
        prefer-ip-address: true
        health-check-interval: 10s
        hostname: ${spring.application.name}
        service-name: ${spring.application.name}
        enabled: true
      host: 192.168.112.133
      port: 8500

註冊多個服務來測試,這幾個service的service-name要一樣。

第二部:新增zuulproxy

配置檔案application.yml為

server:
  port: 8091

spring:
  application:
    name: zuulproxy
  cloud:
    consul:
      discovery:
        instance-id: ${spring.application.name}:${server.port}
        prefer-ip-address: true
        health-check-interval: 10s
        hostname: ${spring.application.name}
        service-name: ${spring.application.name}
        enabled: true
        health-check-path: /health
      host: 192.168.112.133
      port: 8500

zuul:
  routes:
    user:
      path: /user/**
      serviceId: zuulserver

第一步在consul註冊了service-name為zuulserver的多個服務。第二步,zuul的配置使用名為zuulserver的serviceId取得服務。之後訪問已http://localhost:8091/user開頭的連結,會把請求分發到zuulserver去。