我正在尝试使用基于书签服务示例的 RestTemplate 进行功能区配置,但没有运气,这是我的代码:
@SpringBootApplication
@RestController
@RibbonClient(name = "foo", configuration = SampleRibbonConfiguration.class)
public class BookmarkServiceApplication {
public static void main(String[] args) {
SpringApplication.run(BookmarkServiceApplication.class, args);
}
@Autowired
RestTemplate restTemplate;
@RequestMapping("/hello")
public String hello() {
String greeting = this.restTemplate.getForObject("http://foo/hello", String.class);
return String.format("%s, %s!", greeting);
}
}
错误页面如下:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Mar 22 19:59:33 GMT+08:00 2016
There was an unexpected error (type=Internal Server Error, status=500).
No instances available for foo
但是如果我删除注释@RibbonClient,一切都会好起来的,
@RibbonClient(name = "foo", configuration = SampleRibbonConfiguration.class)
这是 SampleRibbonConfiguration 实现:
public class SampleRibbonConfiguration {
@Autowired
IClientConfig ribbonClientConfig;
@Bean
public IPing ribbonPing(IClientConfig config) {
return new PingUrl();
}
@Bean
public IRule ribbonRule(IClientConfig config) {
return new AvailabilityFilteringRule();
}
}
是因为 RibbonClient 不能和 RestTemplate 一起工作吗?
另一个问题是,是否可以通过 application.yml 配置文件配置像负载均衡规则这样的 Ribbon 配置?从Ribbon wiki看来,我们可以在属性文件中配置 NFLoadBalancerClassName、NFLoadBalancerRuleClassName 等 Ribbon 参数,Spring Cloud 是否也支持这个?