美文网首页我的程序员修炼日记
使用Spring Cloud Feign构造httpClient

使用Spring Cloud Feign构造httpClient

作者: 一岁一枯荣啊 | 来源:发表于2019-10-31 09:10 被阅读0次

一。构建rest接口

@RestController
@RequestMapping("/user")
public class UserController {
    @PostMapping("/getUserById")
    public UserResp getUserById(@RequestBody UserRequest userRequest) {
        UserResp userResp = new UserResp();
        userResp.setId(userRequest.getId());
        userResp.setName("张" + userRequest.getId());
        return userResp;
    }
}

二。构建client模块

引入依赖jar以及相关domain的模块

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>client-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>client-core</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>client-core</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>client-domain</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

构建clientService

@FeignClient(name = "user", url = "${user-client-url}")
public interface UserService {
    @PostMapping("/user/getUserById")
    public UserResp getUserById(@RequestBody UserRequest userRequest);
}

3.打成jar包之后提供使用

使用方引入依赖并配置application.properties

user-client-url=http://127.0.0.1:8080

修改启动类注册feign服务

@EnableDiscoveryClient
@EnableFeignClients(basePackageClasses = UserService.class)
/*
使用
*/
@Resource
private UserService userService;

相关文章

网友评论

    本文标题:使用Spring Cloud Feign构造httpClient

    本文链接:https://www.haomeiwen.com/subject/oyszvctx.html