mkczyk
mkczyk

Reputation: 2710

Providing URL for @FeignClient by service name in application.properties without load balancer

I want to use Feign client and provide URL in application.properties by service name.

Prerequisites:

I use Spring Boot and I have dependency to Feign from Spring Cloud:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

I enabled Feign clients:

@SpringBootApplication
@EnableFeignClients
public class MyApplication {
    public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); }
}

Attempts:

1. Hardcoded URL in code

I know how to hardcore URL in code:

@FeignClient(name = "some-service", url = "http://some-service.com/")
public interface SomeServiceClient {}

But I want to use properties file.

2. Hardcoded property

I know how to hardcode property to read URL from properties:

@FeignClient(name = "some-service", url = "${some-service.url}")
public interface SomeServiceClient {}

application.properties:

some-service.url=http://some-service.com/

It's a far better, but I want to decouple property name from Feign client. I would like to use only name.

3. Ribbon with load balancer

I know that Spring Cloud provides Ribbon when I can specify list of URLs (by Feign client name) for Ribbon's load balancer.

@FeignClient(name = "some-service")
public interface SomeServiceClient {}

application.properties:

some-service.ribbon.listOfServers=http://some-service.com/
ribbon.eureka.enabled=false

Notice: I have to disable Eureka (which is enabled for default) because I don't need Eureka server. I need to provide URL in application.properties in my application.

Dependency:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

This solution seems better because my Feign client in code contains only name (without URL or coupled property name for URL). That's exactly what I wanted to get.

But in logs I see that Spring/Ribbon runs load balancer for me:

2020-11-03 23:39:01.832  INFO 12168 --- [nio-8080-exec-2] c.netflix.config.ChainedDynamicProperty  : Flipping property: some-service.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2020-11-03 23:39:01.892  INFO 12168 --- [nio-8080-exec-2] c.netflix.loadbalancer.BaseLoadBalancer  : Client: some-service instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=some-service,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2020-11-03 23:39:01.908  INFO 12168 --- [nio-8080-exec-2] c.n.l.DynamicServerListLoadBalancer      : Using serverListUpdater PollingServerListUpdater
2020-11-03 23:39:01.955  INFO 12168 --- [nio-8080-exec-2] c.netflix.config.ChainedDynamicProperty  : Flipping property: some-service.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2020-11-03 23:39:01.970  INFO 12168 --- [nio-8080-exec-2] c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client some-service initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=some-service,current list of Servers=[jsonplaceholder.typicode.com:443],Load balancer stats=Zone stats: {unknown=[Zone:unknown;  Instance count:1;   Active connections count: 0;    Circuit breaker tripped count: 0;   Active connections per server: 0.0;]
},Server stats: [[Server:jsonplaceholder.typicode.com:443;  Zone:UNKNOWN;   Total Requests:0;   Successive connection failure:0;    Total blackout seconds:0;   Last connection made:Thu Jan 01 01:00:00 CET 1970;  First connection made: Thu Jan 01 01:00:00 CET 1970;    Active Connections:0;   total failure count in last (1000) msecs:0; average resp time:0.0;  90 percentile resp time:0.0;    95 percentile resp time:0.0;    min resp time:0.0;  max resp time:0.0;  stddev resp time:0.0]
]}ServerList:com.netflix.loadbalancer.ConfigurationBasedServerList@6a4bfe52

I don't need load balancer (I have only one URL). How to disable it and always get first of listOfServers? Maybe there's a way to provide custom implementation of load balancer? Or at least disable logs for redundant load balancer?

Or how to configure one URL for service name in other way?

I read documentation but I didn't find solution:

Upvotes: 1

Views: 7647

Answers (1)

stefan123t
stefan123t

Reputation: 343

I am facing the same question, did you try the following as suggested in EnableFeignClients makes Spring not able to load the context ?

spring.main.allow-bean-definition-overriding=true
feign.client.config.some-service.url=http://some-service.com/

Upvotes: 0

Related Questions