Feign超時(shí) 在yml文件里的配置方式
Feign超時(shí) yml文件配置
ribbon: # #指建立連接后從服務(wù)端讀取到可用資源所用的時(shí)間 ReadTimeOut: 10000 #建立連接所用的時(shí)間,適用于網(wǎng)絡(luò)狀況正常的情況下,兩端連接所需要的時(shí)間 ConnectTimeout: 5000
Feign用法和基本配置
SpringBoot集成Feign在不使用注冊(cè)中心實(shí)現(xiàn)模塊之間的調(diào)用
? 今天就來說下怎么使用Fegin在不使用注冊(cè)中心的情況下進(jìn)行模塊之間的調(diào)用。原因是:在項(xiàng)目小的情況下,而且還必須要調(diào)用其他模塊的接口,那么這個(gè)時(shí)候就要用fegin了,當(dāng)然還有其他的方法,但我在這里只說這一種簡單的方法。
上代碼:
首先說下我的模塊結(jié)構(gòu)
test1是根模塊用于對(duì)子模塊maven坐標(biāo)的版本控制管理其pom.xml如下:
<?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.person</groupId> <artifactId>test1</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>provider</module> <module>consumer</module> <module>pojo</module> </modules> <!--spring boot 環(huán)境 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <!--spring cloud 版本--> <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version> </properties> <!--引入Spring Cloud 依賴--> <dependencyManagement> <dependencies> <dependency> <groupId>com.person</groupId> <artifactId>pojo</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!--spring boot web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.1.RELEASE</version> </dependency> <!--feign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.10</version> <scope>provided</scope> </dependency> </dependencies> </dependencyManagement> </project>
緊接著在test1模塊下新建兩個(gè)模塊分別為consumer,provider和pojo,其中consumer使用Feign調(diào)用provider模塊的接口,pojo模塊放實(shí)體類
首先在test1模塊下新建pojo模塊
pojo模塊的pom.xml:
<?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>test1</artifactId> <groupId>com.person</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>pojo</artifactId> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> </project>
在pojo模塊下新建Goods實(shí)體類供其他模塊使用:
package com.person.pojo.consumer; import lombok.*; import javax.validation.constraints.NotNull; import java.io.Serializable; @Setter @Getter @AllArgsConstructor @NoArgsConstructor @Builder(toBuilder = true) public class Goods implements Serializable { private static final long serialVersionUID = 1L; @NotNull(message = "id不能為空") private String id; private String name; private String price; private String colour; }
consumer的yml文件:
server: port: 8012
consumer的pom.xml如下:
<?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>test1</artifactId> <groupId>com.person</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>consumer</artifactId> <dependencies> <dependency> <groupId>com.person</groupId> <artifactId>pojo</artifactId> </dependency> <!--feign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!--spring boot web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
首先在consumer的模塊下新建feign調(diào)用類
package com.person.feign; import com.person.pojo.consumer.Goods; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @FeignClient(name = "provider",url = "http://localhost:8011") @RequestMapping("/person") public interface GoodsFeignClient { @GetMapping("/findone/{id}") public Goods findOnebyId(@PathVariable("id") String id); }
上面代碼所示 url代表想要調(diào)用的模塊的前綴因?yàn)槲业膒rovider模塊的端口是8011因此http://localhost:8011就是我的provider前綴,下面的請(qǐng)求路徑“/person/findone/{id}”指的是我的provider模塊接口路徑
下面在consumer模塊下新建controller方法:
package com.person.controller; import com.person.feign.GoodsFeignClient; import com.person.pojo.consumer.Goods; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/order") public class OrderController { @Autowired private GoodsFeignClient goodsFeignClient; @GetMapping("/findone/{id}") public Goods findOnebyId(@PathVariable("id") String id) { return goodsFeignClient.findOnebyId(id); } }
接下來新建provider模塊
provider的yml文件:
server: port: 8011
其pom.xml坐標(biāo):
<?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>test1</artifactId> <groupId>com.person</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>provider</artifactId> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.person</groupId> <artifactId>pojo</artifactId> </dependency> <!--spring boot web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
然后在provider 中新建controller:
package com.person.controller; import com.person.pojo.consumer.Goods; import com.person.service.GoodsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; /** * 服務(wù)提供方 */ @RestController @RequestMapping("/person") public class GoodsController { @GetMapping("/findone/{id}") public Goods findOne(@PathVariable("id") String id) { return new Goods("1","紅蘋果","8888","紅色"); } }
這個(gè)時(shí)候在瀏覽器里面輸入http://localhost:8012/order/findone/12回車
顯示的是provider的接口返回的數(shù)據(jù),說明feign調(diào)用成功。關(guān)于feign還有很多很多牛x的用法,若有需要可以在官網(wǎng)或者其他地方搜索,我展示的只是適合新手入門上手。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java判斷List中相同值元素的個(gè)數(shù)實(shí)例
今天小編就為大家分享一篇Java判斷List中相同值元素的個(gè)數(shù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07JDBC對(duì)MySQL數(shù)據(jù)庫布爾字段的操作方法
這篇文章主要介紹了JDBC對(duì)MySQL數(shù)據(jù)庫布爾字段的操作方法,實(shí)例分析了JDBC操作mysql布爾字段的原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-02-02java實(shí)現(xiàn)異步導(dǎo)出數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)異步導(dǎo)出數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11java.lang.UnsupportedOperationException分析及解決辦法
日常開發(fā)中我遇到j(luò)ava.lang.UnsupportedOperationException:異常兩次了,下面這篇文章主要給對(duì)大家介紹了關(guān)于java.lang.UnsupportedOperationException分析及解決辦法,需要的朋友可以參考下2024-03-03詳細(xì)分析Java并發(fā)集合ArrayBlockingQueue的用法
這篇文章主要介紹了詳細(xì)分析Java并發(fā)集合ArrayBlockingQueue的用法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04Java中的CopyOnWriteArrayList深入解讀
這篇文章主要介紹了Java中的CopyOnWriteArrayList深入解讀,在 ArrayList 的類注釋上,JDK 就提醒了我們,如果要把 ArrayList 作為共享變量的話,是線程不安全的,需要的朋友可以參考下2023-12-12布隆過濾器詳解以及其在Java中的實(shí)際應(yīng)用
布隆過濾器是一種數(shù)據(jù)結(jié)構(gòu),比較巧妙的概率型數(shù)據(jù)結(jié)構(gòu)(probabilistic data structure),特點(diǎn)是高效地插入和查詢,這篇文章主要給大家介紹了關(guān)于布隆過濾器詳解以及其在Java中的實(shí)際應(yīng)用,需要的朋友可以參考下2023-12-12