第六章 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); } }
本文来自博客园,作者:小白啊小白,Fighting,转载请注明原文链接:https://www.cnblogs.com/ywjfx/p/10549550.html