简体   繁体   English

Consul Service Discovery的自动代理服务

[英]Auto-Proxy services with Consul Service Discovery

I am attempting to move from Eureka to Consul for service discovery and am having an issue - my gateway service registers and my customer-service registers, but the gateway service will not route requests to the customer-service automatically. 我正试图从Eureka转到Consul进行服务发现并遇到问题 - 我的网关服务注册和我的客户服务注册,但网关服务不会自动将请求路由到客户服务。 Routes I have specifically defined in the gateway Controller that use Feign clients to route work fine, but before (with Eureka) I could make a request to any path like "/customer-service/blah" (where customer-service is the registered name) and the gateway would just forward the request on to the downstream microservice. 我在网关控制器中专门定义的路由,使用Feign客户端来正常工作,但之前(使用Eureka)我可以向任何路径发出请求,例如“/ customer-service / blah”(其中customer-service是注册名称)并且网关只是将请求转发到下游微服务。

Here is my gateway bootstrap.yml (it's in bootstrap and not application because I am also using consul for config) 这是我的网关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}}

Try this I think this help you to solve your problem.. 试试这个我觉得这有助于你解决问题..

This is my gateway bootstrap.yml file 这是我的网关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

I use this dependency for gateway and for all applications 我将此依赖关系用于网关和所有应用程序

<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 use as my configuration server. consul用作我的配置服务器。 then I add consul to this configurations. 然后我添加consul这个配置。 configuration path is /config/dev/gateway.yml 配置路径是/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

Gateway service spring boot application annotate like below 网关服务spring boot应用程序注释如下

@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class GatewayServiceApplication {

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

}// End GatewayServiceApplication

if you make your application like this you can use routs your prefer way. 如果你像这样制作你的应用程序,你可以使用你更喜欢的路径。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM