Feign超時 在yml文件里的配置方式
Feign超時 yml文件配置
ribbon: # #指建立連接后從服務(wù)端讀取到可用資源所用的時間 ReadTimeOut: 10000 #建立連接所用的時間,適用于網(wǎng)絡(luò)狀況正常的情況下,兩端連接所需要的時間 ConnectTimeout: 5000
Feign用法和基本配置
SpringBoot集成Feign在不使用注冊中心實現(xiàn)模塊之間的調(diào)用
? 今天就來說下怎么使用Fegin在不使用注冊中心的情況下進行模塊之間的調(diào)用。原因是:在項目小的情況下,而且還必須要調(diào)用其他模塊的接口,那么這個時候就要用fegin了,當(dāng)然還有其他的方法,但我在這里只說這一種簡單的方法。
上代碼:
首先說下我的模塊結(jié)構(gòu)

test1是根模塊用于對子模塊maven坐標的版本控制管理其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模塊下新建兩個模塊分別為consumer,provider和pojo,其中consumer使用Feign調(diào)用provider模塊的接口,pojo模塊放實體類
首先在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實體類供其他模塊使用:
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)用的模塊的前綴因為我的provider模塊的端口是8011因此http://localhost:8011就是我的provider前綴,下面的請求路徑“/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坐標:
<?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","紅色");
}
}這個時候在瀏覽器里面輸入http://localhost:8012/order/findone/12回車

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

