失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 微服务调用Ribbon负载均衡 Feign的使用

微服务调用Ribbon负载均衡 Feign的使用

时间:2024-07-09 18:06:37

相关推荐

微服务调用Ribbon负载均衡 Feign的使用

目录:

Ribbon负载均衡

Feign简介及应用

Ribbon负载均衡

Ribbon是Netflix发布的负载均衡器,它有助于控制HTTP和TCP的客户端的行为。为Ribbon配置服务提供者地址后,Ribbon就可基于某种负载均衡算法,自动地帮助服务消费者去请求。Ribbon默认为我们提供了很多负载均衡算法,例如轮询、随机等。当然,我们也可为Ribbon实现自定义的负载均衡算法。

在Spring Cloud中,当Ribbon与Eureka配合使用时,Ribbon可自动从Eureka Server获取服务提供者地址列表,并基于负载均衡算法,请求其中一个服务提供者实例。展示了Ribbon与Eureka配合使用时的架构。

Ribbon是客户端负载均衡,所以肯定集成再消费端,也就是consumer端

所以我们先要修改microservice-student-consumer-80

pom依赖

<!--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中加上Eureka的配置

server:port: 80context-path: /eureka:client:service-url:defaultZone: :2001/eureka/,:2002/eureka/,:/eureka/register-with-eureka: false

然后在SpringCloudConfig配置类中加上注解

package com.liuxia.microservicestudentconsumer80.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();}}

这是和Eureka整合,所以在启动类还需要加上一个注解

@EnableEurekaClient

然后我们再把controller里面的请求地址改成Eureka注册中心的名称就可以了

private final static String SERVER_IP_PORT = "http://MICROSERVICE-STUDENT";

然后正常访问地址localhost/student/list一样能访问到数据加上成功了

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

然后我们再新建项目microservice-student-provider-1002,microservice-student-provider-1003,pom和其他文件都一样,只要改不同的端口即可

我这里为了方便,直接才用了一个项目复制2个启动文件就好了,主要再yml文件中配置好

---server:port: 1001context-path: /spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/t226_layui?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.liuxia.testSpringcloudartifactId: microservice-student-provider-1001version: 1.0-SNAPSHOTuserName: phone: 123456---server:port: 1002context-path: /spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/t226_layui?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.liuxia.testSpringcloudartifactId: microservice-student-provider-1002version: 1.0-SNAPSHOTuserName: phone: 123456---server:port: 1003context-path: /spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/t226_layui?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.liuxia.testSpringcloudartifactId: microservice-student-provider-1003version: 1.0-SNAPSHOTuserName: phone: 123456

pom依赖

<?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.liuxia</groupId><artifactId>t226_microservice</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>microservice-student-provider</artifactId><properties><java.version>1.8</java.version></properties><dependencies><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><!--添加注册中心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><dependency><groupId>com.liuxia</groupId><artifactId>microservice-common</artifactId><version>1.0-SNAPSHOT</version><scope>compile</scope></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>

然后在消费者microservice-student-consumer-80的controller中加上测试方法

@RequestMapping("/ribbon")public String ribbon() {return restTemplate.getForObject(SERVER_IP_PORT + "/student/ribbon", String.class);}

生产者microservice-student-providerr的StudentProviderController中加上测试方式(如果创建了3个项目,那么在3个都要加上)

@Value("${server.port}")private String port;@RequestMapping("/ribbon")public String ribbon(){return "工号【"+port+"】正在为您服务";}

然后我们可以在页面的请求看到,我们每次刷新请求都回换一个端口,说明它的默认规则是轮回算法

但是当我们有一个服务器宕机了,当轮回到这个服务器时,它不会自动帮我们跳过(好像有新版本已经解决这个问题了),而是会出现404,所以这就需要我们更改它的默认算法

我们在SpringCloudConfig中加一个配置即可

/*** 自定义轮询算法* @return*/@Beanpublic IRule myRule(){return new RetryRule();}

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,后面再说。

也就是把我们共同代码抽取出来,放入common公共部分中去,让维护更加方便

<!--引入Feign依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId></dependency>

我们在microservice-common这个项目新建一个StudentClienService,提取公共的请求代码

package com.liuxia.microservicecommon.service;import com.liuxia.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();}

StudentServiceImpl

package com.liuxia.microservicestudentprovider.service.impl;import com.liuxia.microservicecommon.entity.Student;import com.liuxia.microservicestudentprovider.repository.StudentRepository;import com.liuxia.microservicestudentprovider.service.StudentService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class StudentServiceImpl implements StudentService {@Autowiredprivate StudentRepository studentRepository;@Overridepublic void save(Student student) {studentRepository.save(student);}@Overridepublic Student findById(Integer id) {return studentRepository.findOne(id);}@Overridepublic List<Student> list() {return studentRepository.findAll();}@Overridepublic void delete(Integer id) {studentRepository.delete(id);}}

然后我们可以新建另外一个项目microservice-student-consumer-feign-80来,代码全部复制一遍,然后再加东西,也可以再原来基础上改就好了

需要引入的pom

<!--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><!--引入Feign依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId></dependency>

StudentConsumerController,这个就是需要改动的,把我们映射请求的已经移到了common里面,所以我们需要加一个方法来请求我们的通用部分

package com.liuxia.microservicestudentconsumerfeign80.controller;import com.liuxia.microservicecommon.entity.Student;import com.liuxia.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();}}

启动类也要加上注解

package com.liuxia.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.liuxia.*.*")@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})public class MicroserviceStudentConsumerFeign80Application {public static void main(String[] args) {SpringApplication.run(MicroserviceStudentConsumerFeign80Application.class, args);}}

然后这样就算我们生产者集群有一个服务器宕机了,当Ribbon在轮回的时候就会自动跳过它这个服务器了,让拿两个继续运行

如果觉得《微服务调用Ribbon负载均衡 Feign的使用》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。