Ribbon负载均衡及Feign消费者调用服务
微服务调用Ribbon
简介
前面讲了eureka服务注册与发现,但是结合eureka集群的服务调用没讲。
这里的话 就要用到Ribbon,结合eureka,来实现服务的调用;
Ribbon是Netflix发布的负载均衡器,它有助于控制HTTP和TCP的客户端的行为。为Ribbon配置服务提供者地址后,Ribbon就可基于某种负载均衡算法,自动地帮助服务消费者去请求。Ribbon默认为我们提供了很多负载均衡算法,例如轮询、随机等。当然,我们也可为Ribbon实现自定义的负载均衡算法。
在Spring Cloud中,当Ribbon与Eureka配合使用时,Ribbon可自动从Eureka Server获取服务提供者地址列表,并基于负载均衡算法,请求其中一个服务提供者实例。展示了Ribbon与Eureka配合使用时的架构。
Ribbon是客户端负载均衡,所以肯定集成再消费端,也就是consumer端
我们修改microservice-student-consumer-80
首先,引入依赖,pom.xml 加入 ribbon相关依赖
<!--ribbon相关依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-ribbon</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency>
application.yml加
server:port: 80context-path: /eureka:client:service-url:defaultZone: :2001/eureka/,:2002/eureka/,:/eureka/register-with-eureka: false
ribbon结合eureka来调用服务提供者;
SpringCloudConfig也改成 要加个负载均衡配置 @LoadBalanced
package com.xzy.microservicestudentconsumer80.config;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.client.RestTemplate;@Configurationpublic class SpringCloudConfig {@LoadBalanced //引入ribbon负载均衡@Beanpublic RestTemplate getRestTemplate(){return new RestTemplate();}}
因为和eureka整合,所以启动类StudentConsumerApplication_80 加个注解@EnableEurekaClient
package com.xzy.microservicestudentconsumer80;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;import org.flix.eureka.EnableEurekaClient;@EnableEurekaClient@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})public class MicroserviceStudentConsumer80Application {public static void main(String[] args) {SpringApplication.run(MicroserviceStudentConsumer80Application.class, args);}}
这里还有一个,要修改下StudentConsumerController的PRE_HOST,改成指定的微服务应用名称;
当然这里要先在服务提供者microservice-student-provider-1001的application.yml加下配置,指定下应用名称:
application:
name: microservice-student
我们的微服务应用名称是 microservice-student
所以服务调用者这边的控制器里PRE_HOST改成 http://MICROSERVICE-STUDENT即可;
MICROSERVICE-STUDENT为Eureka注册中心的应用名称
private final static String SERVER_IP_PORT = "http://MICROSERVICE-STUDENT";
上面配置好后,我们可以测试下;
先启动三个eureka,然后再启动服务提供者,再启动服务消费者;
添加链接描述
结果就出来了,说明配置OK;
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.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.xzy</groupId><artifactId>springcloud</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>microservice-student-provider</artifactId><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>com.xzy</groupId><artifactId>microservice-common</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId></dependency><!-- 修改后立即生效,热部署 --><dependency><groupId>org.springframework</groupId><artifactId>springloaded</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency><dependency><groupId>com.xzy</groupId><artifactId>microservice-common</artifactId><version>1.0-SNAPSHOT</version><scope>compile</scope></dependency><!--添加注册中心Eureka相关配置--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><!-- actuator监控引入 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
yml文件
---server:port: 1001context-path: /spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf8username: rootpassword: 123jpa:hibernate:ddl-auto: updateshow-sql: trueapplication:name: microservice-studentprofiles: provider-1001eureka:instance:hostname: localhostappname: microservice-studentinstance-id: microservice-student:1001prefer-ip-address: trueclient:service-url:defaultZone: :2001/eureka/,:2002/eureka/,:/eureka/info:groupId: com.xzy.springcloudartifactId: microservice-student-provider-1001version: 1.0-SNAPSHOTuserName: phone: 7758258---server:port: 1002context-path: /spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf8username: rootpassword: 123jpa:hibernate:ddl-auto: updateshow-sql: trueapplication:name: microservice-studentprofiles: provider-1002eureka:instance:hostname: localhostappname: microservice-studentinstance-id: microservice-student:1002prefer-ip-address: trueclient:service-url:defaultZone: :2001/eureka/,:2002/eureka/,:/eureka/info:groupId: com.xzy.springcloudartifactId: microservice-student-provider-1002version: 1.0-SNAPSHOTuserName: phone: 7758258---server:port: 1003context-path: /spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf8username: rootpassword: 123jpa:hibernate:ddl-auto: updateshow-sql: trueapplication:name: microservice-studentprofiles: provider-1003eureka:instance:hostname: localhostappname: microservice-studentinstance-id: microservice-student:1003prefer-ip-address: trueclient:service-url:defaultZone: :2001/eureka/,:2002/eureka/,:/eureka/info:groupId: com.xzy.springcloudartifactId: microservice-student-provider-1003version: 1.0-SNAPSHOTuserName: phone: 7758258
启动类
package com.xzy.microservicestudentprovider;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.domain.EntityScan;import org.flix.eureka.EnableEurekaClient;@EntityScan("com.xzy.*.*")@EnableEurekaClient@SpringBootApplicationpublic class MicroserviceStudentProviderApplication {public static void main(String[] args) {SpringApplication.run(MicroserviceStudentProviderApplication.class, args);}}
StudentProviderController.java
生产者microservice-student-providerd controller中加:
@Value("${server.port}")private String port;@RequestMapping("/ribbon")public String ribbon(){return "工号【"+port+"】正在为您服务";}
在消费者microservice-student-provider-80的controller中加:
StudentConsumerController.java
@RequestMapping("/ribbon")public String ribbon(){return restTemplate.getForObject(SERVER_IP_PORT + "/student/ribbon", String.class);}
先测试服务提供者:
http://localhost:1001/student/list
http://localhost:1002/student/list
http://localhost:1003/student/list
看看是否有结果;
再测试下 eureka:
:2001/
:2002/
:/
有这种的话,就说明没问题;
我们在请求页面每刷新一次就会刷新一次端口:
但是这种默认的轮询策略肯定是不能满足实际需求的,比如有3个服务提供者,突然挂了一个,这样的话,默认轮询 ,总有1/3的概率访问失败; 所以我们看下ribbon默认给我们提供的策略有哪些;
/*** 自定义轮询算法* @return*/@Beanpublic IRule myRule(){return new RetryRule();}
Feign简介及应用
简介
声明式服务调用Feign简单介绍下;
Feign是一个声明式的Web Service客户端,它使得编写Web Serivce客户端变得更加简单。我们只需要使用Feign来创建一个接口并用注解来配置它既可完成。它具备可插拔的注解支持,包括Feign注解和JAX-RS注解。Feign也支持可插拔的编码器和解码器。Spring Cloud为Feign增加了对Spring MVC注解的支持,还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。
这段话看起来比较懵逼,这里说下实际使用,前面Ribbon调用服务提供者,我们通过restTemplate调用,缺点是,多个地方调用,同一个请求要写多次,不方便统一维护,这时候Feign来了,就直接把请求统一搞一个service作为FeignClient,然后其他调用Controller需要用到的,直接注入service,直接调用service方法即可;同时Feign整合了Ribbon和Eureka,所以要配置负载均衡的话,直接配置Ribbon即可,无其他特殊地方;当然Fiegn也整合了服务容错保护,断路器Hystrix,后面再说。
应用
1、在common项目里建一个service(实际项目肯定是多个service)作为Feign客户端,用Feign客户端来调用服务器提供者,当然可以配置负载均衡;Feign客户端定义的目的,就是为了方便给其他项目调用;
修改 microservice-common
pom.xml引入Feign依赖:
<!--引入Feign依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId></dependency>
建StudentClientService接口:
package com.xzy.microservicecommon.service;import com.xzy.microservicecommon.entity.Student;import org.flix.feign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import java.util.List;/*** Student Feign接口客户端* @author Administrator**/@FeignClient(value="MICROSERVICE-STUDENT")public interface StudentClientService {/*** 根据id查询学生信息* @param id* @return*/@GetMapping(value="/student/get/{id}")public Student get(@PathVariable("id") Integer id);/*** 查询学生信息* @return*/@GetMapping(value="/student/list")public List<Student> list();/*** 添加或者修改学生信息* @param student* @return*/@PostMapping(value="/student/save")public boolean save(Student student);/*** 根据id删除学生信息* @return*/@GetMapping(value="/student/delete/{id}")public boolean delete(@PathVariable("id") Integer id);@RequestMapping("/student/ribbon")public String ribbon();}
新建一个Feign消费者项目:
参考microservice-student-consumer-80建一个microservice-student-consumer-feign-80
代码都复制一份,包括pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.xzy</groupId><artifactId>springcloud</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>microservice-student-consumer-feign-80</artifactId><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>com.xzy</groupId><artifactId>microservice-common</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></dependency><!-- 修改后立即生效,热部署 --><dependency><groupId>org.springframework</groupId><artifactId>springloaded</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency><!--ribbon相关依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-ribbon</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
SpringCloudConfig.java
package com.xzy.microservicestudentconsumerfeign80.config;import flix.loadbalancer.IRule;import flix.loadbalancer.RetryRule;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.client.RestTemplate;@Configurationpublic class SpringCloudConfig {@LoadBalanced // 引入ribbon负载均衡@Beanpublic RestTemplate getRestTemplate() {return new RestTemplate();}/*** 自定义调用规则(服务提供者掉线后不再调用,解决轮询问题)* @return*/@Beanpublic IRule myRule(){return new RetryRule();// return new RandomRule();}}
yml文件
server:port: 80context-path: /eureka:client:service-url:defaultZone: :2001/eureka/,:2002/eureka/,:/eureka/register-with-eureka: false
修改启动类名称,和加注解
启动类名称改下,改成StudentConsumerFeignApplication_80,同时加个注解@EnableFeignClients
package com.xzy.microservicestudentconsumerfeign80;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;import org.flix.eureka.EnableEurekaClient;import org.flix.feign.EnableFeignClients;@EnableEurekaClient@EnableFeignClients(value = "com.xzy.*.*")@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})public class MicroserviceStudentConsumerFeign80Application {public static void main(String[] args) {SpringApplication.run(MicroserviceStudentConsumerFeign80Application.class, args);}}
StudentConsumerController.java
package com.xzy.microservicestudentconsumerfeign80.controller;import com.xzy.microservicecommon.entity.Student;import com.xzy.microservicecommon.service.StudentClientService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import org.springframework.web.client.RestTemplate;import java.util.List;@RestController@RequestMapping("/student")public class StudentConsumerController {@Autowiredprivate StudentClientService studentClientService;@Autowiredprivate RestTemplate restTemplate;@PostMapping(value = "/save")private boolean save(Student student) {return studentClientService.save(student);}@GetMapping(value = "/list")public List<Student> list() {return studentClientService.list();}@GetMapping(value = "/get/{id}")public Student get(@PathVariable("id") Integer id) {return studentClientService.get(id);}@GetMapping(value = "/delete/{id}")public boolean delete(@PathVariable("id") Integer id) {try {studentClientService.delete(id);return true;} catch (Exception e) {return false;}}@RequestMapping("/ribbon")public String ribbon(){return studentClientService.ribbon();}}
因为现在用Fiegn,所以把restTemplate去掉,改成注入service,调用service方法来实现服务的调用;
如果觉得《Ribbon负载均衡及Feign消费者调用服务》对你有帮助,请点赞、收藏,并留下你的观点哦!