您好,登錄后才能下訂單哦!
這篇文章主要介紹“Feign超時(shí)在yml文件里怎么配置”的相關(guān)知識(shí),小編通過實(shí)際案例向大家展示操作過程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“Feign超時(shí)在yml文件里怎么配置”文章能幫助大家解決問題。
ribbon: # #指建立連接后從服務(wù)端讀取到可用資源所用的時(shí)間 ReadTimeOut: 10000 #建立連接所用的時(shí)間,適用于網(wǎng)絡(luò)狀況正常的情況下,兩端連接所需要的時(shí)間 ConnectTimeout: 5000
SpringBoot集成Feign在不使用注冊(cè)中心實(shí)現(xiàn)模塊之間的調(diào)用
今天就來說下怎么使用Fegin在不使用注冊(cè)中心的情況下進(jìn)行模塊之間的調(diào)用。原因是:在項(xiàng)目小的情況下,而且還必須要調(diào)用其他模塊的接口,那么這個(gè)時(shí)候就要用fegin了,當(dāng)然還有其他的方法,但我在這里只說這一種簡(jiǎn)單的方法。
上代碼:
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í)體類
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>
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的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回車
關(guān)于“Feign超時(shí)在yml文件里怎么配置”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。