OpenFeign入门神级篇,字里行间透露出一种睥(pi)睨天下的气势,你还不首当其冲?
文章目录
- #Feign简介
- 1、Feign是什么?
- 2、Feign有什么用?
- 3、OpenFeign和Feign有什么区别?
- 1、框架搭建
- 2、导入依赖
- 3、代码编写
- 4、分析OpenFeign使用步骤
- 1、导入依赖已经完成
- 2、接口创建
- 3、激活
- 5、测试
- 6、总结
#Feign简介
1、Feign是什么?
Feign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用Feign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求。
2、Feign有什么用?
Feign是一个声明式的Web Service客户端。它的出现使开发Web Service客户端变得很简单。使用Feign只需要创建一个接口加上对应的注解,比如:FeignClient注解。Feign有可插拔的注解,包括Feign注解和JAX-RS注解。Feign也支持编码器和解码器。
3、OpenFeign和Feign有什么区别?
OpenFeign对Feign进行增强支持Spring MVC注解,可以像Spring Web一样使用HttpMessageConverters等
集成了Ribbon,可以实现客户端的负载均衡
1、框架搭建
使用Eureka作为注册中心 (当然nacos、consul、zookeeper也可以);至于为什么要使用Eureka,你懂的
不是Eureka的代码量少,只是因为我有现成的代码
首先创建一个父工程,然后创建四个子模块,两个server作为注册中心,provider模块作为服务提供模块,consumer只作为模拟消费模块,不注册进Eureka的注册中心
2、导入依赖
1、父pom
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.hao</groupId><artifactId>cloud-eureka-demo</artifactId><packaging>pom</packaging><version>1.0-SNAPSHOT</version><modules><module>cloud-eureka-server</module><module>cloud-eureka-server02</module><module>service-provider</module><module>service-consumer-feign</module></modules><parent><artifactId>spring-boot-starter-parent</artifactId><groupId>org.springframework.boot</groupId><version>2.4.3</version></parent><properties><spring-cloud.version>2020.0.2</spring-cloud.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement></project>2、两个server服务模块pom(相同)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>cloud-eureka-demo</artifactId><groupId>com.hao</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-eureka-server02</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency></dependencies></project>3、provider模块
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>cloud-eureka-demo</artifactId><groupId>com.hao</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>service-provider</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies> </project>4、consumer模块
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>cloud-eureka-demo</artifactId><groupId>com.hao</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><groupId>com.feign</groupId><artifactId>service-consumer-feign</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies> </project>3、代码编写
1、两个server模块除了配置文件稍微不一样外,其他都一样
2、product模块代码编写,框架如下
product实体类
service
@Service public class ProductServiceImpl implements ProductService {@Overridepublic List<Product> selectProductList() {return Arrays.asList(new Product(1, "HuaWei nova3", 100, 2999.0),new Product(2, "xiaomi", 99, 1999.0),new Product(3, "vivo", 102, 2999.0));} }application.yml
server:port: 7070 spring:application:name: service-providereureka:instance:hostname: providerprefer-ip-address: trueinstance-id: http://${spring.cloud.client.ip-address}:${server.port}client:service-url:defaultZone: http://root:root@127.0.0.1:8080/eureka/,http://root:root@127.0.0.1:8081/eureka/4、分析OpenFeign使用步骤
1、导入OpenFeign依赖
2、创建接口,添加@FeignClient注解声明调用的服务
3、激活Feign组件,在启动类上添加@EnableFeignClients即可
1、导入依赖已经完成
2、接口创建
@FeignClient(value = "service-provider") public interface ProductService {@GetMapping(value = "/product/list")List<Product> selectProductList(); } @Service public class OrderServiceImpl implements OrderService {@Autowiredprivate ProductService productService;@Overridepublic Order selectOrderById(Integer id) {return new Order(id, "one", "china", 199D, productService.selectProductList());} } @RestController public class OrderController {@Resourceprivate OrderService orderService;@GetMapping(value ="/order/{id}")public Order getOrderById(@PathVariable("id") Integer id) {return orderService.selectOrderById(id);} }3、激活
4、其他代码
application.yml
server:port: 9091spring:application:name: service-consumer-feigneureka:client:register-with-eureka: false #是否是将自己注册到注册中心registry-fetch-interval-seconds: 20 #Client多久去服务器拉去注册信息 默认30sservice-url:defaultZone: http://root:root@127.0.0.1:8080/eureka/,http://root:root@127.0.0.1:8081/eureka/ @Data @AllArgsConstructor @NoArgsConstructor public class Order {private Integer id;private String orderNo;private String orderAddress;private Double totalPrice;private List<Product> productList; } @Data @AllArgsConstructor @NoArgsConstructor public class Product {private Integer id;private String productName;private Integer productNum;private Double productPrice; }5、测试
成功!
6、总结
使用OpenFeign进行远程调用还是非常符合我们程序员的习惯的,完全感知不到像在远程调用,帮我们省掉了很多重复的代码。
与50位技术专家面对面20年技术见证,附赠技术全景图总结
以上是生活随笔为你收集整理的OpenFeign入门神级篇,字里行间透露出一种睥(pi)睨天下的气势,你还不首当其冲?的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: Eureka出现Root name ‘t
- 下一篇: java.lang.AbstractMe