Configure FeignClient to use url by env variable without disabling Eureka - spring

My current environment is a Spring Cloud setup using Eureka and I have multiple Feign clients in the application. What I want to do is to allow most of my Feign clients to resolve their services via the discovery server but prevent one or two from doing so in order to use my local instance that I am currently developing on. I'm running the service I'm developing and the client application locally.
I would like the client application to use discovery for all over service discovery and override one Feign Client to use only my local running service.
Is there a way to do this without disabling Eureka on the client? I have explored these two questions (one, two) and have not managed to get the listOfServers field to have any impact unless I disabled Eureka. If it matters in working on this I made the service I wish to connect to not register with eureka.

You can specify a URL for a specific FeignClient without disabling Eureka client with property <ribbonclientname>.ribbon.NIWSServerListClassName property.
Assume that service id for directing routing is testA. You can define the below property without disabling Eureka client.
testA:
ribbon:
NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
listOfServers: ${url for your test server}
If you specify com.netflix.loadbalancerConfigurationBasedServerList as NIWSServerListClassName, ribbon client inside your Feign client will use address that was given via listOfServers property without disabling eureka.

I got a way to pass the environment variables in a very simple way:
interface FeignClient
#FeignClient(url = "https://"+"\${url}", name = "limit", decode404 = true)
interface HbFeignClient {
#GetMapping("/credit-limit")
fun getLimitCompany(#RequestParam(required = true) companyId: Long): ResponseEntity<Any>
properties
#URL
url=${URL}
.env
URL=https:localhost:8080

Related

Programmatically find Eureka server host and port

I want to setup Eureka server for service registry and erueka clients that will get access rest api using the service registry from Eureka server. But we use mesos as our container management system and when we start the Eureka server we will not know which host and port it is running on. So there is an api that we can use that gives us the complete url where Eureka server will be running. Now all the examples for Eureka client that I have seen have hard coded the Eureka server in the config file. Is it possible for Eureka clients to use that api and get the url for Eureka server.
FYI: we are using spring boot and have our own DC and are not on any cloud
I think I found the answer but if someone can confirm that will be great. I need to create my own EurekaClientConfigBean and override the following methods:
public void setEurekaServiceUrlPollIntervalSeconds(int eurekaServiceUrlPollIntervalSeconds)
List<String> getEurekaServerServiceUrls(String zone);

Spring Ribbon RestTemplate - Mix Eureka and Real URLs

I have a need to mix calls to a Spring Rest Template using either a Eureka-compliant service name (example-service-name) or a real url (http://my-url.com).
The Eureka-friendly URLs work fine, as Ribbon can look them up without issue. Obviously real URLs fail because they can't be found.
I'd like to be able to integrate Eureka-URLs over time, while maintaining the existing functionality for Spring property-driven direct URLs.
Can I
Configure Ribbon to fall back to a default non-Eureka behavior in the event it fails to resolve a URL ?
or
Spoof Eureka Name/URL pairs in my local Spring configuration and include them in Ribbon's Eureka url resolution ?
Edit:
Real URLs are failing because the Ribbon client throws an exception if a Eureka lookup fails
Caused by: java.lang.IllegalStateException: No instances available for http://my-url.com
at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:75)
you can regist url as a fake service use this:
my-url:
ribbon:
listOfServers: my-url.com
NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
and then use http://my-url/xx/xx

How to capture log on each instance of the microservice through zuul

I have setup multiple instances of my microservice and registered to my eureka server. It uses ribbon for client side load balancing and uses zuul as gateway server. All usual stuff. I would like to capture the logs of which instance of my service is responding for each request. So that I can able to bring some conclusion based on my usage of each instances. How to do that?
You can try to set the loglevel just of the LoadBalancerContext to debug in application.properties
#logging
logging.level.com.netflix.loadbalancer.LoadBalancerContext=DEBUG

Route Existing Services via ZUUL without adding routing rule

I am trying to route existing services via Spring cloud Netflix Zuul.
I have an existing service available at below url,
http://localhost:3080/query-service/getquery/1
Out of the box, with zuul I can route to the service as below,
localhost:9000/queryservice-id/queryservice/getquery/1, with "queryservice-id" as the service-id of the service when it is registered in Service Registry. The zuul port is 9000.
I do not want to change the context path and service path information when accessing the service via ZUUL.
With ZUUL, I want to access the service as below,
http://localhost:9000/query-service/getquery/1
I do want to prefix with the "serviceId". This is because I do not want to impact any existing clients of the service. Only the host and port changes, without serviceId.
I was able to accomplish it as below with ZUUL Configuration,
zuul:
routes:
query-service:
path: /query-service/**
serviceId: query-service
stripPrefix: false
With the above configuration, I am able to only use the zuul host and port, the other service specific information remains as before. Seems like stripPrefix is helping in routing without the serviceId.
But, I have a lot of services and will be adding more services too.
For every such service, I do not want to be adding a rule like that to ZUUL configuration which will mean rebuilding and recycling the ZUUL Service.
I feel there should be a simpler and better way to accomplish this, without a big effort, because the change I want to do is common to all services
Is there a way to making this change common for alll the services I want
to be routed via ZUUL.
Thanks,
As checked with the Spring Cloud Netflix team this is recommended approach.
https://github.com/spring-cloud/spring-cloud-netflix/issues/1549

Spring Cloud Sidecar is not working as expected

Based on the documentation, the #EnableSidecar annotation acts as a proxy for non-JVM applications that wish to register in Eureka. Accordingly, the configuration to be set is:
sidecar:
port: 81 <-- Port of the non-JVM application
health-uri: http://10.135.16.50:${sidecar.port}/api/health.php <-- URI of the non-JVM application's health endpoint
home-page-uri: http://10.135.16.50:${sidecar.port}/
Once the "sidecar" is up and running, we should be able to invoke one of the non-JVM endpoints through the service registry just by using the name that the "sidecar" application used to register in Eureka. So for example, if our "sidecar" application was registered in Eureka as "php-sidecar" to proxy a PHP application with an endpoint such as:
http://10.135.16.50:81/api/microservice.php
Then we should be able to invoke the following endpoint to get to the non-JVM application (assuming "sidecar" is in "localhost" and port 8080):
http://localhost:8080/php-sidecar/api/microservice.php
However, this is not working as expected. When we request the URI just above, a request to "localhost:81" is actually issued, because somehow the "sidecar" is picking up its host URI and not the home-page-uri defined as part of the sidecar's properties.
If we run the non-JVM application locally using localhost, then everything works as expected, but this is definitively not the realistic use case.
Therefore, what am I missing in my configuration to tell Spring Cloud (Zuul in this particular case) to use the non-JVM home-page-uri and not my local host URI?
Thanks for your support.
After some research, it turns out that the sidecar must always be deployed in the same host as the non-JVM application.
After some research, it turns out that the sidecar must always be deployed in the same host as the non-JVM application.
No, You can add sidecar.ip-address = 10.135.16.50 to your yaml config file
I'm using spring-boot-starter-parent version is 1.5.6.RELEASE
working fine

Resources