spring-cloud with Feign/Ribbon/Eureka - unable to discover service instances - spring-cloud

My feign client currently fails to discover service instances using Eureka. My configuration includes the following annotations:
#Configuration
#ComponentScan
#EnableAutoConfiguration
#EnableDiscoveryClient
#FeignClientScan
I included the following dependencies (spring-cloud 1.0.0-BUILD-SNAPSHOT):
org.springframework.cloud:spring-cloud-starter-eureka
com.netflix.feign:feign-core
com.netflix.feign:feign-ribbon
As far as I can tell, the feign client makes use of a ConfigurationBasedServerList and therefore requires the addresses of the available service instances to be explicitly specified in the configuration file (ribbon.serverList property).
However, everything is working fine if I explicitly specify:
ribbon.NIWSServerListClassName: com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList
My test application also makes use of a Spring RestTemplate based client automatically configured to make use of an Eureka-enabled Ribbon implementation. Strange thing is that the Feign client is working fine (i.e. finds service instances) if the RestTemplate is invoked first. Looks like the RestTemplate setup performs some (static) initialisations somewhere that then benefit to the feign client.
Do I miss something in how Feign support should be configured?

Related

Spring cloud - Server and client

I'm config an application using spring cloud eureka. I start my discovery app in the 8761 port and reaching the console in "http://localhost:8761".
So, I start my client application and it's appear in the "Application" page of eureka console.
First question:
My client is using "server.port=0" in properties config so the tomcat port is starting in random. How can I reaching my services in client? Example: I have a get request in "/api/stuff", is that possible to access this not using the random port? Suppose I don't know the port!
Second Question:
I can start any clients I want, they will start, assuming a random port and register in the cloud server discovery, I can see the log:
"Registering application FLY-CLIENT with eureka with status UP"
But they don't appear in "Application" page of eureka console, why they don't appear?
Thanks!
If you are using Spring RestTemplate to request the services registered in Eureka you need to make it #LoadBalanced, something like this should do the try:
#LoadBalanced
#Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
// usage
restTemplate.getForObject("http://your-service-name/api/stuff", StuffResponse.class);
As for the 2nd question, I'm a little confused, as you mentioned earlier in the question that your application appears on Eureka's dashboard. Is this behavior only happening for the "fly-client"?

How to use http2 in Feign for better performance RPC?

A new project consider use Spring Cloud build micro service. But we have many inner RPC call within services.
For performance, how to upgrade Feign support http2?
There are gRPC has give a great example for high performance by http2 but our project based on JVM and Feign and relative annotation is good enough for interface definition.
So, I'm first consider Feign support http2 without SSL to speed up RPC.
Hope there are benchmark on http2 if someone has done.
Thanks.
To use http/2 with feign you just need to replace the Http client Bean with a client that prefers http/2.
With default HttpClient (as opposed to OkHttp), you can just add this as a bean, and feign should use it, or you can build it into the feignClient:
HttpClient client = HttpClient.newBuilder()
.version(Version.HTTP_2)
.build();
ref: https://openjdk.java.net/groups/net/httpclient/intro.html
The OkHttp Client may have http/2
I think you would also have to enable http/2 on the server with:
server.http2.enabled: true
This should allow it to receive incoming requests with http/2.
https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#application-properties.server.server.http2.enabled

Zuul routing to a mutual auth endpoint

I was trying to set up a Zuul proxy using a Spring boot application which can either produce mock response or reaching out to an external endpoint. The communication to the external endpoint uses mutual authentication where we need to present truststore and keystore files. The implementation of SimpleHostRoutingFilter default route filter doesn't seem to have any implementation to present the certs in newConnectionManager() method.
I tried to override that method by extending SimpleHostRoutingFilter, but no luck.
How do we overcome this? Please help
From Spring Cloud Edgware release, there is a way to provide your own HttpClient. If you are using Apache Http Client (it's default), you can create a bean of type ClosableHttpClient. If it is provided as a Spring Bean, SimpleHostRoutingFilter will be created with your own Http client. You can handle any your requirement with this.
You can find the brief note about this here.
You can find the code related to this change here.

How to disable eureka lookup on specific #FeignClient

I have a microservice that uses #FeignClient predominantly to talk to other micro-services. This works beautifuly using Eureka's service discovery mechanism.
Now I have a pressing need to use a #FeignClient to connect to an external system and still perform load balancing using configurations as shown below.
Feign client:
#FeignClient("externalServers")
public interface ExternalServersClient {
#RequestMapping(method = RequestMethod.GET, value = "/someExternalUrl")
ResponseEntity<Object> callExternalServer();
}
application.yml:
externalServers:
ribbon:
listOfServers: server1:18201,server2:18201
From many documentations that I have gone through, it is adviced to disable eureka to allow loadbalancing to be picked up from available listOfServers. I did follow that up and used following configuration to disable it.
application.yml:
ribbon:
eureka:
enabled: false
This allowed me to perform loadbalancing for feign client targeting external systems but all other feign clients that need to use service discovery broke.
Is there any way to disable eureka for feign client setup for external system alone but allow it to function normally for other clients?
Thanks in advance!
In spring-cloud-netflix 1.2.0 (part of the Camden release train), the ability to set the server list implementation was recently added.
You'll be able to do the following:
externalServers:
ribbon:
NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
listOfServers: server1:18201,server2:18201
Probably released sometime in August or September.

Spring Integration annotation implementation with multiple gateway

I have a requirement to implement a serial web service call to 2 web services, say ServiceA followed by ServiceB. I implemented this using xml, having two separated gateways for each service and all, I wanted to know how to do this using annotation. How will I mention which service activator goes with which gateway.
Thanks