使用Consul服务发现的自动代理服务

15

我正尝试从Eureka迁移到Consul进行服务发现,但遇到问题——我的网关服务和客户服务都已注册,但网关服务无法自动路由到客户服务。我在网关控制器中明确定义的使用Feign客户端路由的路由正常工作,但是以前(使用Eureka)我可以请求任何路径,例如“/customer-service/blah”(其中customer-service是已注册的名称),网关将只是将请求转发到下游微服务。

这是我的网关bootstrap.yml(它位于bootstrap而不是application中,因为我还在使用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作为网关服务使用,它正常工作。 - wthamira
1
Consul可以像Eureka一样使用。 - wthamira
结果发现我已经从类路径中删除了Zuul。 - Gandalf
1个回答

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
我将用这个依赖项来作为网关和所有应用程序的通用依赖。
<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配置enter image description here


1
所以我接受了这个因为它把我推向了正确的方向。真正的问题是,当我移除了Eureka和Cloud-Config时,我无意中也移除了Zuul。<DOH!>将Zuul作为依赖项添加回去,BAM!我确实使用了您的前缀配置,但不知道该设置。 - Gandalf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接