您好,登录后才能下订单哦!
这篇文章主要介绍“Feign超时在yml文件里怎么配置”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Feign超时在yml文件里怎么配置”文章能帮助大家解决问题。
ribbon: # #指建立连接后从服务端读取到可用资源所用的时间 ReadTimeOut: 10000 #建立连接所用的时间,适用于网络状况正常的情况下,两端连接所需要的时间 ConnectTimeout: 5000
SpringBoot集成Feign在不使用注册中心实现模块之间的调用
今天就来说下怎么使用Fegin在不使用注册中心的情况下进行模块之间的调用。原因是:在项目小的情况下,而且还必须要调用其他模块的接口,那么这个时候就要用fegin了,当然还有其他的方法,但我在这里只说这一种简单的方法。
上代码:
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 环境 --> <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调用provider模块的接口,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>
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的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回车
关于“Feign超时在yml文件里怎么配置”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。