feign配置超时时间,feign默认超时时间

  feign配置超时时间,feign默认超时时间

  

目录

假装超时阳明海运股份有限公司文件配置假装用法和基本配置首先说下我的模块结构首先在测试数字一(one)模块下新建持久化类模块首先在消费者的模块下新建假装调用类接下来新建供应者模块

 

  

Feign超时 yml文件配置

丝带:# #指建立连接后从服务端读取到可用资源所用的时间ReadTimeOut: 10000#建立连接所用的时间,适用于网络状况正常的情况下,两端连接所需要的时间连接超时: 5000

 

  

Feign用法和基本配置

SpringBoot集成Feign在不使用注册中心实现模块之间的调用

 

   今天就来说下怎么使用费金在不使用注册中心的情况下进行模块之间的调用。原因是:在项目小的情况下,而且还必须要调用其他模块的接口,那么这个时候就要用费金了,当然还有其他的方法,但我在这里只说这一种简单的方法。

  上代码:

  

首先说下我的模块结构

 

  测试数字一(one)是根模块用于对子模块专家坐标的版本控制管理其pom.xml如下:

  ?可扩展标记语言版本=1.0 编码=UTF八号?项目xmlns= http://maven。阿帕奇。org/POM/4。0 .0 xmlns : xsi= http://www。w3。org/2001/XML schema-instance xsi :架构位置= http://maven。阿帕奇。org/POM/4。0 .0 http://maven.apache.org/xsd/maven-4.0.0.xsd模型版本4 .0 .0/模型版本groupIdcom.person/groupId artifactId test1/artifactId包装POM/打包版本1.0-弹簧靴环境-父groupIdorg.springframework.boot/groupId artifact id spring-boot-starter-parent/artifact id version2.1.0.RELEASE/version相对路径//父属性项目。建造。sourceencodingutf-8/项目。建造。源代码编码项目。报道。outputencodingtf-8/项目。报道。输出编码Java。版本1.8/Java。版本!-春天的云版本-spring-cloud . version Greenwich . release/spring-cloud . version房产公司!- 引入春天的云依赖属国管理依赖项依赖项groupIdcom.person/groupId工艺品店

  /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调用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调用类

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代表想要调用的模块的前缀因为我的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.*;/** * 服务提供方 */@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的接口返回的数据,说明feign调用成功。关于feign还有很多很多牛x的用法,若有需要可以在官网或者其他地方搜索,我展示的只是适合新手入门上手。

  以上为个人经验,希望能给大家一个参考,也希望大家多多支持盛行IT。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: