Create multiple Ribbon clients dynamically - java

I have a spring microservices architecture application but we aren't using eureka or any other service discovery. My requirement is to create a dynamic load balancer. So, for each services there will be unique and dynamic set of servers.
The configuration will be something like:
ribbon:
listOfServices: say-hello-service, say-hi-service
What I don't want to do is, repeat #RibbonClient annotation for each service client that I will use.
I found out that RibbonClientSpecification is the key component of the Ribbon factory. If I register it with names say-hello-service.RibbonClientSpecification and say-hi-service.RibbonClientSpecification, it will try to call respective service even though I don't have #RibbonClient and any Ribbon related configuration. So my questions are
Why is #RibbonClient annotation necessary since we can identify the services from the properties defined? It seems to me that, it is redundant what we have to define ribbon properties in properties file and as well as have the annotation with same name.
How do I register load balancers for each ribbon client that I create dynamically?
What is the right of creating multiple ribbon clients dynamically without having multiple #RibbonClient configuration classes?

You can use its as
#RibbonClient("{services1,service2}")
No need to specify or create new ribbon all the time for different service.

Related

Spring boot Load configuration per environment using PropertySource

I'm trying to add authorization to several microservices. Given all the services share similar authorization process, I want to extract the logic to a shared library.
I managed to create library, but I realise all the configurations need to be set in the application.yml file in the microservice which calls the library. I don't want to expose some of the configurations at service layer though.
After some searches, I found I could set #PropertySource("library.properties") in my library's configuration class to force reading properties from the specified .properties file within the library.
The problem now is I want to set different values for different environments, e.g. authorization URL for test and production would be different. How can I configure the file so that the configuration class would read same property value based on active profile (e.g. environment = test/staging/production)?
You can have multiple property files such as "application-environment.yml” in your resource folder. Spring framework picks the right one based on the active profile.
For example, if you define a “staging” environment and have a staging profile and then your property file should be named as application-staging.yml.

Can Eureka registry one service like two different services?

I have few micro services and I use eureka for service discovery. I want to split one micro service, but I can't do it from code now(create separate jar). I want to registry one service twice in eureka with same address but different service name. Can I do it?
The answer is No!
Assuming you are using Spring Boot, the Service name is going to be your spring-application-name. Hence, you cannot have more than one name registered from a single Jar
But you have mentioned that you have the constraint that the address should also be same. May be that if that can be relaxed, through Spring Cloud Config you can start multiple instance of your Service with different Profile names and under each profile's config you can override your spring-application-name.

How to detect unused spring beans configured in XML?

I have a java project in which many .xml files are present.
All these xml files contain many beans.
The test for unused is:
A bean that is defined but never injected.
A bean that is injected but is never called in the code.
A bean that is defined but never loaded into the Spring context.
Questions
How do I identify which bean is used or not?
Is there a utility to do that?
There is no tools for detecting the unused springbeans in xml file. You can use the Spring Tools Suite for detecting. But it is taking too much time for checking.
I think that you can use spring actuator to check the opposite problem - which beans have been loaded in your context.
https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html
Actuator endpoints allow you to monitor and interact with your
application. Spring Boot includes a number of built-in endpoints and
you can also add your own. For example the health endpoint provides
basic application health information.
The way that endpoints are exposed will depend on the type of
technology that you choose. Most applications choose HTTP monitoring,
where the ID of the endpoint is mapped to a URL. For example, by
default, the health endpoint will be mapped to /health.
beans Displays a complete list of all the Spring beans in your application.
Looking for all the beans which are unused requires scanning XML files within application, then you can compare it with the list of beans produced by actuator.

Spring Cloud shared configuration for group of microservices

Is it possible to share some properties for limited group of microservices? I mean I just want to declare common datasourse in one place for several microservices, which will use the same database
I try to implement it using gradle variables, which should consist all data related to db connection with profiles, but probably easier way to do it exists.
Thanks
Yes it is. You're looking for "Spring Cloud Config" server:
Spring Cloud’s config server acts as a single source of configuration
data for all other services in a microservice-based application. It is
itself a microservice whose job is to obtain configuration data from a
Git repository and serve it via RESTful endpoints. All other services
can either consume the config server’s API directly or, if they’re
Spring applications, treat the configuration server as another
property source in Spring’s Environment abstraction.
You can find a nice reading along with examples here.

Different bean configuration depending on runtime?

I have a bean, say manager, which is used all over my application for remoting. The bean is using httpclient which in turn can be configured with or without proxy. But this configuration can be only one. What i want in my application is: when the application started, the user is asked does she want to use a proxy or not? And depending on the user's answer the bean is properly configured and only then started. Some sort of dynamic configuration during runtime.
Is it possible or maybe I should achieve this some other way?
Thank you.
Why can't you call setProxy() on httpclient configuration depending on user's input?
Alternatively you can configure httpclient as bean in Spring context (either directly or create a simple wrapper) two times - one with proxy and one without. Then manager can choose which one to use depending on user's input (both can be injected into manager).

Categories

Resources