第六章 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 ...
随机推荐
- fiddler4自动生成jmeter脚本
接口.性能测试任务当遇到从浏览器或移动app自己抓包的情况出现时就变得巨苦逼了,苦在哪里?苦在需要通过抓包工具抓报文,需要通过抓包报文梳理业务逻辑.需要将梳理的逻辑编写成脚本.最最苦的情况是,自己抓包 ...
- springboot jpa junit测试遇到的问题
jpa在插入数据的时候,插入的对象变量user中不能包含变量,需要时确切的值,否则会出现sql解析报错 解析报错如下图
- GoAccess安装及分析nginx实时日志
GoAccess是一个基于终端的快速日志分析器.其核心思想是实时快速分析和查看Web服务器统计信息,而无需使用您的浏览器(如果您希望通过SSH快速分析访问日志,或者只是喜欢在终端中工作),终端输出是默 ...
- noi.ac NA529 【神树的矩阵】
表示今天一发A了这题拿了rk3...是个sb构造... 考虑除了\(n=1/m=1\)的情况,最小次数\(ans\)不会\(>3\). 对于\(n=1/m=1\),暴力即可. 然后考虑\(ans ...
- 【BZOJ2752】【Luogu P2221】 [HAOI2012]高速公路
不是很难的一个题目.正确思路是统计每一条边被经过的次数,但我最初由于习惯直接先上了一个前缀和再推的式子,导致极其麻烦难以写对而且会爆\(longlong\). 推导过程请看这里. #include & ...
- radio(单选框)/checkbox(复选框) 美化
由于某种原因,可能需要对单选框(radio)或复选框(checkbox)进行美化,那么直接修改样式是行不通,要实现就需要添加js,以下js依赖于jquery radio.js: function ra ...
- java邮箱正则验证
import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class tes ...
- Struts2基本原理【转】
阐述struts2的执行流程. Struts 2框架本身大致可以分为3个部分:核心控制器FilterDispatcher.业务控制器Action和用户实现的企业业务逻辑组件. 核心控制器FilterD ...
- LINUX笔记之二常用命令(权限管理命令)
1. +增加权限:-去掉权限:=直接赋权. r(4)w(2)x(1) 重点掌握:通过数字授权,例如rxwr-xr--是754 例题:用root用户创建目录并在此目录新建文件,之后更改新文件的权限为77 ...
- BeanUtils对象属性copy的性能对比以及源码分析
1. 对象属性拷贝的常见方式及其性能 在日常编码中,经常会遇到DO.DTO对象之间的转换,如果对象本身的属性比较少的时候,那么我们采用硬编码手工setter也还ok,但如果对象的属性比较多的情况下,手 ...