当前位置:网站首页>Ribbon和Feign的对比-带简易例子
Ribbon和Feign的对比-带简易例子
2022-06-09 04:57:00 【是在下了】
Ribbon和Feign的对比-带简易例子
Ribbon和Feign的对比
Ribbon和Feign都是SpringCloud中的基于 HTTP 和 TCP 客户端 的软负载均衡的工具。
Feign 是在 Ribbon的基础上进行了一次改进,是一个使用起来更加方便的 HTTP 客户端。
采用接口的方式, 只需要创建一个接口,然后在上面添加注解即可 ,将需要调用的其他服务的方法定义成抽象方法即可, 不需要自己构建http请求。
两者要实现负载均衡都需要依托于eureka注册中心但也可以不借助注册中心,这种方式还没有尝试过。
由一个简单的例子来说明
整体项目结构
eureka-server(注册中心)
eureka-service-b(服务提供者B)
eureka-service-b-api(服务提供者B标准API和基础POJO)
feign-client-demo(服务调用者-Feign方式)
ribbon-client-demo(服务调用者-Ribbon方式)
通过Ribbon,A来调用B的服务
服务提供者B中书写简易示例:
服务提供者不需要做额外配置,正常书写暴露API请求即可
服务调用者A中调用B的示例代码:
关键部分代码
TestController.java
@RestController
public class TestController {
@Autowired
private RestTemplate restTemplate;
private static final String REST_URL_PREFIX = "http://EUREKA-SERVICE-B";
@GetMapping("/ribbon/test")
public List<Test> listTest(@RequestParam("id") String id){
ParameterizedTypeReference<List<Test>> typeReference = new ParameterizedTypeReference<List<Test>>() {
};
return restTemplate.exchange(REST_URL_PREFIX+"/tests?id="+id, HttpMethod.GET,null,typeReference).getBody();
}
}
Config.java
@Configuration
public class Config {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
通过Feign,A来调用B的服务
服务提供者B中书写简易示例:
服务提供者需要对eureka-service-b-api中新增ServiceFeignClient.java
@FeignClient(value = "EUREKA-SERVICE-B")
public interface ServiceFeignClient {
@GetMapping("/tests")
List<Test> listTest(@RequestParam("id") String id);
}
服务调用者A中调用B的示例代码:
关键部分代码
TestController.java
@RestController
public class TestController {
@Autowired
private ServiceFeignClient serviceFeignClient;
@GetMapping("/feign/test")
public List<Test> listTest(@RequestParam("id") String id){
return serviceFeignClient.listTest(id);
}
}
总结
从上面来看吧,功能都一样,看代码量的话也差不多,区别就在于,ribbion的方式,服务调用者和服务提供者需要通过接口文档进行沟通交流。
feign的方式的话,接口文档可以省了。
工程项目源码
边栏推荐
- ASP. Net core build scheduling service - use generic host with quartz Net
- openGL_ 05 simple application of shader
- 数据库的三大范式
- 2022 R2 mobile pressure vessel filling test simulation 100 questions and simulation test
- Mmdet modify the font size, position, color and fill box of the detection box
- Rendering pipeline ---- easy to understand and introduce to the interviewer
- openGL_ 02 point line surface triangle
- 三方账号授权登录系统设计思路
- Troubleshooting: MySQL containers in alicloud lightweight application servers stop automatically
- OpenGL 01 - créer une fenêtre
猜你喜欢

Proteus simulation Arduino

Win10 installing appium environment

Three paradigms of database

Openstack Learning Series 12: installing CEPH and docking openstack

The half year revenue of mushroom street was 168million yuan: a year-on-year decrease of 29% and an operating loss of 240million yuan
![[006] [ESP32开发笔记] 使用Flash下载工具烧录固件步骤](/img/a0/5d5e6c076d267c0ffe4c1e8f10e408.png)
[006] [ESP32开发笔记] 使用Flash下载工具烧录固件步骤

JVM interview

Faster RCNN

Just now, we received a letter of thanks from the Beijing Organizing Committee for the Winter Olympics

ASP. Net core build scheduling service - use generic host with quartz Net
随机推荐
Make in-depth research and summary, go to a higher level, and make new year's resolutions
[004] [ESP32开发笔记] 音频开发框架ADF环境搭建——基于ESP-IDF
2022 welder (elementary) special operation certificate examination question bank and simulation examination
Applets have become a must for super apps, competing for private domain "reserve"
渗透测试路径字典、爆破字典
Format interchange among Yolo, coco and VOC datasets
[sword finger offer II 001. integer division] the same as leedcode 29 Divide two numbers
Typescript learning [7] advanced types: Union type and cross type
Quickly detect high-risk vulnerabilities of common middleware and components in penetration testing
Test question bank and online simulation test for operation certificate of main principals of hazardous chemical business units in 2022
openGL_ 01 create window
"Diwen Cup" skill competition between teachers and students of Electrical Engineering Department of Zibo technician college was successfully held
Win10 registry failed to save changes to permissions access denied
. Net core 3.0 grpc custom service
wps ppt图片如何一张一张出来
API 網關 Apache APISIX 在 AWS Graviton3 上的安裝與性能測試
Clcnet: Rethink integrated modeling with classified confidence network (with source code download)
wps ppt背景图片如何换颜色
[006] [ESP32開發筆記] 使用Flash下載工具燒錄固件步驟
ps如何给图像加白边