Spring Cloud Load Balancer - Custom Load Balancer Client config through Java - spring-cloud-loadbalancer

Specifying custom configuration for load balanced services is possible through the use the "LoadBalancerClient" & "LoadBalancerClients" annotations as illustrated below.
https://docs.spring.io/spring-cloud-commons/docs/current/reference/html/#custom-loadbalancer-configuration
How can we specify the same config through Java? We have a case where the services can increase dynamically and we don't want to keep modifying code to add them. Their load balancer configs will remain similar except for the service instances. We are looking to add a generic custom config which can then return the supplier list depending on the service name.

Declaring a bean of type LoadBalancerClientFactory containing the list of all applicable LoadBalancerClientSpecification did the trick.
Pretty straightforward but had to dig around to figure out which bean to expose as there was no example that I could find.

Related

how to customize the ribbon load balancer in Zuul server

As I understand the default load balancer using in Zuul proxy server as DynamicServerListLoadBalancer and it use ZoneAffinityServerListFilter rule to choose server. However, is there any way I can customize the loadbalancer used in zuul proxy server
I have tried to add following configuration to change to loadbalancer Rule:
eureka.client.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RoundRobinRule
But seems it still stick with default configuration.
Any advice is highly appreciated.
To change load balancing rule with configuration, you should define ribbon configuration like below.
your_ribbonclient_name.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RoundRobinRule
your_ribbonclient_name shoud be replaced with the proper one for your configuration. In Zuul, ribbon client name is same with service id for each route.
You can also provide your own IRule bean for load balancing rule with #RibbonClient like the following.
#RibbonClient(name = "your_ribbonclient_name", configuration = YourConfigurationClass.class)
You can find an example code here
If you want to apply your Ribbon config to whole ribbon clients in your server,
you can do that with #RibbonClients (not #RibbonClient).
#RibbonClients(defaultConfiguration = DefaultRibbonConfig.class)
You can find the example code here, and the related issues is here.

Service and Replication controller

Is it possible to create a replication controller and service for a containerized application using one configuration file (yml/json)
yes, you can have a normal yaml array of objects under List type, typical example can be found in the main repo like https://raw.githubusercontent.com/kubernetes/kubernetes/master/hack/testdata/list.yaml

Keycloak and Docker - Cannot set two types of URLs

I use standalone version of keycloak in docker-based application.
Since Keycloak 1.9.2 there is an "auth-server-url-for-backend-requests" attribute removed from keycloak properties.
This field was by me to indicate the internal ip address of auth server (inside a dock).
The external one (auth-server-url) is used for redirection purpose.
My question is: how to replace former auth-server-url-for-backend-request to solve a problem of having different network addresses inside docker and outside of it.
According to the following links, it appears you can use the same DNS for external requests as you would for internal. See these:
keycloak issue
http://keycloak.github.io/docs/userguide/keycloak-server/html_single/index.html#d4e4114
You should set the KEYCLOAK_FRONTEND_URL parameter in the Dockerfile or docker-compose.yml (if you use them). In other case your should set this parameter in Keycloak General settings UI.
Eg.:
It is quite tricky because you shouldn't set the real front-end's URL, however you should set the URL which is used by front-end. I have the same problem so you can see some examples in my SO question/answer

How to timeout a domain class cache in Grails (i.e. specify max age)

Mostly static, frequently accessed domain classes such as countries and currencies can benefit greatly from caching.
class country {
:
static mapping = {
cache usage: 'read-only', include: 'non-lazy'
}
}
However, once in a while, a new country is added or an existing one modified in a multi-server environment. The solution we are looking for is a cache timeout, so that after a configurable time per domain class, e.g. 10 minutes, it re-reads the data when a get is requested.
Currently, the only option is if you cache something, you have to restart all the servers. We don't want the complexity of a distributed cache, which broadcasts changes, just a cache aging option on a per domain basis.
Is this possible with grails 2.4.4, or do we need to implement our own caching layer? With MyBatis specifying the max age (aka cache timeout) is easy - hope grails has an undocumented feature for this.
Restarting the server is very far from being the only option.
The configuration options for caching in the mapping block are quite limited, but that's at least in part because there's no standard configuration API, so you need to do it differently depending on the provider.
The default implementation is Ehcache, and it's pretty simple to configure. If Ehcache finds an ehcache.xml file in the root of the classpath, it will use that instead of its defaults. Non-Java files in src/java and non-Java/non-Groovy files in grails-app/conf are copied to a directory that's in the classpath during compilation, so putting your file there is the best bet for making it accessible.
Use this heavily commented example ehcache.xml file to get started.
If you want to remove one or more cached instances, Hibernate has an API for that. There are several "evict" methods that you can call on the sessionFactory bean, e.g. sessionFactory.evict(Book) removes all cached Book instances, sessionFactory.evict(Book, 5) removes the cached Book with id 5, etc.
There's a lot of relevant info in the slides from this SpringOne/2GX talk including configuring Ehcache and working with the 2nd-level cache API.

How to get domain/subdomains of an app outside of controllers/views?

How can I get the domain and subdomains of my application without being in a controller/view?
The reason for needing this is that I am subscribing to process_action.action_controller notifications within an initializer, and I need my applications' url from within that initializer.
If the host part of the URL (domain, subdomain) is dynamic ... which is to say "depends on the request" then, of course, the only way to get it is within the context of the request itself, which the controllers and views know about.
So I am assuming the application has a known host, perhaps dependent upon runtime environment (production, test, development, etc.), or maybe based on the server environment, but otherwise static. In this case, you could define a custom config variable containing the name, as noted in the more recent answer from Jack Pratt on this SO question: How to define custom configuration variables in rails.

Resources