![](/img/trans.png)
[英]How to use Apache ZooKeeper with Spring Cloud service discovery and load balancing?
[英]Integrate Spring Cloud load balancing with KeycloakRestTemplate
我有一个配置了 Spring Cloud 发现的微服务环境,因此我可以仅使用它们的 id 访问其他服务实例:
public class MyClass {
@Autowired
@LoadBalanced
private RestTemplate restTemplate;
public String doOtherStuff() {
String results = restTemplate.getForObject("http://stores/stores", String.class);
return results;
}
}
现在我想访问需要 OAuth2 授权的服务。 我使用 Keycloak 服务器来提供它,而 Keycloak 已经提供了一个带有特定KeycloakRestTemplate的适配器。 无论如何,如何通过负载平衡来增强它?
我们需要创建一个特定的KeycloakRestTemplate,它将使用LoadBalancerInterceptor :
@Configuration
@EnableWebSecurity
@ComponentScan(basePackageClasses = KeycloakSecurityComponents.class)
public class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
@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;
}
//More configurations for keycloak
}
因此,有机会获得一个Authorized / LoadBalanced模板:
@Autowired
@LoadBalanced
protected KeycloakRestTemplate restTemplate;
也可以看看:
你的解决方案不是很好,因为
@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;
}
没有工作,因为你有例外
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)
└─────┘
你必须做什么?
@Bean
@LoadBalanced
public KeycloakRestTemplate keycloakRestTemplate(
KeycloakClientRequestFactory keycloakClientRequestFactory) {
return new KeycloakRestTemplate(keycloakClientRequestFactory);
}
then you get singleton which you cane use like this没有那么你会得到单例,你可以像这样使用
@Autowired
protected KeycloakRestTemplate restTemplate;
like prototype或者你可以像原型一样定义
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@LoadBalanced
public KeycloakRestTemplate keycloakRestTemplate(
KeycloakClientRequestFactory keycloakClientRequestFactory) {
return new KeycloakRestTemplate(keycloakClientRequestFactory);
}
然后像这样使用
@Autowired
@LoadBalanced
protected KeycloakRestTemplate restTemplate;
wchich is created based on prototype is to without sets interceptor :|编辑:由于循环问题,Xtreme Biker 的解决方案没有与 SpringBoot 2 和 Keycloak 6 一起使用,我的第一个命题不是线程/会话保存,第二个不起作用,因为将在运行 @LoadBalanced 和之前wchich是基于prototype创建的,不需要设置拦截器:|
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.