使用 Consul Service Discovery 的自动代理服务

Auto-Proxy services with Consul Service Discovery

我正在尝试从 Eureka 转移到 Consul 以进行服务发现,但我遇到了一个问题 - 我的网关服务注册和我的客户服务注册,但网关服务不会自动将请求路由到客户服务。我在使用 Feign 客户端路由的网关控制器中专门定义的路由工作正常,但之前(使用 Eureka)我可以向任何路径发出请求,如“/customer-service/blah”(其中客户服务是注册名称)网关只会将请求转发到下游微服务。

这是我的网关 bootstrap.yml(它在 bootstrap 而不是应用程序,因为我也在使用 consul 进行配置)

    spring:
  application:
    name: gateway-api
  cloud:
    consul:
      config:
        watch:
          wait-time: 30
      discovery:
        prefer-ip-address: true
        instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}}

试试这个,我认为这可以帮助您解决问题..

这是我的网关bootstrap.yml文件

spring:
  application:
    name: gateway-service
---

spring:
  profiles: default
  cloud:
    consul:
      config:
        prefix: config/dev/
        format: FILES
      host: localhost
      port: 8500
      discovery:
        prefer-ip-address: true
spring.profiles.active: dev

我将此依赖项用于网关和所有应用程序

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>

consul 用作我的配置服务器。然后我将领事添加到此配置中。配置路径为 /config/dev/gateway.yml

zuul:
  prefix: /api
  ignoredServices: '*'
  host:
    connect-timeout-millis: 20000
    socket-timeout-millis: 20000

  routes:
    customer-service:
        path: /customer/**
        serviceId: customer-service
        stripPrefix: false
        sensitiveHeaders: Cookie,Set-Cookie

网关服务spring启动应用程序注释如下

@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class GatewayServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayServiceApplication.class, args);
    } // End main ()

}// End GatewayServiceApplication

如果你像这样制作你的应用程序,你可以使用你喜欢的方式。

sample consul configuration