spring cloud feign demo

1. 工程结构

2. 父工程pom.xml

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>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>com.anycode</groupId>
    <artifactId>springcloud-lk</artifactId>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>springcloud-api</module>
        <module>springcloud-provider-user-8001</module>
        <module>springcloud-provider-user-8002</module>
        <module>springcloud-consumer-user-feign</module>
    </modules>

    <!--打包方式-->
    <packaging>pom</packaging>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring.cloud.version>Hoxton.SR1</spring.cloud.version>
        <spring-cloud-alibaba-dependencies.version>2.1.1.RELEASE</spring-cloud-alibaba-dependencies.version>
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
        <lombok.version>1.16.18</lombok.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!--springCloud的依赖-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba-dependencies.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

3. springcloud-api 模块只是提供一个实体

4. springcloud-provider-user-8001 服务提供者模块

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <artifactId>springcloud-lk</artifactId>
        <groupId>com.anycode</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>springcloud-provider-user-8001</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.anycode</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--nacos客户端-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--热部署工具-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

</project>
java 复制代码
@RestController
@RequestMapping("/user")
public class UserController {
    @GetMapping("/{id}")
    public User getUser(@PathVariable("id") int id) {
        System.out.println("call provide-user-8001");
        return new User("Anycode", 18, "[email protected]", "北京");
    }
}
java 复制代码
@SpringBootApplication
@EnableDiscoveryClient
public class SpringcloudProviderUser8001 {

    public static void main(String[] args) {
        SpringApplication.run(SpringcloudProviderUser8001.class, args);
    }

}

5. springcloud-provider-user-8002

与springcloud-provider-user-8001 完全一样, 用于启动两个服务测试负载均衡.

6. springcloud-consumer-user-feign

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <artifactId>springcloud-lk</artifactId>
        <groupId>com.anycode</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>springcloud-consumer-user-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--nacos客户端-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.anycode</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

Feign客户端开发, 照猫画虎, 和Controller异曲同工.

java 复制代码
@FeignClient(name = "springcloud-provider-user", path = "/user")
public interface UserService {
    @GetMapping("/{id}")
    public User getUser(@PathVariable("id") int id);
}

启动类

java 复制代码
@SpringBootApplication
@EnableDiscoveryClient //注册服务
@EnableFeignClients // 开启Feign
public class SpringcloudConsumerUserFeign {

    public static void main(String[] args) {
        SpringApplication.run(SpringcloudConsumerUserFeign.class, args);
    }

}

7.启动所有服务查看nacos

8. 浏览器中测试

相关推荐
洗澡水加冰1 小时前
n8n搭建多阶段交互式工作流
后端·llm
陈随易1 小时前
Univer v0.8.0 发布,开源免费版 Google Sheets
前端·后端·程序员
六月的雨在掘金1 小时前
通义灵码 2.5 | 一个更懂开发者的 AI 编程助手
后端
朱龙凯2 小时前
MySQL那些事
后端
Re2752 小时前
剖析 MyBatis 延迟加载底层原理(1)
后端·面试
Victor3562 小时前
MySQL(63)如何进行数据库读写分离?
后端
Cache技术分享2 小时前
99. Java 继承(Inheritance)
前端·后端
M1A12 小时前
Python数据结构操作:全面解析与实践
后端·python
程序员蜗牛2 小时前
Controller层代码瘦身70%!5招打通任督二脉,效率飙升
后端
程序员岳焱2 小时前
Java高级反射实战:15个场景化编程技巧与底层原理解析
java·后端·编程语言