your solution is not realy good, because
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@LoadBalanced
public KeycloakRestTemplate keycloakRestTemplate(
KeycloakClientRequestFactory keycloakClientRequestFactory,
LoadBalancerInterceptor interceptor) {
KeycloakRestTemplate result = new KeycloakRestTemplate(
keycloakClientRequestFactory);
// Add the interceptor for load balancing
result.getInterceptors().add(interceptor);
return result;
}
not has worked, becouse you have got exception
The dependencies of some of the beans in the application context form a cycle:
...
┌─────┐
| keycloakRestTemplate defined in class path resource [...]
↑ ↓
| ribbonInterceptor defined in class path resource [org/springframework/cloud/client/loadbalancer/LoadBalancerAutoConfiguration$LoadBalancerInterceptorConfig.class]
↑ ↓
| org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration (field private java.util.List org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration.restTemplates)
└─────┘
What do you have to do?
@Bean
@LoadBalanced
public KeycloakRestTemplate keycloakRestTemplate(
KeycloakClientRequestFactory keycloakClientRequestFactory) {
return new KeycloakRestTemplate(keycloakClientRequestFactory);
}
without @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) then you get singleton which you cane use like this
@Autowired
protected KeycloakRestTemplate restTemplate;
or you can define restTemplate like prototype
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@LoadBalanced
public KeycloakRestTemplate keycloakRestTemplate(
KeycloakClientRequestFactory keycloakClientRequestFactory) {
return new KeycloakRestTemplate(keycloakClientRequestFactory);
}
and then use like this
@Autowired
@LoadBalanced
protected KeycloakRestTemplate restTemplate;
Edit:
The solution of Xtreme Biker has not worked with SpringBoot 2 and Keycloak 6 because of problem with cycle, my first proposition is not threads/sesions save, the second does not work because of been will be createing before run @LoadBalanced and the restTemplate wchich is created based on prototype is to without sets interceptor :|