【发布时间】:2018-10-04 11:16:15
【问题描述】:
我正在使用 OAuth2RestTemplate 来通过 REST 请求传递 oauth 令牌。但是,我现在需要对我的网址进行硬编码,例如
restTemplate.postForLocation("http://localhost:5555/other-service2/message", "Message")
而当我使用自行创建的带注释的功能区(使用 @LoadBalanced)RestTemplate bean 时,我可以执行类似的操作
restTemplate.postForLocation("http://service1/other-service2/message", "Message")
这是因为当您使用 LoadBalanced 时,它会自动将其设为 Ribbon Rest 模板,让您可以使用服务发现功能或 Eureka,但是当您使用 @Loadbalanced 注释 OAuth2RestTemplate bean 时,它会在尝试使用 OAuth2RestTemplate 时的运行时,它说
o.s.b.a.s.o.r.UserInfoTokenServices : Could not fetch user details: class java.lang.IllegalStateException, No instances available for localhost
我的 OAuth2RestTemplate 创建看起来像
@LoadBalanced
@Bean
public OAuth2RestTemplate restTemplate(final UserInfoRestTemplateFactory factory) {
final OAuth2RestTemplate userInfoRestTemplate = factory.getUserInfoRestTemplate();
return userInfoRestTemplate;
}
如何在 OAuth2RestTemplate 上使用 Eureka 功能区的服务发现功能和负载平衡功能?
【问题讨论】:
标签: java spring-boot spring-security spring-security-oauth2 netflix-eureka