spring cloud feign demo

1. 工程结构

spring cloud feign demo_第1张图片

2. 父工程pom.xml


<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.0modelVersion>

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

    <groupId>com.anycodegroupId>
    <artifactId>springcloud-lkartifactId>
    <version>1.0-SNAPSHOTversion>

    <modules>
        <module>springcloud-apimodule>
        <module>springcloud-provider-user-8001module>
        <module>springcloud-provider-user-8002module>
        <module>springcloud-consumer-user-feignmodule>
    modules>

    
    <packaging>pompackaging>

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

    <dependencyManagement>
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring.cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>

            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>${spring-cloud-alibaba-dependencies.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>

            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>${junit.version}version>
            dependency>
            <dependency>
                <groupId>log4jgroupId>
                <artifactId>log4jartifactId>
                <version>${log4j.version}version>
            dependency>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <version>${lombok.version}version>
            dependency>
        dependencies>
    dependencyManagement>

project>

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

spring cloud feign demo_第2张图片

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


<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-lkartifactId>
        <groupId>com.anycodegroupId>
        <version>1.0-SNAPSHOTversion>
    parent>

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

    <dependencies>
        <dependency>
            <groupId>com.anycodegroupId>
            <artifactId>springcloud-apiartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
        dependency>
    dependencies>

project>

@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]", "北京");
    }
}
@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


<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.0modelVersion>

    <parent>
        <artifactId>springcloud-lkartifactId>
        <groupId>com.anycodegroupId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>springcloud-consumer-user-feignartifactId>
    <version>0.0.1-SNAPSHOTversion>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
        <dependency>
            <groupId>com.anycodegroupId>
            <artifactId>springcloud-apiartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

project>

spring cloud feign demo_第3张图片

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

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

启动类

@SpringBootApplication
@EnableDiscoveryClient //注册服务
@EnableFeignClients // 开启Feign
public class SpringcloudConsumerUserFeign {

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

}

7.启动所有服务查看nacos

spring cloud feign demo_第4张图片

8. 浏览器中测试

spring cloud feign demo_第5张图片

你可能感兴趣的:(spring,cloud,spring,后端)