目录
3. 创建⼀个service, 并使⽤Fegin实现微服务调⽤
一、Feign远程调⽤
先来看我们以前利⽤RestTemplate发起远程调⽤的代码:
存在下⾯的问题:
- 代码可读性差,编程体验不统⼀
- 参数复杂URL难以维护
二、Feign简介
Feign是Spring Cloud提供的⼀个声明式的伪Http客户端, 它使得调⽤远程服务就像调⽤本地服务⼀样简单, 只需要创建⼀个接⼝并添加⼀个注解即可。 Nacos很好的兼容了Feign, Feign 默认集了Ribbon, 所以在Nacos下使⽤Fegin默认就实现了负载均衡的效果官⽅地址: https://github.com/OpenFeign/feign![]()
三、基本使⽤
基于之前ribbon代码的基础上
1. 加⼊Fegin的依赖
<!-- fegin组件 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
2. 在主类上添加Fegin的注解
@EnableFeignClients//开启Fegin@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients//开启Fegin public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class); } //


&spm=1001.2101.3001.5002&articleId=140714628&d=1&t=3&u=8b4cd2f2763348f68f8a6e7cf1cb5aa3)
1万+

被折叠的 条评论
为什么被折叠?



