1. pom.xml添加配置
    说明:这里服务注册与发现用的是Eureka,所以消费者端需要引入eureka,使用EurekaClient来调用服务

    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-ribbon</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
  2. 修改application.yml 添加eureka的服务注册地址,如下图:
    说明:下图中的 service-url配置的是3个eureka集群服务地址
  3. ConfigBean代码
    说明:主要代码,获取RestTemplate时,添加@LoadBalanced注解实现客户端负载均衡

    package com.thr.springcloud.cfgbean;
    
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.client.RestTemplate; import com.netflix.loadbalancer.IRule;
    import com.netflix.loadbalancer.RandomRule; /**
    * 注解@Configuration说明:相当于spring的applicationContext.xml
    * 即此时加了@Configuration注解的CfgBean类 等同于 applicationContext.xml
    * @author xiongxiaomeng
    */
    @Configuration
    public class CfgBean { /**
    * 使用RestTemplate调用Rest服务
    * @LoadBalanced 获取RestTemplate时,加入Ribbon的负载均衡的配置
    * @return Rest服务调用模板
    */
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate()
    {
    return new RestTemplate();
    } /**
    * 定义负载均衡算法:
    * 默认:轮询,当前改成:随机
    * @return 随机算法
    */
    @Bean
    public IRule myRule() {
    return new RandomRule();
    } }
  4. 主启动类添加EurekaClient配置:
  5. 客户端访问类 UserController_Consumer.java
    package com.thr.springcloud.controller;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate; import com.thr.springcloud.entities.User; /**
    * 消费者端的RestController
    * 消费者端 都是 调用服务提供者的Controller,这里使用RestTemplate的方式调用
    * @author xiongxiaomeng
    *
    */
    @RestController
    public class UserController_Consumer { //未加入Eureka+Ribbon之前,采用ip+端口的访问方式
    //private static final String REST_URL_PREFIX = "http://localhost:8001";
    //加入Eureka+Ribbon之后,可以直接采用服务名的调用方式,无须再使用ip+端口的调用方式
    private static final String REST_URL_PREFIX = "http://THRCLOUD-DEPT"; //使用restTemplate访问restful接口非常的简单粗暴无脑。(url, requestMap, ResponseBean.class)这三个参数分别代表
    @Autowired
    private RestTemplate _restTemplate; @RequestMapping(value="/consumer/user/add")
    public boolean add(User user) {
    return _restTemplate.postForObject(REST_URL_PREFIX + "/user/add", user, Boolean.class);
    } @RequestMapping(value="/consumer/user/get/{userId}")
    public User get(@PathVariable("userId") Long userId) {
    return _restTemplate.getForObject(REST_URL_PREFIX + "/user/get/" + userId, User.class);
    } @SuppressWarnings("unchecked")
    @RequestMapping(value="/consumer/user/list")
    public List<User> list() {
    return _restTemplate.getForObject(REST_URL_PREFIX + "/user/list", List.class);
    } @RequestMapping(value="/consumer/user/discovery")
    public Object discovery() {
    return _restTemplate.getForObject(REST_URL_PREFIX + "/user/discovery", Object.class);
    }
    }

springcloud 之Ribbon客户端负载均衡配置使用的更多相关文章

  1. SpringCloud实战-Ribbon客户端负载均衡

    前面我们已经完成了注册中心和服务提供者两个基础组件.接着介绍使用Spring Cloud Ribbon在客户端负载均衡的调用服务. ribbon 是一个客户端负载均衡器,可以简单的理解成类似于 ngi ...

  2. springcloud(十二):Ribbon客户端负载均衡介绍

    springcloud(十二):Ribbon客户端负载均衡介绍 Ribbon简介 使用分布式微服务脚骨的应用系统,在部署的时候通常会为部分或者全部微服务搭建集群环境,通过提供多个实例来提高系统的稳定型 ...

  3. SpringBoot(三) - Ribbon客户端负载均衡,Zuul网关,Config配置中心

    1.Ribbon客户端负载均衡 1.1 依赖 1.2 配置信息 # feign默认加载了ribbon负载均衡,默认负载均衡机制是:轮询 # 负载均衡机制是添加在消费端(客户端)的,如果改为随机,指定服 ...

  4. spring cloud --- Ribbon 客户端负载均衡 + RestTemplate ---心得【无熔断器】

    spring boot      1.5.9.RELEASE spring cloud    Dalston.SR1 1.前言 了解了 eureka 服务注册与发现 的3大角色 ,会使用RestTem ...

  5. spring cloud --- Ribbon 客户端负载均衡 + RestTemplate + Hystrix 熔断器 [服务保护] ---心得

    spring boot      1.5.9.RELEASE spring cloud    Dalston.SR1 1.前言 当超大并发量并发访问一个服务接口时,服务器会崩溃 ,不仅导致这个接口无法 ...

  6. SpringCloud系列之客户端负载均衡Netflix Ribbon

    1. 什么是负载均衡? 负载均衡是一种基础的网络服务,它的核心原理是按照指定的负载均衡算法,将请求分配到后端服务集群上,从而为系统提供并行处理和高可用的能力.提到负载均衡,你可能想到nginx.对于负 ...

  7. ③SpringCloud 实战:使用 Ribbon 客户端负载均衡

    这是SpringCloud实战系列中第三篇文章,了解前面第两篇文章更有助于更好理解本文内容: ①SpringCloud 实战:引入Eureka组件,完善服务治理 ②SpringCloud 实战:引入F ...

  8. 笔记:Spring Cloud Ribbon 客户端负载均衡

    Spring Cloud Ribbon 是一个基于 HTTP 和 TCP 的客户端负载均衡工具,基于 Netflix Ribbon 实现,通过Spring Cloud 的封装,可以让我们轻松的将面向服 ...

  9. SpringCloud Netflix Ribbon(负载均衡)

    ⒈Ribbon是什么? Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡工具. Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负 ...

随机推荐

  1. 在tomcat下context.xml中配置各种数据库连接池

    作者:郑文亮 Tomcat6的服务器配置文件放在 ${tomcat6}/conf 目录底下.我们可以在这里找到 server.xml 和 context.xml.当然,还有其他一些资源文件.但是在在本 ...

  2. 编译安装hls协议切片工具 m3u8-segmenter

    操作系统:Ubuntu16.04.4 amd64 安装http://m3u8-segmenter.inodes.org/方式安装m3u8-segmenter报错,于是有了这篇文章 apt instal ...

  3. 【 Linux 】Systemd 使用说明(1)

    1. 前言 在 CentOS 7 中使用 systemd 取代了 init 的启动模式,这样的更新换代有什么好处呢?首先需要对 init 和 systemd 有个概念的认识. 2. init 概述 在 ...

  4. easyui datagrid里的toobar按钮隐藏、显示、禁用等方式的实现

    easyui datagrid里的toobar按钮隐藏.显示.禁用等方式的实现 //隐藏第一个按钮 $('div.datagrid-toolbar a').eq(0).hide(); //隐藏第一条分 ...

  5. Nginx日志挂载目录为nfs文件服务器时开机无法自启动的问题解决

    为了方便收集查看日志把nginx日志输出至nfs文件服务器,nfs文件服务器使用autofs自动挂载,nginx和autofs都使用systemctl设置了开机自启动. 但是在重启主机的时候nginx ...

  6. iOS底层框架浅析

    1.简介 IOS是由苹果公司为iPhone.iPod touch和iPad等设备开发的操作系统. 2.知识点 iPhone OS(现在叫iOS)是iPhone, iPod touch 和 iPad 设 ...

  7. 什么时候该用readfile() , fread(), file_get_contents(), fgets()?

    fread() 和 readfile() fread() 最大一次性能读取 8k长度的字节数,所以不能一次性读取大文件去作下载. 优势在于,操作更加灵活,每次读取指定字节的内容,用于下载时方便控制服务 ...

  8. 上传大文件到腾讯云cos遇到的一些问题

    讲一个开发遇到的问题. 开发中遇到一个需求,需要在后台表单页面支持上传视频.因为项目中一直用的是腾讯云的COS做第三方存储平台,所以视频也要上传到cos中保存.首先想到的是使用腾讯提供的php的SDK ...

  9. 页面数据加载完成时,显示loading页面.数据加载完,loading隐藏.

    一,引入三个文件 jQuery版本使用 jQuery v1.7.1 jquery-easyui文件中,引入easyui-lang-zh_CN.js的js 做数据加载时使用jquery.blockui. ...

  10. [转帖]QC 和 PD:关于你所不知道的快充

    QC 和 PD:关于你所不知道的快充 http://www.sohu.com/a/276214250_465976 2018-11-18 06:02 当我们使用支持 PD 或者 QC 快充协议的电源适 ...