1

我有一个配置了 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的适配器。无论如何,如何通过负载平衡来增强它?

4

2 回答 2

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;

也可以看看:

于 2015-12-23T12:48:34.643 回答
0

你的解决方案不是很好,因为

@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);
}

没有@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)那么你会得到你可以像这样使用的单例

@Autowired
protected KeycloakRestTemplate restTemplate;

或者你可以像原型一样定义restTemplate

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@LoadBalanced
public KeycloakRestTemplate keycloakRestTemplate(
        KeycloakClientRequestFactory keycloakClientRequestFactory) {
    return new KeycloakRestTemplate(keycloakClientRequestFactory);
}

然后像这样使用

@Autowired
@LoadBalanced
protected KeycloakRestTemplate restTemplate;

编辑:由于循环问题,Xtreme Biker 的解决方案无法与 SpringBoot 2 和 Keycloak 6 一起使用,我的第一个提议不是线程/会话保存,第二个提议不起作用,因为将在运行 @LoadBalanced 和restTemplate之前创建wchich 是基于原型创建的,没有设置拦截器:|

于 2019-10-02T07:14:58.567 回答