【问题标题】:Auto-Proxy services with Consul Service Discovery使用 Consul 服务发现的自动代理服务
【发布时间】:2017-08-16 10:14:07
【问题描述】:

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

这是我的网关 bootstrap.yml(它在引导程序中而不是应用程序中,因为我也在使用 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}}

【问题讨论】:

  • 能否在 consul 中给出示例 application.yml 或应用程序配置?我使用 consul 和 zuul 进行路由。 zuul 用作网关服务。它工作正常。
  • consul 可以和 eureka 一样使用
  • 原来我已经从类路径中删除了 Zuul。

标签: java spring-boot consul netflix-zuul service-discovery


【解决方案1】:

试试这个我认为这可以帮助你解决你的问题..

这是我的网关 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 boot应用注释如下

@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class GatewayServiceApplication {

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

}// End GatewayServiceApplication

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

示例 consul 配置

【讨论】:

  • 所以我接受了这个,因为它把我推向了正确的方向。真正的问题是,当我删除 Eureka 和 Cloud-Config 时,我也无意中删除了 Zuul。 添加 Zuul 作为依赖项和 BAM!我确实使用了您的前缀配置,但不知道该设置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-07
  • 2017-12-10
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多