第六章 SpringCloud之Ribbon负载均衡
###################使用默认的负载均衡(轮询)#############################
1、pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.test</groupId><artifactId>eureka-client-ribbon</artifactId><version>0.0.1-SNAPSHOT</version><name>eureka-client-ribbon</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version><spring-cloud.version>Greenwich.SR1</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
View Code
2、application.yml
server:port: 8665 user:userServicePath: http://localhost:8663/user/spring:application:name: eureka-client-ribbon eureka:instance:hostname: localhostprefer-ip-address: trueinstance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}client:serviceUrl:defaultZone: http://${eureka.instance.hostname}:8661/eureka
3、对RestTemplate添加注解
package com.test.eurekaclientribbon;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate;@SpringBootApplication @EnableEurekaClient public class EurekaClientRibbonApplication {@Bean@LoadBalanced //使restTemplate具备负载均衡的作用public RestTemplate restTemplate(){return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(EurekaClientRibbonApplication.class, args);}}
4、使用restTemplate对象远程调用服务
package com.test.eurekaclientribbon.controller;import com.test.eurekaclientribbon.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate;/*** Ribbon使用步骤:* 全局配置* 1、添加依赖* 2、配置application.yml文件* 3、对启动类中的RestTemplate添加@LoadBalanced注解,使得RestTemplate对象具备负载均衡能力* 4、请求路径为:被调用方的地址,但必须将被调用方的ip地址改为virtualIp* 例如 restTemplate.getForObject("http://eureka-client-user/user/"+id, User.class);* 被调用方URL:http://192.168.137.1:8663/user/* 现为:http://eureka-client-user/user/*/ @RestController public class MovieController {@Autowiredprivate RestTemplate restTemplate;@Value("${user.userServicePath}")private String userServicePath;/* @Autowiredprivate LoadBalancerClient loadBalancerClient;*/@GetMapping("/movie/{id}")public User findById(@PathVariable Long id) {//请用虚拟ipreturn restTemplate.getForObject("http://eureka-client-user/user/"+id, User.class);}/*** 配置单一的方法进行轮询* @return*//* @GetMapping("/test")public String test() {//请用虚拟ipServiceInstance serviceInstance =loadBalancerClient.choose("eureka-client-user");System.out.println(serviceInstance.getHost()+":"+serviceInstance.getPort());return "1";}*/ }
5、访问
http://localhost:8661/ #查看eureka http://192.168.137.1:8663/user/2 #确保自己调用可以访问 http://192.168.137.1:8665/movie/4 #远程调用测试
###################配置对单一的方法进行轮询(使用loadBalancerClient)########################
1、针对第一个步骤中的(4),进行修改
package com.test.eurekaclientribbon.controller;import com.test.eurekaclientribbon.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate;/*** Ribbon使用步骤:* 全局配置* 1、添加依赖* 2、配置application.yml文件* 3、对启动类中的RestTemplate添加@LoadBalanced注解,使得RestTemplate对象具备负载均衡能力* 4、请求路径为:被调用方的地址,但必须将被调用方的ip地址改为virtualIp* 例如 restTemplate.getForObject("http://eureka-client-user/user/"+id, User.class);* 被调用方URL:http://192.168.137.1:8663/user/* 现为:http://eureka-client-user/user/*/ @RestController public class MovieController {/* @Autowiredprivate RestTemplate restTemplate; */@Value("${user.userServicePath}")private String userServicePath;@Autowiredprivate LoadBalancerClient loadBalancerClient;/* @GetMapping("/movie/{id}")public User findById(@PathVariable Long id) {//请用虚拟ipreturn restTemplate.getForObject("http://eureka-client-user/user/"+id, User.class);}*//*** 配置单一的方法进行轮询* @return*/@GetMapping("/test")public String test() {//请用虚拟ipServiceInstance serviceInstance =loadBalancerClient.choose("eureka-client-user");System.out.println(serviceInstance.getHost()+":"+serviceInstance.getPort());return "1";} }
###################全局修改负载均衡方式########################
1、针对(2)application.yml文件添加下面配置
server:port: 8665 user:userServicePath: http://localhost:8663/user/ spring:application:name: eureka-client-ribbon eureka:instance:hostname: localhostprefer-ip-address: trueinstance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}client:serviceUrl:defaultZone: http://${eureka.instance.hostname}:8661/eureka eureka-client-user: #远程服务虚拟主机名ribbon:NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #随机方式#可以通过IRule类查看负载均衡方式#RoundRobinRule 轮询#RandomRule 随机
###################编写Configuration文件指定负载均衡方式########################
1、创建一个配置类
package com.test.eurekaclientribbon.config; import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.RandomRule; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration public class RibbonConfig {@Beanpublic IRule ribbonRule() {return new RandomRule();} }
2、在启动类上添加注解
package com.test.eurekaclientribbon;import com.test.eurekaclientribbon.config.RibbonConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.ribbon.RibbonClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate;@SpringBootApplication @EnableEurekaClient @RibbonClient(name = "eureka-client-user",configuration = RibbonConfig.class) //表示eureka-client-user主机使用这个规则 public class EurekaClientRibbonApplication {@Bean@LoadBalanced //使restTemplate具备负载均衡的作用public RestTemplate restTemplate(){return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(EurekaClientRibbonApplication.class, args);}}
转载于:https://www.cnblogs.com/ywjfx/p/10549550.html
第六章 SpringCloud之Ribbon负载均衡相关推荐
- SpringCloud:Ribbon负载均衡(基本使用、 负载均衡、自定义配置、禁用 Eureka 实现 Ribbon 调用)
现在所有的服务已经通过了 Eureka 进行了注册,那么使用 Eureka 注册的目的是希望所有的服务都统一归属到 Eureka 之中进 行处理,但是现在的问题,所有的微服务汇集到了 Eureka 之 ...
- SpringCloud的Ribbon负载均衡
Spring Cloud Ribbon相关学习: 简介 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具. 简单的说,Ribbon是Netflix ...
- SpringCloud[04]Ribbon负载均衡服务调用
文章目录 Ribbon负载均衡服务调用 1. 概述 1. Ribbon是什么 2. Ribbon能做什么 2. Ribbon负载均衡演示 1. 架构说明 2. Ribbon的POM依赖 3. Rest ...
- 【SpringCloud】Ribbon:负载均衡
什么是Ribbon? 并且Eureka中已经集成了Ribbon,所以我们无需引入新的依赖 如果要引入也是可以以的,依赖如下 <dependency><groupId>org.s ...
- 【SpringCloud】Ribbon 负载均衡
文章目录 1.概述 1.1 Ribbon在工作时分成两步 1.2 引入 1.2 替换方式 2.负载算法 2.1 IRule 接口 2.2 拓扑图 3. 负载均衡算法 4.RoundRobinRule源 ...
- SpringCloud源码:Ribbon负载均衡分析
本文主要分析 SpringCloud 中 Ribbon 负载均衡流程和原理. SpringCloud版本为:Edgware.RELEASE. 一.时序图 和以前一样,先把图贴出来,直观一点: 二.源码 ...
- JavaEE进阶知识学习-----SpringCloud(六)Ribbon负载均衡
Ribbon负载均衡 Ribbon概述 Spring Cloude Ribbon是基于Netfilx Ribbon实现的一套客户端 负载均衡的工具,简单说,Ribbon是Netfilix发布的开源项目 ...
- SpringCloud系列五:Ribbon 负载均衡(Ribbon 基本使用、Ribbon 负载均衡、自定义 Ribbon 配置、禁用 Eureka 实现 Ribbon 调用)...
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:Ribbon 负载均衡 2.具体内容 现在所有的服务已经通过了 Eureka 进行了注册,那么使用 Eureka 注册 ...
- 四、SpringCloud——Ribbon负载均衡Ribbon自定义
1 Ribbon简介 1)是什么 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具. 简单的说,Ribbon是Netflix发布的开源项目,主要功 ...
最新文章
- wxWidgets刚開始学习的人导引(3)——wxWidgets应用程序初体验
- netstat和lsof端口结果不一致
- pixhawk px4 commander.cpp
- dedecms首页怎么调用公司简介的内容
- 电脑休眠和睡眠的区别_关机、睡眠、休眠有啥区别?微软说非特殊情况不要关机...
- Debian下IPv6设定主地址 Set primary IPv6 address under Debian Linux
- row_number() over()排序功能
- Struts2与Ajax数据交互
- 个人计算机与工作站 服务器有什么区别,工作站与服务器有什么区别?
- 起始方位角怎么确定_起始方位角.PPT
- android 内存至少一半,极客修:为什么iPhone比安卓内存小,却更流畅?
- Lasso回归理论及代码实现
- Windows系统简体中文版官方镜像大全
- 超分辨率——综述文章
- 旧版OpenGL 与 新版OpenGL
- C++语言程序设计第四版郑莉
- R语言多层桑基图_绘制炫酷桑基图,R语言(networkD3包)能搞定?
- excel 的操作 http://wwwb.pconline.com.cn/pcedu/soft/excel.html
- 一张图介绍mysql执行过程
- 一、A/B升级之系统image的生成