OpenFeign入门神级篇,字里行间透露出一种睥(pi)睨天下的气势,你还不首当其冲?
文章目錄
- #Feign簡(jiǎn)介
- 1、Feign是什么?
- 2、Feign有什么用?
- 3、OpenFeign和Feign有什么區(qū)別?
- 1、框架搭建
- 2、導(dǎo)入依賴(lài)
- 3、代碼編寫(xiě)
- 4、分析OpenFeign使用步驟
- 1、導(dǎo)入依賴(lài)已經(jīng)完成
- 2、接口創(chuàng)建
- 3、激活
- 5、測(cè)試
- 6、總結(jié)
#Feign簡(jiǎn)介
1、Feign是什么?
Feign是一種聲明式、模板化的HTTP客戶(hù)端。在Spring Cloud中使用Feign,可以做到使用HTTP請(qǐng)求訪(fǎng)問(wèn)遠(yuǎn)程服務(wù),就像調(diào)用本地方法一樣的,開(kāi)發(fā)者完全感知不到這是在調(diào)用遠(yuǎn)程方法,更感知不到在訪(fǎng)問(wèn)HTTP請(qǐng)求。
2、Feign有什么用?
Feign是一個(gè)聲明式的Web Service客戶(hù)端。它的出現(xiàn)使開(kāi)發(fā)Web Service客戶(hù)端變得很簡(jiǎn)單。使用Feign只需要?jiǎng)?chuàng)建一個(gè)接口加上對(duì)應(yīng)的注解,比如:FeignClient注解。Feign有可插拔的注解,包括Feign注解和JAX-RS注解。Feign也支持編碼器和解碼器。
3、OpenFeign和Feign有什么區(qū)別?
OpenFeign對(duì)Feign進(jìn)行增強(qiáng)支持Spring MVC注解,可以像Spring Web一樣使用HttpMessageConverters等
集成了Ribbon,可以實(shí)現(xiàn)客戶(hù)端的負(fù)載均衡
1、框架搭建
使用Eureka作為注冊(cè)中心 (當(dāng)然nacos、consul、zookeeper也可以);至于為什么要使用Eureka,你懂的
不是Eureka的代碼量少,只是因?yàn)槲矣鞋F(xiàn)成的代碼
首先創(chuàng)建一個(gè)父工程,然后創(chuàng)建四個(gè)子模塊,兩個(gè)server作為注冊(cè)中心,provider模塊作為服務(wù)提供模塊,consumer只作為模擬消費(fèi)模塊,不注冊(cè)進(jìn)Eureka的注冊(cè)中心
2、導(dǎo)入依賴(lài)
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、兩個(gè)server服務(wù)模塊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、代碼編寫(xiě)
1、兩個(gè)server模塊除了配置文件稍微不一樣外,其他都一樣
2、product模塊代碼編寫(xiě),框架如下
product實(shí)體類(lèi)
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、導(dǎo)入OpenFeign依賴(lài)
2、創(chuàng)建接口,添加@FeignClient注解聲明調(diào)用的服務(wù)
3、激活Feign組件,在啟動(dòng)類(lèi)上添加@EnableFeignClients即可
1、導(dǎo)入依賴(lài)已經(jīng)完成
2、接口創(chuàng)建
@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 #是否是將自己注冊(cè)到注冊(cè)中心registry-fetch-interval-seconds: 20 #Client多久去服務(wù)器拉去注冊(cè)信息 默認(rèn)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、測(cè)試
成功!
6、總結(jié)
使用OpenFeign進(jìn)行遠(yuǎn)程調(diào)用還是非常符合我們程序員的習(xí)慣的,完全感知不到像在遠(yuǎn)程調(diào)用,幫我們省掉了很多重復(fù)的代碼。
與50位技術(shù)專(zhuān)家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的OpenFeign入门神级篇,字里行间透露出一种睥(pi)睨天下的气势,你还不首当其冲?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Eureka出现Root name ‘t
- 下一篇: java.lang.AbstractMe