当前位置:网站首页>OpenFeign realize load balance
OpenFeign realize load balance
2022-07-30 03:32:00 【Leon_Jinhai_Sun】
Official Documentation: SpringCloud OpenFeign
Feign, like RestTemplate, is also an HTTP client request tool, but it is more convenient to use.The first is dependencies:
org.springframework.cloud spring-cloud-starter-openfeign Then add the @EnableFeignClients annotation to the startup class:
@[email protected] class BorrowApplication {public static void main(String[] args) {SpringApplication.run(BorrowApplication.class, args);}}So now we need to call the interfaces provided by other microservices, what should we do?We can directly create an interface class corresponding to the service:
@FeignClient("userservice") //The HTTP request client declared as userservice servicepublic interface UserClient {}Then we directly create the method of the desired type, such as our previous:
RestTemplate template = new RestTemplate();User user = template.getForObject("http://userservice/user/"+uid, User.class);It can now be written directly like this:
@FeignClient("userservice")public interface UserClient {//The path is guaranteed to be consistent with that provided by other [email protected]("/user/{uid}")User getUserById(@PathVariable("uid") int uid); //The parameter and return value are also consistent}Then we inject it directly (it smells like Mybatis):
@ResourceUserClient userClient;@Overridepublic UserBorrowDetail getUserBorrowDetailByUid(int uid) {List borrow = mapper.getBorrowsByUid(uid);User user = userClient.getUserById(uid);//There is no need to write IP here, just write the service name bookservice directlyList bookList = borrow.stream().map(b -> template.getForObject("http://bookservice/book/"+b.getBid(), Book.class)).collect(Collectors.toList());return new UserBorrowDetail(user, bookList);} Access, you can see that the result is still correct:

And we can observe the invocation of the two user microservices, also in the form of load balancing.
According to the same method, we then change the call of the book management service to the interface form:

Finally, our Service code becomes:
@Servicepublic class BorrowServiceImpl implements BorrowService {@ResourceBorrowMapper mapper;@ResourceUserClient userClient;@ResourceBookClient bookClient;@Overridepublic UserBorrowDetail getUserBorrowDetailByUid(int uid) {List borrow = mapper.getBorrowsByUid(uid);User user = userClient.getUserById(uid);List bookList = borrow.stream().map(b -> bookClient.getBookById(b.getBid())).collect(Collectors.toList());return new UserBorrowDetail(user, bookList);}} Continue to visit for testing:

OK, normal.
Of course, Feign also has many other configuration options, which will not be introduced here, please refer to the official documentation for details.
边栏推荐
猜你喜欢
![[3D检测系列-PointRCNN]复现PointRCNN代码,并实现PointRCNN3D目标检测可视化,包含预训练权重下载链接(从0开始以及各种报错的解决方法)](/img/ca/17c047b8b1f030cc4ebb7e354070d9.png)
[3D检测系列-PointRCNN]复现PointRCNN代码,并实现PointRCNN3D目标检测可视化,包含预训练权重下载链接(从0开始以及各种报错的解决方法)

记录NLP各种资源网址

Nacos服务注册与发现

QT基础第三天(3)widget,dialog和mainwindow

【Node访问MongoDB数据库】

(RCE)远程代码/命令执行漏洞漏洞练习

北京bgp机房和普通机房的区别

分类之决策树分类

Awesome, Tencent technical experts handed Redis technical notes, and the download volume has exceeded 30W

复星医药募资44.84亿:高毅资产认购20亿 成第三大股东
随机推荐
Redis(十) - Redission原理与实践
基于全志D1-H和XR806的名贵植物监控装置
vscode 调试和远程
英诺特生物上市市值45亿:年营收降68% 红杉与元生是股东
nSoftware.PowerShell.Server.2020
REUSE_ALV_GRID_DISPLAY详解
传输层详解
路由过滤器
The most important transport layer
淘宝/天猫获得淘宝店铺详情 API
JUC (8) : synchronized little exercise
day10--在Linux上安装mysql
SQL Server中如何在date类型中提取年、月、日数据
selenium应用之拉勾简历邀约数据抓取与分析
解决谷歌浏览器跨域问题has been blocked by CORS policy: The request client is not a secure context and the resou
web初识
NLP自然语言处理(一)
朴素贝叶斯分类
JIT VS AOT
三年经验只会点点点(功能测试),辞职后你可能连工作都找不到了。