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去。

你可能感兴趣的:(分布式)