Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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}}
like image 853
Gandalf Avatar asked Mar 23 '17 17:03

Gandalf


People also ask

What is Consul service discovery?

One of the major use cases for Consul is service discovery. Consul provides a DNS interface that downstream services can use to find the IP addresses of their upstream dependencies. Consul knows where these services are located because each service registers with its local Consul client.

Does Consul use envoy?

Consul Connect has first class support for using Envoy as a proxy. Consul configures Envoy by optionally exposing a gRPC service on the local agent that serves Envoy's xDS configuration API.

Which is better Consul or Eureka?

Consul and Eureka can be categorized as "Open Source Service Discovery" tools. "Great service discovery infrastructure" is the primary reason why developers consider Consul over the competitors, whereas "Easy setup and integration with spring-cloud " was stated as the key factor in picking Eureka.


1 Answers

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

like image 110
wthamira Avatar answered Sep 20 '22 20:09

wthamira