Search code examples
javaspring-bootconsulnetflix-zuulservice-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. 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.

Here is my gateway bootstrap.yml (it's in bootstrap and not application because I am also using 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}}

Solution

  • Try this I think this help you to solve your problem..

    This is my gateway bootstrap.yml file

    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. then I add consul to this configurations. configuration path is /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

    @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 enter image description here