Consul + zuul:api网关,提供路由转发功能之服务路由的路径模式即指定服务的某几个接口在某服务下执行

it2025-12-15  12

实现功能:

相同的服务我们发布多个服务,服务名字(spring.application.name)相同,服务id(对应于Consul注册服务的spring.cloud.consul.discovery.instance-id)不同,不同的服务id对应的服务器或者ip不同。我们通过zuul访问的时候,会通过访问的路径自动路由到某台服务器上的服务去执行。这种模式将失去Consul发现服务的功能,新增服务得修改zuul-gateway-service的路由配置。

如下图的业务模式

 

serviceservice-id(instance-id)ip:porturi通过路由的访问方式test-configtest-config-880110.9.100.100:8801

/api/aaa/getCountNum /api/bbb/sites /api/ccc/health

localhost:8804/aaa/getCountNum

test-configtest-config-880510.9.100.100:8805

/api/aaa/getCountNum /api/bbb/sites /api/ccc/health

localhost:8804/bbb/sites

test-configtest-config-880610.9.100.100:8806

/api/aaa/getCountNum /api/bbb/sites /api/ccc/health

localhost:8804/ccc/health

路由服务

zuul-gateway-service

(在这里是

test-config-service02)

zuul-gateway-servicelocalhost:8804/health 

 

consul上的服务(只展示两个服务):

请求方式:

${ZuulServerIP}:${server.port}:/[新服务的请求uri] 例如;不加zuul网关访问方式为:10.9.100.100:8080/api/getNum 加zuul后,zuul服务器为10.9.100.101: 10.9.100.101:8081/api/getNum

zuul的服务:

 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: test

application-test.yml

#*********************************************************************************************************** # 一下配置为zuul 按模块路径路由模式 #*********************************************************************************************************** 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=xxx enabled: true # ribbon: # enabled: false application: name: test-config-service02 server: port: 8804 zuul: routes: service1: #自定义 #静态路径配置 所有的以/aaa 开始的请求,全部转发到test-config服务的url 8801端口服务去执行 path: /aaa/** #dns为自定义前缀 匹配规则三种:/? /* /** #service-id: test-config url: http:// 10.9.100.100:8801/api/aaa #注意不要写成  10.9.100.100:8801/api/aaa 否则报错Load balancer does not have available server for client:  10.9.100.100:8801/api/aaa service2: path: /bbb/** #service-id: test-config url: http:// 10.9.100.100:8805/api/bbb service3: path: /ccc/** #service-id: test-config url: http://10.9.100.100:8801/api/ccc

 

Consul配置注册中心:

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/aaa/getCountNum

localhost:8804/bbb/sites

localhost:8804/ccc/health

展示的访问情况即将会分别在

 10.9.100.100:8801,10.9.100.100:8805,10.9.100.100:8806上执行。

最新回复(0)