我有microservice landscape配置了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的适配器。无论如何,如何使用load balancing来增强它?
答案 0 :(得分:2)
我们需要创建一个特定的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;
另见:
答案 1 :(得分:-1)
您的解决方案不是很好,因为
SELECT COUNT(
DISTINCT
SYS.ANYDATA.getTypeName(
SYS.ANYDATA.convertObject( value )
)
) AS num_types
FROM test_data
没有用,因为您有异常
@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)
└─────┘
没有 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE),那么您会得到像这样使用的单例
@Bean
@LoadBalanced
public KeycloakRestTemplate keycloakRestTemplate(
KeycloakClientRequestFactory keycloakClientRequestFactory) {
return new KeycloakRestTemplate(keycloakClientRequestFactory);
}
或者您可以像原型一样定义 restTemplate
@Autowired
protected KeycloakRestTemplate restTemplate;
然后像这样使用
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@LoadBalanced
public KeycloakRestTemplate keycloakRestTemplate(
KeycloakClientRequestFactory keycloakClientRequestFactory) {
return new KeycloakRestTemplate(keycloakClientRequestFactory);
}
编辑: Xtreme Biker的解决方案由于周期问题而无法与SpringBoot 2和Keycloak 6配合使用,我的第一个建议不是线程/会话保存,第二个建议不起作用,因为将在运行@LoadBalanced和之前创建restTemplate wchich是基于原型创建的,即没有设置拦截器:|