Consul Service Discovery的自动代理服务

Gan*_*alf 15 java service-discovery spring-boot consul netflix-zuul

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

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

    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}}
Run Code Online (Sandbox Code Playgroud)

wth*_*ira 15

试试这个我觉得这有助于你解决问题..

这是我的网关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
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

consul用作我的配置服务器.然后我添加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
Run Code Online (Sandbox Code Playgroud)

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

@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class GatewayServiceApplication {

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

}// End GatewayServiceApplication
Run Code Online (Sandbox Code Playgroud)

如果你像这样制作你的应用程序,你可以使用你更喜欢的路径.

样本领事配置 在此输入图像描述