相同的服务我们发布多个服务,服务名字(spring.application.name)相同,服务id(对应于Consul注册服务的spring.cloud.consul.discovery.instance-id)不同,不同的服务id对应的服务器或者ip不同。我们通过zuul访问的时候,会轮询的方式路由到不同的服务id服务上了,即会在10.9.100.100:8801,10.9.100.100:8805,10.9.100.100:8806三个服务上轮询执行。
serviceservice-id(instance-id)ip:porturi通过路由的访问方式test-configtest-config-880110.9.100.100:8801/api/getCountNum
/api/hand/sites
/health
localhost:8804/api/getCountNum
localhost:8804/api/hand/sites
localhost:8804/health
test-configtest-config-880510.9.100.100:8805/api/getCountNum
/api/hand/sites
/health
localhost:8804/api/getCountNum
localhost:8804/api/hand/sites
localhost:8804/health
test-configtest-config-880610.9.100.100:8806/api/getCountNum
/api/hand/sites
/health
localhost:8804/api/getCountNum
localhost:8804/api/hand/sites
localhost:8804/health
路由服务
zuul-gateway-service
(在这里是
test-config-service02)
zuul-gateway-servicelocalhost:8804/healthconsul上的服务(只展示两个服务):
请求方式:
${ZuulServerIP}:${server.port}:/[新服务的请求uri] 例如;不加zuul网关访问方式为:10.9.100.100:8080/api/getNum 加zuul后,zuul服务器为10.9.100.101: 10.9.100.101:8081/api/getNumzuul的服务:
SpringBoot启动类ZuulGatewayTestApplication.java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication @EnableZuulProxy public class ZuulGatewayTestApplication { public static void main(String[] args) { SpringApplication.run(ZuulGatewayTestApplication.class, args); } }Consul健康检查类HealthController.java
@RestController public class HealthController { @RequestMapping("/health") public String health(){ return "health"; } }Resources:
application.yml
spring: profiles: active: testapplication-test.yml
#*********************************************************************************************************** # 一下配置为zuul轮询方式 #*********************************************************************************************************** server: port: 8804 spring: cloud: consul: host: hdp04 # consul 服务器主机名 port: 8500 # consul 服务器端口 discovery: # healthCheckPath: ${management.contextPath}/health # actuator 健康检查的 url 路径 healthCheckPath: /health # 自定义健康检查的 url(适合于不适用 actuator 的场景) healthCheckInterval: 15s # 健康检查的频率, 默认 15 秒 # 直接指定服务的 consul service id(即 instance id). # 默认情况下为 spring.application.name + server.port, 如果在多个服务器上同一个服务, 因为应用名和端口都一致, #会导致service id 会重复, 所以一般情况都需要引入一个随机数避免重复 . # ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port} 应用名称+服务器IP+端口 instance-id: test-config-service02 #${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}} # prefer-ip-address: true # 在注册时使用 consul IP, 而不是 hostname # ip-address: hdp04 # 使用 consul 服务器 IP, 而不是 hostname, 需要搭配 prefer-ip-address 属性 # acl-token: 4efb1523-76a3-f476-e6d8-452220593089 #设定 consul acl token 值 enabled: true #是否启用服务发现 # 维护 tags #$ 下面示例的 tag map 是: foo->bar 和 baz->baz tags: version=1.0,author=xx enabled: true # ribbon: # enabled: false application: name: test-config-service02 #zuul属性配置 zuul.routes路由转发配置 #Zuul来代理请求转发到内部的服务上去,统一为外部提供服务 动态的路由转发功能 zuul: routes: #prefix: /zuul order: serviceId: test-config path: /** ignored-services: test-config # 添加ribbon的超时时间设置 #ribbon: # ReadTimeout: 1000 # ConnectTimeout: 3000Consul配置注册中心:
bootstrap.yml
spring: cloud: consul: host: hdp04 #host: 00.0.100.200 port: 8500 #enabled将此值设置为“false”禁用Consul配置 config: enabled: true #默认是true -- format: YAML # 表示consul上面文件的格式 有四种 YAML PROPERTIES KEY-VALUE FILES #data-key: configuration #表示consul上面的KEY值(或者说文件的名字) 默认是data data-key: data #表示consul上面的KEY值(或者说文件的名字) 默认是data #prefix设置配置值的基本文件夹 prefix: spring-cloud #defaultContext设置所有应用程序使用的文件夹名称 #profileSeparator设置用于使用配置文件在属性源中分隔配置文件名称的分隔符的值
pom.xml同Consul + zuul:api网关,提供路由转发功能之服务路由的默认配置
测试:
localhost:8804/api/getCountNum
localhost:8804/api/hand/sites
localhost:8804/health
展示的访问情况即将会在
10.9.100.100:8801,10.9.100.100:8805,10.9.100.100:8806上交替执行。