文章目录
一、Ribbon简介
ribbon是什么?
- SpringCloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具。
- 简单的说,Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将NetFlix的中间层服务连接在一起。Ribbon的客户端组件提供一系列完整的配置项如:连接超时、重试等等。简单的说,就是在配置文件中列出LoadBalancer(简称LB:负载均衡)后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随机连接等等)去连接这些机器。我们也很容易使用Ribbon实现自定义的负载均衡算法!
二、Ribbon负载均衡
1、在客户端(消费者) 导入依赖
<!-- ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
2、配置
server:
port: 8083
# 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/
3、在启动类加入注解
@EnableEurekaClient
4、修改DeptConsumerController
5、新建数据库
/*
Navicat Premium Data Transfer
Source Server : localhost本地
Source Server Type : MySQL
Source Server Version : 50719
Source Host : localhost:3306
Source Schema : db01
Target Server Type : MySQL
Target Server Version : 50719
File Encoding : 65001
Date: 29/01/2021 18:11:41
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for dept
-- ----------------------------
DROP TABLE IF EXISTS `dept`;
CREATE TABLE `dept` (
`deptno` bigint(20) NOT NULL AUTO_INCREMENT,
`dname` varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`db_source` varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`deptno`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '部门表' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of dept
-- ----------------------------
insert into dept (dname, db_source) values ('开发部',DATABASE());
insert into dept (dname, db_source) values ('人事部',DATABASE());
insert into dept (dname, db_source) values ('市场部',DATABASE());
insert into dept (dname, db_source) values ('运维部',DATABASE());
insert into dept (dname, db_source) values ('财务部',DATABASE());
SET FOREIGN_KEY_CHECKS = 1;
6、新增两个子模块
7、把8001拷贝到8002和8003
1)pom.xml
2)配置
8、测试
依次启动
访问:http://localhost:7001/
第一次访问:http://localhost:8083/consumer/dept/list
第二次访问:http://localhost:8083/consumer/dept/list
第三次访问:http://localhost:8083/consumer/dept/list
这样,就实现了轮询负载均衡。
三、Feign简介
四、Feign负载均衡
1、新建子模块 springcloud-consumer-dept-feign
把子模块 springcloud-consumer-dept-80 完全复制一份到feign子模块中。
2、在feign模块和api中导入feign依赖
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
3、在api模块添加service
import com.xiaol.springcloud.pojo.Dept;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.List;
@Component
@FeignClient(value ="SPRINGCLOUD-PROVIDER-DEPT") //这里的value对应注册中心的资源名
public interface DeptClientService {
@GetMapping("/dept/get/{id}")
public Dept queryById(@PathVariable("id")Long id);
@GetMapping("/dept/list")
public List<Dept> queryA11();
@PostMapping("/dept/add")
public Boolean addDept(Dept dept);
}
4、DeptConsumerController
import com.xiaol.springcloud.pojo.Dept;
import com.xiaol.springcloud.service.DeptClientService;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.List;
@RestController
public class DeptConsumerController {
@Autowired
private DeptClientService service = null;
@RequestMapping("/consumer/dept/add")
public boolean add(Dept dept){
return this.service.addDept(dept);
}
@RequestMapping("/consumer/dept/get/{id}")
public Dept get(@PathVariable("id")Long id) {
return this.service.queryById(id);
}
@RequestMapping("/consumer/dept/list")
public List<Dept> list() {
return this.service.queryA11();
}
}
5、在启动类DeptConsumer_feign开启feign
@EnableFeignClients(basePackages = {“com.xiaol.springcloud”})
@ComponentScan(“com.xiaol.springcloud”)
6、测试
依次启动
第一次访问:http://localhost:8083/consumer/dept/list
第二次访问:http://localhost:8083/consumer/dept/list
同样,可以实现负载均衡。
点我前往《SpringCloud专栏》
项目地址:关注公众号【小L星光】,回复【springcloud示例】即可获取。