无法使用Spring Cloud Consul和@EnableDiscoveryClient连接RestTemplate


哇塞
2025-01-29 02:32:32 (18天前)
  1. 我正在尝试评估使用


领事
</跨度>
对于服务发现,但是使用简单的项目,RestTemplate不会自动连接。使用Spring Cloud

领事
</跨度>
1.0.0.M6和Spring Boot 1.3.3。

望着

领事
</跨度>
UI,我可以看到我的“多媒体”服务注册,如果我在下面的代码中注释掉RestTemplate的使用,我可以看到服务实例信息。

是否支持RestTemplate的自动接线

2 条回复
  1. 0# 布偶的表弟派大星丶 | 2019-08-31 10-32



    我们在路上做了一些改变

    RestTemplate

    已配置。它已不再为您创建。看到

    更新的文档

    。你需要创建一个

    RestTemplate

    豆自己。




    1. @Configuration
      public class MyConfiguration {

    2. @LoadBalanced
    3. @Bean
    4. RestTemplate restTemplate() {
    5.     return new RestTemplate();
    6. }
    7. }

    8. public class MyClass {
      @Autowired
      private RestTemplate restTemplate;

    9. public String doOtherStuff() {
    10.     String results = restTemplate.getForObject("http://stores/stores", String.class);
    11.     return results;
    12. }
    13. }

    14. </code>

登录 后才能参与评论