java ribbon配置
来源:http://www.tudoupe.com时间:2022-03-02
概念:
Ribbon是一种客户端负载平衡器,可让您对HTTP和TCP客户端的行为进行大量控制。Feign已使用Ribbon,因此,如果使用@FeignClient,则本节也适用。Ribbon中的中心概念是指定客户的概念。每个负载均衡器都是组件的一部分,这些组件可以一起工作以按需联系远程服务器,并且该组件具有您作为应用程序开发人员提供的名称(例如,使用@FeignClient批注)。根据需要,Spring Cloud通过使RibbonClientConfiguration为每个命名的客户端创建一个新的集合作为ApplicationContext。其中包含ILoadBalancer,RestClient和ServerListFilter。
1引入jar
pom.xml
org.springframework.cloud spring-cloud-starter-netflix-ribbon
2.数据库easy code插件生成各个层
3添加application.yml配置
4
5
6.设置接口内容

@Service(“carService”)
public class CarServiceImpl extends ServiceImpl<CarDao, Car> implements CarService {
//依赖注入
@Resource
private RestTemplate restTemplate;
7.轮询,随机配置
@Configuration
public class RibbonConfiguration {
//实例化bean,交给容器管理
@Bean//相当于过去的
@LoadBalanced //当使用RestTemplate调用远程接口的时候使用负载均衡
public RestTemplate restTemplate(){
return new RestTemplate();
}
//实例化IRule的实现类,指定负载均衡策略
@Bean
public IRule iRule()
{
//随机
//return new RandomRule();
//轮询
return new RoundRobinRule();
}
}
8.客户端controller修改
@GetMapping("/selectOne")
public R selectOne(Integer id) {
Order order = this.orderService.getById(id);
order.setCarId(14221);
order.setOrderno(“服务器member_server_a”);
return success(order);
}
10.登陆客户端localhost:http://localhost:14222/swagger-ui/index.html
F服务注册中心:http://localhost:8848/nacos
ribbon:localhost:14231/car/selectOne/1
观察轮询随机即可
上一篇:Object.hasOwnProperty和Object.propertyIsEnumerable的区别
下一篇:vue3 setup + ts + vite 项目问题解决:Cannot find module ... or its
相关新闻
- 2023-05-06 微pe怎么初始化U盘(微pe怎么恢复初
- 2023-05-06 Xp系统boot 进入pe(boot manager 怎么进入
- 2023-05-06 win pe修复bcdboot(pe修复系统)
- 2023-05-06 win7更新失败 pe(win7更新失败还原更
- 2023-05-06 u盘装了pe读取不了(u盘能进pe读取不
- 2023-05-06 u盘pe 发热(u盘发热烫手)
- 2023-05-06 u盘pe下看不到硬盘(u盘启动pe看不到
- 2023-05-06 pe盘 ntfs(u盘ntfs格式)
- 2023-05-06 sony笔记本进入pe模式(联想笔记本怎
- 2023-05-06 pe启动盘进不去(pe启动盘进不去系统
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
