第六章 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>
2、application.yml
server:
port: 8665
user:
userServicePath: http://localhost:8663/user/ spring:
application:
name: eureka-client-ribbon
eureka:
instance:
hostname: localhost
prefer-ip-address: true
instance-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 {
@Autowired
private RestTemplate restTemplate; @Value("${user.userServicePath}")
private String userServicePath; /* @Autowired
private LoadBalancerClient loadBalancerClient;*/ @GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
//请用虚拟ip
return restTemplate.getForObject("http://eureka-client-user/user/"+id, User.class);
} /**
* 配置单一的方法进行轮询
* @return
*/
/* @GetMapping("/test")
public String test() {
//请用虚拟ip
ServiceInstance 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 {
/* @Autowired
private RestTemplate restTemplate;
*/
@Value("${user.userServicePath}")
private String userServicePath; @Autowired
private LoadBalancerClient loadBalancerClient; /* @GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
//请用虚拟ip
return restTemplate.getForObject("http://eureka-client-user/user/"+id, User.class);
}*/ /**
* 配置单一的方法进行轮询
* @return
*/
@GetMapping("/test")
public String test() {
//请用虚拟ip
ServiceInstance 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: localhost
prefer-ip-address: true
instance-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 {
@Bean
public 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);
} }
第六章 SpringCloud之Ribbon负载均衡的更多相关文章
- spring-cloud配置ribbon负载均衡
spring-cloud配置ribbon负载均衡 ribbon提供的负载均衡就是开箱即用的,简单的不能再简单了 为了顺利演示此demo,你需要如下 需要提前配置eureka服务端,具体看 https: ...
- SpringCloud系列——Ribbon 负载均衡
前言 Ribbon是一个客户端负载均衡器,它提供了对HTTP和TCP客户端的行为的大量控制.我们在上篇(猛戳:SpringCloud系列——Feign 服务调用)已经实现了多个服务之间的Feign调用 ...
- 浅谈SpringCloud (三) Ribbon负载均衡
什么是负载均衡 当一台服务器的单位时间内的访问量越大时,服务器压力就越大,大到超过自身承受能力时,服务器就会崩溃.为了避免服务器崩溃,让用户有更好的体验,我们通过负载均衡的方式来分担服务器压力. 我们 ...
- SpringCloud:Ribbon负载均衡
1.概述 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端 负载均衡的工具. 简单的说,Ribbon是Netflix发布的开源项目,主要功能是提供客 ...
- 四(2)、springcloud之Ribbon负载均衡
2.Ribbon负载均衡 Ribbon在工作时分成两步第一步先选择 EurekaServer ,它优先选择在同一个区域内负载较少的server. 第二步再根据用户指定的策略,在从server取到的 ...
- Spring-cloud之Ribbon负载均衡的使用及负载均衡策略配置(与Eurka配合使用)
什么是Ribbon,ribbon有什么用,个人先总结一下(不正确请提出讨论):Ribbon是基于客户端的负载均衡器,为我们提供了多样的负载均衡的方案,比如轮询,最小的并发请求的server,随机ser ...
- SpringCloud之Ribbon负载均衡及Feign消费者调用服务
目的: 微服务调用Ribbon Ribbon负载均衡 Feign简介及应用 微服务调用Ribbon Ribbon简介 1. 负载均衡框架,支持可插拔式的负载均衡规则 2. 支持多种协议,如HTTP.U ...
- SpringCloud之Ribbon负载均衡配置
一.负载均衡解决方案分类及特征 业界主流的负载均衡解决方案有: 1.1 集中式负载均衡 即在客户端和服务端之间使用独立的负载均衡设施(可以是硬件,如F5, 也可以是软件,如nginx), 由该设施负责 ...
- SpringCloud学习笔记(六):Feign+Ribbon负载均衡
简介 官网解释: http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feign Feign是一个声明式WebS ...
随机推荐
- C语言字符串操作小结
1)字符串操作strcpy(p, p1) 复制字符串strncpy(p, p1, n) 复制指定长度字符串strcat(p, p1) 附加字符串strncat(p, p1, n) 附加指定长度字符串s ...
- 标准C语言(10)
指针数组的每个存储区是一个指针类型的存储区,字符指针数组包含多个字符类型的指针,每个字符类型指针可以代表一个字符串.字符指针数组可以用来代表多个相关字符串,二维字符数组也可以用来记录多个相关字符串,通 ...
- php中限制ip段访问、禁止ip提交表单的代码
在需要禁止访问或提交表单的页面添加下面的代码进行判断就可以了. 注意:下边只是一个PHP限制IP的实例代码,如果您打算应用到CMS中,请自行修改. <?php /加IP访问限制 if(geten ...
- JavaScript 的 this 指向问题深度解析
与我们常见的很多语言不同,JavaScript 函数中的 this 指向并不是在函数定义的时候确定的,而是在调用的时候确定的.换句话说,函数的调用方式决定了 this 指向. JavaScript 中 ...
- ffmpeg函数02__swr_alloc_set_opts()
SwrContext *swr_alloc(void); // 分配重采样的上下文. SwrContext *swr_alloc_set_opts(struct SwrContext *s, int ...
- ORACLE纯SQL实现多行合并一行
项目中遇到一个需求,需要将多行合并为一行.表结构如下:NAME Null Type---------------------- ...
- centos swap分区
swap分区 通常memory是机器的物理内存,读写速度低于cpu一个量级,但是高于磁盘不止一个量级.所以,程序和数据如果在内存的话,会有非常快的读写速度.但是,内存的造价是要高于磁盘 ...
- 我所亲身经历的CMMI3 [问题点数:20分,结帖人outer2000]--转载
很荣幸,作为某公司软件部门的软件项目经理,亲身经历了CMMI3,以下就把整个改进过程,用自己的亲身体会,详述如下,文中一些观点与看法难免带有个人感情,还请各位酌情参考. 公司情况简单介绍下,因为是为某 ...
- shell知识点(二)
Shell 中的数组 Shell 数组用括号来表示,元素用"空格"符号分割开,语法格式如下: 方式2: arr=(value1 value2 value3) (这种方式带值) ...
- 自我介绍 & 友链
"Never say never until the very end." --Flere825 想了想其实没什么可介绍的--毕竟我这么菜也没多少人认识我qwq还是写一点吧. 某弱 ...