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)用
? 今天就來(lái)說(shuō)下怎么使用Fegin在不使用注冊(cè)中心的情況下進(jìn)行模塊之間的調(diào)用。原因是:在項(xiàng)目小的情況下,而且還必須要調(diào)用其他模塊的接口,那么這個(gè)時(shí)候就要用fegin了,當(dāng)然還有其他的方法,但我在這里只說(shuō)這一種簡(jiǎn)單的方法。
上代碼:
首先說(shuō)下我的模塊結(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);
}
}接下來(lái)新建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ù),說(shuō)明feign調(diào)用成功。關(guān)于feign還有很多很多牛x的用法,若有需要可以在官網(wǎng)或者其他地方搜索,我展示的只是適合新手入門上手。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于SpringSecurity?Context?中獲取和更改當(dāng)前用戶信息的問(wèn)題
SpringSecurityContext在異步線程中無(wú)法獲取用戶信息,因其與請(qǐng)求線程綁定;此外,用戶信息更新后跳轉(zhuǎn)頁(yè)面時(shí),身份會(huì)被降級(jí)為匿名,導(dǎo)致信息無(wú)法及時(shí)同步,本文給大家介紹SpringSecurity?Context?中獲取和更改當(dāng)前用戶信息的問(wèn)題,感興趣的朋友一起看看吧2024-09-09
Spring Cloud Eureka服務(wù)治理的實(shí)現(xiàn)
服務(wù)治理是微服務(wù)框架中最為核心和基礎(chǔ)的模塊,它主要是用來(lái)實(shí)現(xiàn)各個(gè)微服務(wù)實(shí)例的自動(dòng)化注冊(cè)與發(fā)現(xiàn)。這篇文章主要介紹了Spring Cloud Eureka服務(wù)治理的實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2018-06-06
通過(guò)RedisTemplate連接多個(gè)Redis過(guò)程解析
這篇文章主要介紹了通過(guò)RedisTemplate連接多個(gè)Redis過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
java基礎(chǔ)學(xué)習(xí)JVM中GC的算法
這篇文章主要介紹了java基礎(chǔ)學(xué)習(xí)JVM中GC的算法,通過(guò)圖文加深對(duì)GC算法思路的理解。2017-11-11
Springboot詳解實(shí)現(xiàn)食品倉(cāng)庫(kù)管理系統(tǒng)流程
這是一個(gè)使用Springboot開發(fā)的食品倉(cāng)庫(kù)管理系統(tǒng),是為商家提供商品貨物進(jìn)銷存的信息化管理系統(tǒng),具有一個(gè)倉(cāng)庫(kù)管理系統(tǒng)該有的所有功能,感興趣的朋友快來(lái)看看吧2022-06-06
k8s+springboot+CronJob定時(shí)任務(wù)部署實(shí)現(xiàn)
本文主要介紹了k8s+springboot+CronJob定時(shí)任務(wù)部署實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Java設(shè)置請(qǐng)求響應(yīng)時(shí)間的多種實(shí)現(xiàn)方式
在前后端分離的開發(fā)模式中,前端請(qǐng)求后端獲取數(shù)據(jù)時(shí),合理設(shè)置響應(yīng)時(shí)間(超時(shí)時(shí)間)是提升系統(tǒng)性能和用戶體驗(yàn)的關(guān)鍵,本文將深入探討如何在Java中設(shè)置請(qǐng)求的響應(yīng)時(shí)間,需要的朋友可以參考下2025-01-01

