学习负载均衡Ribbon时,启动项目报错No instances available for SPRINGCLOUD-PROVIDER-DEPT......

学习内容

这块报的错误是在学负载均衡Ribbon时,启动项目后,客户端(消费者)去调用时产生的

报错详情

在这里插入图片描述

关于Ribbon的配置

导入相关的依赖

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-ribbon -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <!--   Eureka服务提供者的jar包     -->
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

application.yml配置

在这里插入图片描述
我的错误出现在这里,fetch-registry:必须是true,不能是false,为true时的意思是表示检索注册中心的所有服务。如果为false则无法发现服务

自定义Spring配置类:ConfigBean.java配置负载均衡实现RestTemplate

@Configuration // 相当于spring中的 applicationContext.xml
public class ConfigBean {

    /**
     *  配置负载均衡实现RestTemplate
     */
    @Bean
    @LoadBalanced    //实现 Ribbon 的注解
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

controller中业务

@RestController
public class DeptConsumerController {

    /**
     *  (url,实体:Map,Class<T> responseType)
     *  提供多种便捷访问远程http服务的方法,简单的Restful服务模板
     */
    @Autowired
    private RestTemplate restTemplate;

    private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-DEPT";

    @RequestMapping("/consumer/dept/list")
    public List<Dept> list(){
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
    }
}

启动查看结果

在这里插入图片描述

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值