@FeginClient 实例 (简单完整)

这里在同一个端口下演示,简单模拟一下

项目结构

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>groupId</groupId>
    <artifactId>com-tt-web</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>3.0.3</version>
        </dependency>

    </dependencies>
</project>

controller

@RestController
public class ControllerTest {

    @Autowired
    TestClient testClient;


    @RequestMapping("feign/h1")
    public String hello1(){
        System.out.println("hello1来了");
        testClient.hh();
        return "TestController hello1";
    }

    @RequestMapping("feign/h2")
    public String hello2(){
        System.out.println("hello2来了");
        return "TestController hello2";
    }

    @RequestMapping("feign/h3")
    public String hello3(){
        System.out.println("hello3来了");
        return "TestController hello3";
    }
}

client

@FeignClient(name = "testClient",url = "localhost:8080")
public interface TestClient {
    @RequestMapping("feign/h2")
    String hh();

}

  

main

@SpringBootApplication
@EnableFeignClients
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class,args);
    }
}
测试 直接浏览器输入:localhost:8080/fegin/h1
访问controller,然后controller来回转,没什么意义就是简单演示一下
 
原文地址:https://www.cnblogs.com/blog-tian/p/14994520.html