亲宝软件园·资讯

展开

Feign超时 yml文件配置

柠檬喜欢水蜜桃 人气:0

Feign超时 yml文件配置

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

Feign用法和基本配置

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模块放实体类

首先在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的用法,若有需要可以在官网或者其他地方搜索,我展示的只是适合新手入门上手。

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

加载全部内容

相关教程
猜你喜欢
用户评论