当前位置:网站首页>OpenFeign实现负载均衡
OpenFeign实现负载均衡
2022-07-30 03:25:00 【Leon_Jinhai_Sun】
Feign和RestTemplate一样,也是HTTP客户端请求工具,但是它的使用方式更加便捷。首先是依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
接着在启动类添加@EnableFeignClients
注解:
@SpringBootApplication
@EnableFeignClients
public class BorrowApplication {
public static void main(String[] args) {
SpringApplication.run(BorrowApplication.class, args);
}
}
那么现在我们需要调用其他微服务提供的接口,该怎么做呢?我们直接创建一个对应服务的接口类即可:
@FeignClient("userservice") //声明为userservice服务的HTTP请求客户端
public interface UserClient {
}
接着我们直接创建所需类型的方法,比如我们之前的:
RestTemplate template = new RestTemplate();
User user = template.getForObject("http://userservice/user/"+uid, User.class);
现在可以直接写成这样:
@FeignClient("userservice")
public interface UserClient {
//路径保证和其他微服务提供的一致即可
@RequestMapping("/user/{uid}")
User getUserById(@PathVariable("uid") int uid); //参数和返回值也保持一致
}
接着我们直接注入使用(有Mybatis那味了):
@Resource
UserClient userClient;
@Override
public UserBorrowDetail getUserBorrowDetailByUid(int uid) {
List<Borrow> borrow = mapper.getBorrowsByUid(uid);
User user = userClient.getUserById(uid);
//这里不用再写IP,直接写服务名称bookservice
List<Book> bookList = borrow
.stream()
.map(b -> template.getForObject("http://bookservice/book/"+b.getBid(), Book.class))
.collect(Collectors.toList());
return new UserBorrowDetail(user, bookList);
}
访问,可以看到结果依然是正确的:
并且我们可以观察一下两个用户微服务的调用情况,也是以负载均衡的形式进行的。
按照同样的方法,我们接着将图书管理服务的调用也改成接口形式:
最后我们的Service代码就变成了:
@Service
public class BorrowServiceImpl implements BorrowService {
@Resource
BorrowMapper mapper;
@Resource
UserClient userClient;
@Resource
BookClient bookClient;
@Override
public UserBorrowDetail getUserBorrowDetailByUid(int uid) {
List<Borrow> borrow = mapper.getBorrowsByUid(uid);
User user = userClient.getUserById(uid);
List<Book> bookList = borrow
.stream()
.map(b -> bookClient.getBookById(b.getBid()))
.collect(Collectors.toList());
return new UserBorrowDetail(user, bookList);
}
}
继续访问进行测试:
OK,正常。
当然,Feign也有很多的其他配置选项,这里就不多做介绍了,详细请查阅官方文档。
边栏推荐
- MySQL之数据查询(分类汇总与排序)
- 淘宝/天猫获得淘宝店铺详情 API
- 【SQL】按某个关联列用一张表的数据更新另一张表
- (六)《数电》——二极管与CMOS门电路(入门)
- Answer these 3 interview questions correctly, and the salary will go up by 20K
- Ansible简介(详细)特性+优点+设计理念+应用领域+系统架构+工作原理+任务执行流程
- JIT VS AOT
- Nuxt3学习
- 26 basic models in 6 categories that operators must master
- JUC (four): five/six states of shorthand thread
猜你喜欢
计划处理链的很多种情况
NLP Natural Language Processing (1)
OA项目之待开会议&历史会议&所有会议
JUC(五):共享带来的问题
Software testing interview questions and answer analysis, the strongest version in 2022
三年经验只会点点点(功能测试),辞职后你可能连工作都找不到了。
Solve The problem of Google browser cross-domain has had been blocked by CORS policy: The request The client is not a secure context and The resou
SQL Server中如何在date类型中提取年、月、日数据
测试人员,除了测试还得会点什么
Leetcode.24 两两交换链表中的节点(递归)
随机推荐
QT based on the third day (3) widget, dialog and mainwindow
【ModelArts系列】华为ModelArts Notebook训练yolov3模型(开发环境)
web初识
新接口——“淘特”关键词搜索的API接口
redis的学习_基础部分
QT基础第三天(3)widget,dialog和mainwindow
新手入门上位机开发 C#语言:PC串口发送数据
VMware disk expansion record
Are you still using the command line to read logs?Quickly use Kibana, visual log analysis YYDS
三年经验只会点点点(功能测试),辞职后你可能连工作都找不到了。
NLP Natural Language Processing (2)
22/07/21
Nacos实现高可用
Ansible简介(详细)特性+优点+设计理念+应用领域+系统架构+工作原理+任务执行流程
Leetcode.24 两两交换链表中的节点(递归)
复星医药募资44.84亿:高毅资产认购20亿 成第三大股东
精品:淘宝/天猫获取购买到的商品订单详情 API
leetcode每天5题-Day01
如何有效进行回顾会议(上)?
NLP自然语言处理(一)