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}}
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With