一. 概述
原创不易,转载请注明出处
author:yww
1. 什么是Ribbon?
Spring Cloud Ribbon 是基于 Netflix Ribbon 实现的一套客户端负载均衡的工具。
简单的说,Ribbon 是 Netflix 发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将 Netflix 的中间层服务连接在一起。Ribbon 客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中列出Load Balancer(简称LB)后面所有的机器,Ribbon 会自动的帮助你基于某种规则(如简单轮询,随机连接等)去连接这些机器。我们也很容易使用 Ribbon 实现自定义的负载均衡算法。
2. Ribbon搭建步骤
建立消费者工程 项目结构如下
pom依赖引入
<?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">
<parent>
<artifactId>springcloudstudy</artifactId>
<groupId>com.xxqy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-consumer-dept-80</artifactId>
<dependencies>
<!--eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--ribbon负载均衡-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<groupId>com.xxqy</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
application.yml 配置 defaultZone为eureka集群配置
server:
port: 80
#消费者eureka配置
eureka:
client:
register-with-eureka: false #不向eureka注册自己
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
编写Config配置类
package com.byxx.springcloud.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;
/**
* @Author yww
* @CreateTime 2021-01-16
*/
@Configuration
public class ConfigBean {
//配置负载均衡实现RestTemplate
@Bean
@LoadBalanced //ribbon
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
自定义负载均衡算法 自定义算法 注入即可
package com.byxx.myribbonrule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RoundRobinRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 自定义ribbon负载均衡算法
* @Author yww
* @CreateTime 2021-01-19
*/
@Configuration
public class MyRibbonRule {
/**
* IRule
* RoundRobbinRule 轮询,默认
* RandomRule 随机
* AvailabilityFilteringRule 会先过滤掉,跳闸,访问故障的服务, 对剩下的进行轮询
* RetryRule 会先按照轮询获取服务,如果服务获取失败,则会在指定的时间内进行,重试
*/
//负载均衡配置 随机算法,默认轮询
@Bean
public RandomRule myRule(){
return new RandomRule();
}
}
Controller编写 使用RestTemplate发送http请求
package com.byxx.springcloud.controller;
import com.byxx.springcloud.entity.Dept;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.yaml.snakeyaml.events.Event;
import javax.annotation.Resource;
import java.util.List;
/**
* 服务消费者
* @Author yww
* @CreateTime 2021-01-16
*/
@RestController
@RequestMapping("/consumer")
public class DeptController {
@Resource
private RestTemplate restTemplate;
//单机
//private final String REST_URL_PREFIX = "http://localhost:8001";
//Ribbon 我们这里的地址,应该是一个变量,通过服务名来访问
private static final String REST_URL_PREFIX = "http://SPRINGCLOUDDEPT";
@RequestMapping("/getById/{id}")
public Dept getById(@PathVariable("id")Integer id){
return restTemplate.getForObject(REST_URL_PREFIX+"/dept/getById/"+id, Dept.class);
}
@RequestMapping("/add")
public boolean addDept(Dept dept){
return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",dept,Boolean.class);
}
@RequestMapping("/getAll")
public List<Dept> getAll(){
return restTemplate.getForObject(REST_URL_PREFIX+"/dept/getAll",List.class);
}
}
SpringBoot启动类 加上 @RibbonClient
package com.byxx.springcloud;
import com.byxx.myribbonrule.MyRibbonRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
/**
* 消费者使用eureka服务(集群),
* 整合Ribbon 负载均衡,使用注解@LoadBalanced 可以直接调用eureka服务
* @Author yww
* @CreateTime 2021-01-16
*/
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableEurekaClient
//在微服务启动的时候就去使用我们自定义的Ribbon负载均衡算法
@RibbonClient(name = "SPRINGCLOUDDEPT",configuration = MyRibbonRule.class)
public class SpringCloud_Consumer_80 {
public static void main(String[] args) {
SpringApplication.run(SpringCloud_Consumer_80.class,args);
}
}