Ribbon负载均衡及Feign消费者调用服务

文章目录

    • 1、ribbon集成eureka完成服务调用
      • 2、ribbon负载均衡

      • 3、feign集成ribbon调用

1、ribbon集成eureka完成服务调用

Ribbon是什么?
Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中列出Load Balancer(简称LB)后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随即连接等)去连接这些机器。我们也很容易使用Ribbon实现自定义的负载均衡算法。
在这里插入图片描述
Ribbon是客户端负载均衡,所以肯定集成再消费端,也就是consumer端
我们修改microservice-student-consumer-80
首先,引入依赖,pom.xml 加入 ribbon相关依赖

1<!--ribbon相关依赖--> 2<dependency> 3 <groupId>org.springframework.cloud</groupId> 4 <artifactId>spring-cloud-starter-eureka</artifactId> 5</dependency> 6<dependency> 7 <groupId>org.springframework.cloud</groupId> 8 <artifactId>spring-cloud-starter-ribbon</artifactId> 9</dependency> 10<dependency> 11 <groupId>org.springframework.cloud</groupId> 12 <artifactId>spring-cloud-starter-config</artifactId> 13</dependency> 14 15

修改yml文件

1server: 2 context-path: / 3 port: 80 4eureka: 5 client: 6 service-url: 7 defaultZone: http://eureka2001.zrh.com:2001/eureka/,http://eureka2002.zrh.com:2002/eureka/,http://eureka2003.zrh.com:2003/eureka/ 8 register-with-eureka: false 9 10

SpringCloudConfig也改成 要加个负载均衡配置 @LoadBalanced

1package com.zrh.microservicestudentconsumer80.config; 2 3import org.springframework.cloud.client.loadbalancer.LoadBalanced; 4import org.springframework.context.annotation.Bean; 5import org.springframework.context.annotation.Configuration; 6import org.springframework.web.client.RestTemplate; 7 8 9@Configuration 10public class SpringCloudConfig { 11 @LoadBalanced // 引入ribbon负载均衡 12 @Bean 13 public RestTemplate getRestTemplate(){ 14 return new RestTemplate(); 15 } 16} 17 18 19

因为和eureka整合,所以启动类StudentConsumerApplication_80 加个注解 @EnableEurekaClient
在这里插入图片描述
这里还有一个,要修改下StudentConsumerController的PRE_HOST,改成指定的微服务应用名称;
在这里插入图片描述
我们的微服务应用名称是 microservice-student
所以服务调用者这边的控制器里PRE_HOST改成 http://MICROSERVICE-STUDENT即可;

注意: 在这之前记得去yml文件加下配置

1application: 2 name: microservice-student 3 4

好了之后就测试一下
先启动三个eureka,然后再启动服务提供者,再启动服务消费者;
在这里插入图片描述
执行这个http://localhost:1001/student/list
在这里插入图片描述
这样就说明成功了

2、ribbon负载均衡

按照它microservice-student-provider-1001建立一个microservice-student-provider子项目,然后将microservice-student-provider-1001这个子项目干掉;

前面搭建了初步例子,但是还没实现真正负载均衡,我们这里要先搞三个服务提供者集群,然后才能演示负载均衡,以及负载均衡策略;

新建项目microservice-student-provider-1002,microservice-student-provider-1003
pom.xml,application.yml,以及java类都复制一份,启动类名称对应的改下;
yml配置文件有两处要对应的改下,port端口改下,以及服务实例名称改下;

相关代码如下
pom依赖:

1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>com.zrh</groupId> 7 <artifactId>springcloud</artifactId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 11 <artifactId>microservice-student-provider</artifactId> 12 13 14 <dependencies> 15 <dependency> 16 <groupId>com.zrh</groupId> 17 <artifactId>microservice-common</artifactId> 18 </dependency> 19 <dependency> 20 <groupId>org.springframework.boot</groupId> 21 <artifactId>spring-boot-starter-web</artifactId> 22 </dependency> 23 <dependency> 24 <groupId>org.springframework.boot</groupId> 25 <artifactId>spring-boot-starter-test</artifactId> 26 <scope>test</scope> 27 </dependency> 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-data-jpa</artifactId> 31 </dependency> 32 <dependency> 33 <groupId>mysql</groupId> 34 <artifactId>mysql-connector-java</artifactId> 35 </dependency> 36 <dependency> 37 <groupId>org.springframework.boot</groupId> 38 <artifactId>spring-boot-starter-tomcat</artifactId> 39 </dependency> 40 <dependency> 41 <groupId>com.alibaba</groupId> 42 <artifactId>druid-spring-boot-starter</artifactId> 43 </dependency> 44 <!-- 修改后立即生效,热部署 --> 45 <dependency> 46 <groupId>org.springframework</groupId> 47 <artifactId>springloaded</artifactId> 48 </dependency> 49 <dependency> 50 <groupId>org.springframework.boot</groupId> 51 <artifactId>spring-boot-devtools</artifactId> 52 </dependency> 53 <dependency> 54 <groupId>com.zrh</groupId> 55 <artifactId>microservice-common</artifactId> 56 <version>1.0-SNAPSHOT</version> 57 <scope>compile</scope> 58 </dependency> 59 <!--添加注册中心Eureka相关配置--> 60 <dependency> 61 <groupId>org.springframework.cloud</groupId> 62 <artifactId>spring-cloud-starter-eureka</artifactId> 63 </dependency> 64 <dependency> 65 <groupId>org.springframework.cloud</groupId> 66 <artifactId>spring-cloud-starter-config</artifactId> 67 </dependency> 68 <!--&lt;!&ndash; actuator监控引入 &ndash;&gt;--> 69 <dependency> 70 <groupId>org.springframework.boot</groupId> 71 <artifactId>spring-boot-starter-actuator</artifactId> 72 </dependency> 73 74 </dependencies> 75 <build> 76 <plugins> 77 <plugin> 78 <groupId>org.springframework.boot</groupId> 79 <artifactId>spring-boot-maven-plugin</artifactId> 80 </plugin> 81 </plugins> 82 </build> 83 84</project> 85 86 87

yml文件

1--- 2server: 3 port: 1001 4 context-path: / 5spring: 6 datasource: 7 type: com.alibaba.druid.pool.DruidDataSource 8 driver-class-name: com.mysql.jdbc.Driver 9 url: jdbc:mysql://localhost:3306/zrh?useUnicode=true&characterEncoding=utf8 10 username: root 11 password: 123 12 jpa: 13 hibernate: 14 ddl-auto: update 15 show-sql: true 16 application: 17 name: microservice-student 18 profiles: provider-1001 19 20eureka: 21 instance: 22 hostname: localhost 23 appname: microservice-student 24 instance-id: microservice-student:1001 25 prefer-ip-address: true 26 client: 27 service-url: 28 defaultZone: http://eureka2001.zrh.com:2001/eureka/,http://eureka2002.zrh.com:2002/eureka/,http://eureka2003.zrh.com:2003/eureka/ 29 30info: 31 groupId: com.zrh.springcloud 32 artifactId: microservice-student-provider-1001 33 version: 1.0-SNAPSHOT 34 userName: http://zrh.com 35 phone: 123456 36 37--- 38server: 39 port: 1002 40 context-path: / 41spring: 42 datasource: 43 type: com.alibaba.druid.pool.DruidDataSource 44 driver-class-name: com.mysql.jdbc.Driver 45 url: jdbc:mysql://localhost:3306/zrh?useUnicode=true&characterEncoding=utf8 46 username: root 47 password: 123 48 jpa: 49 hibernate: 50 ddl-auto: update 51 show-sql: true 52 application: 53 name: microservice-student 54 profiles: provider-1002 55 56eureka: 57 instance: 58 hostname: localhost 59 appname: microservice-student 60 instance-id: microservice-student:1002 61 prefer-ip-address: true 62 client: 63 service-url: 64 defaultZone: http://eureka2001.zrh.com:2001/eureka/,http://eureka2002.zrh.com:2002/eureka/,http://eureka2003.zrh.com:2003/eureka/ 65 66info: 67 groupId: com.zrh.springcloud 68 artifactId: microservice-student-provider-1002 69 version: 1.0-SNAPSHOT 70 userName: http://zrh.com 71 phone: 123456 72 73--- 74server: 75 port: 1003 76 context-path: / 77spring: 78 datasource: 79 type: com.alibaba.druid.pool.DruidDataSource 80 driver-class-name: com.mysql.jdbc.Driver 81 url: jdbc:mysql://localhost:3306/zrh?useUnicode=true&characterEncoding=utf8 82 username: root 83 password: 123 84 jpa: 85 hibernate: 86 ddl-auto: update 87 show-sql: true 88 application: 89 name: microservice-student 90 profiles: provider-1003 91 92eureka: 93 instance: 94 hostname: localhost 95 appname: microservice-student 96 instance-id: microservice-student:1003 97 prefer-ip-address: true 98 client: 99 service-url: 100 defaultZone: http://eureka2001.zrh.com:2001/eureka/,http://eureka2002.zrh.com:2002/eureka/,http://eureka2003.zrh.com:2003/eureka/ 101 102info: 103 groupId: com.zrh.springcloud 104 artifactId: microservice-student-provider-1003 105 version: 1.0-SNAPSHOT 106 userName: http://zrh.com 107 phone: 123456 108 109 110 111

在这里插入图片描述

3、feign集成ribbon调用

Feign是一个声明式的Web Service客户端,它使得编写Web Serivce客户端变得更加简单。我们只需要使用Feign来创建一个接口并用注解来配置它既可完成。它具备可插拔的注解支持,包括Feign注解和JAX-RS注解。Feign也支持可插拔的编码器和解码器。Spring Cloud为Feign增加了对Spring MVC注解的支持,还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。

应用
1、在common项目里建一个service(实际项目肯定是多个service)作为Feign客户端,用Feign客户端来调用服务器提供者,当然可以配置负载均衡;Feign客户端定义的目的,就是为了方便给其他项目调用;
修改 microservice-common
pom.xml引入Feign依赖:

1<!--引入Feign依赖--> 2<dependency> 3 <groupId>org.springframework.cloud</groupId> 4 <artifactId>spring-cloud-starter-feign</artifactId> 5</dependency> 6 7

建StudentClientService接口;

1package com.zrh.microservicecommon.service; 2 3 4import com.zrh.microservicecommon.entity.Student; 5import org.springframework.cloud.netflix.feign.FeignClient; 6import org.springframework.web.bind.annotation.GetMapping; 7import org.springframework.web.bind.annotation.PathVariable; 8import org.springframework.web.bind.annotation.PostMapping; 9import org.springframework.web.bind.annotation.RequestMapping; 10 11import java.util.List; 12 13/** 14 * Student Feign接口客户端 15 * @author Administrator 16 * 17 */ 18@FeignClient(value="MICROSERVICE-STUDENT") 19public interface StudentClientService { 20 21 /** 22 * 根据id查询学生信息 23 * @param id 24 * @return 25 */ 26 @GetMapping(value="/student/get/{id}") 27 public Student get(@PathVariable("id") Integer id); 28 29 /** 30 * 查询学生信息 31 * @return 32 */ 33 @GetMapping(value="/student/list") 34 public List<Student> list(); 35 36 /** 37 * 添加或者修改学生信息 38 * @param student 39 * @return 40 */ 41 @PostMapping(value="/student/save") 42 public boolean save(Student student); 43 44 /** 45 * 根据id删除学生信息 46 * @return 47 */ 48 @GetMapping(value="/student/delete/{id}") 49 public boolean delete(@PathVariable("id") Integer id); 50 51 @RequestMapping("/student/ribbon") 52 public String ribbon(); 53} 54 55 56

2、新建一个Feign消费者项目;
参考microservice-student-consumer-80建一个microservice-student-consumer-feign-80
代码都复制一份,包括pom.xml

1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>com.zrh</groupId> 7 <artifactId>springcloud</artifactId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 11 <artifactId>microservice-student-consumer-feign-80</artifactId> 12 13 <properties> 14 <java.version>1.8</java.version> 15 </properties> 16 17 <dependencies> 18 <dependency> 19 <groupId>org.springframework.boot</groupId> 20 <artifactId>spring-boot-starter</artifactId> 21 </dependency> 22 23 <dependency> 24 <groupId>org.springframework.boot</groupId> 25 <artifactId>spring-boot-starter-test</artifactId> 26 <scope>test</scope> 27 <exclusions> 28 <exclusion> 29 <groupId>org.junit.vintage</groupId> 30 <artifactId>junit-vintage-engine</artifactId> 31 </exclusion> 32 </exclusions> 33 </dependency> 34 <dependency> 35 <groupId>org.springframework.cloud</groupId> 36 <artifactId>spring-cloud-commons</artifactId> 37 <version>1.3.4.RELEASE</version> 38 <scope>compile</scope> 39 </dependency> 40 <dependency> 41 <groupId>org.springframework</groupId> 42 <artifactId>spring-web</artifactId> 43 <version>5.2.1.RELEASE</version> 44 <scope>compile</scope> 45 </dependency> 46 <dependency> 47 <groupId>org.springframework</groupId> 48 <artifactId>spring-web</artifactId> 49 <version>5.2.1.RELEASE</version> 50 <scope>compile</scope> 51 </dependency> 52 <dependency> 53 <groupId>com.netflix.ribbon</groupId> 54 <artifactId>ribbon-loadbalancer</artifactId> 55 <version>2.2.5</version> 56 <scope>compile</scope> 57 </dependency> 58 <dependency> 59 <groupId>org.springframework.cloud</groupId> 60 <artifactId>spring-cloud-netflix-core</artifactId> 61 <version>1.4.5.RELEASE</version> 62 <scope>compile</scope> 63 </dependency> 64 <dependency> 65 <groupId>org.springframework.cloud</groupId> 66 <artifactId>spring-cloud-netflix-eureka-client</artifactId> 67 <version>1.4.5.RELEASE</version> 68 <scope>compile</scope> 69 </dependency> 70 </dependencies> 71 72 <build> 73 <plugins> 74 <plugin> 75 <groupId>org.springframework.boot</groupId> 76 <artifactId>spring-boot-maven-plugin</artifactId> 77 </plugin> 78 </plugins> 79 </build> 80 81</project> 82 83 84

SpringCloudConfig.java

1package com.zrh.microservicestudentconsumerfeign80.config; 2 3 4import com.netflix.loadbalancer.IRule; 5import com.netflix.loadbalancer.RetryRule; 6import org.springframework.cloud.client.loadbalancer.LoadBalanced; 7import org.springframework.context.annotation.Bean; 8import org.springframework.context.annotation.Configuration; 9import org.springframework.web.client.RestTemplate; 10 11 12@Configuration 13public class SpringCloudConfig { 14 15 @LoadBalanced // 引入ribbon负载均衡 16 @Bean 17 public RestTemplate getRestTemplate() { 18 return new RestTemplate(); 19 } 20 21 /** 22 * 自定义调用规则(服务提供者掉线后不再调用,解决轮询问题) 23 * @return 24 */ 25 @Bean 26 public IRule myRule(){ 27 return new RetryRule(); 28 } 29} 30 31 32

在启动类加上注释

MicroserviceStudentConsumerFeign80Application

1package com.zrh.microservicestudentconsumerfeign80; 2 3import org.springframework.boot.SpringApplication; 4import org.springframework.boot.autoconfigure.SpringBootApplication; 5import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 6import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; 7import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 8import org.springframework.cloud.netflix.feign.EnableFeignClients; 9 10@EnableEurekaClient 11@EnableFeignClients(value = "com.zrh.*.*") 12@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) 13public class MicroserviceStudentConsumerFeign80Application { 14 15 public static void main(String[] args) { 16 SpringApplication.run(MicroserviceStudentConsumerFeign80Application.class, args); 17 } 18 19} 20 21 22

StudentConsumerController.java

1package com.zrh.microservicestudentconsumerfeign80.conterller; 2 3 4import com.zrh.microservicecommon.entity.Student; 5import com.zrh.microservicecommon.service.StudentClientService; 6import org.springframework.beans.factory.annotation.Autowired; 7import org.springframework.web.bind.annotation.*; 8import org.springframework.web.client.RestTemplate; 9 10import java.util.List; 11 12@RestController 13@RequestMapping("/student") 14public class StudentConsumerController { 15 16 @Autowired 17 private StudentClientService studentClientService; 18 19 @Autowired 20 private RestTemplate restTemplate; 21 22 @PostMapping(value = "/save") 23 private boolean save(Student student) { 24 return studentClientService.save(student); 25 } 26 27 @GetMapping(value = "/list") 28 public List<Student> list() { 29 return studentClientService.list(); 30 } 31 32 @GetMapping(value = "/get/{id}") 33 public Student get(@PathVariable("id") Integer id) { 34 return studentClientService.get(id); 35 } 36 37 @GetMapping(value = "/delete/{id}") 38 public boolean delete(@PathVariable("id") Integer id) { 39 try { 40 studentClientService.delete(id); 41 return true; 42 } catch (Exception e) { 43 return false; 44 } 45 } 46 47 @RequestMapping("/ribbon") 48 public String ribbon(){ 49 return studentClientService.ribbon(); 50 } 51} 52 53 54

因为现在用Fiegn,所以把restTemplate去掉,改成注入service,调用service方法来实现服务的调用;
在这里插入图片描述

在这里插入图片描述

代码交流 2021