当前位置 博文首页 > 文章内容

    OpenFeign - 使用

    作者: 栏目:未分类 时间:2020-09-16 14:00:25

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    踩坑说明: 通过Feign发送Get请求,一定要在接口的参数前面加上 @RequestParam 注解,否则会报如下的错误: feign.FeignException$MethodNotAllowed: [405] during [GET] to [http://api-02-application/getObjParam] [PersonClient#getObjParam(String)]: [{"timestamp":"2020-09-16T05:35:22.853+0000","status":405,"error":"Method Not Allowed","message":"Request method 'POST' not supported","path":"/getObjParam"}]

    准备

    构建一个新的项目API-02-APPLICATION,提供访问的API,并注册到注册中心(这里使用Eureka做注册中心)上

    (OPENFEIGN-APP是等下要构建的项目,这里先不用管)

    在API-02-APPLICATION中的Controller中提供两个接口,以便等下在其他项目中演示调用该接口:

    @GetMapping("/getObjParam")
    public Person getObjParam(String name) {
        Person person = new Person();
        person.setId(100);
        person.setName(name);
        return person;
    }
    
    @PostMapping("/postParam")
    public Person postParam(@RequestBody String name) {
        System.out.println("name:" + name);
        Person person = new Person();
        person.setId(100);
        person.setName("xiaoming" + name);
        return person;
    }
    

    OpenFeign 使用

    构建项目

    1. 引入OpenFeign依赖
    <!-- openFeign-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    

    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>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.6.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>client-open-feign</artifactId>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
        </properties>
    
        <dependencies>
            <!-- 导入自定义公共模块中的Person类 -->
            <dependency>
                <groupId>com.qiankai</groupId>
                <artifactId>client-common</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
    
            <!-- eureka client -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
    
            <!-- openFeign-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
    
            <!-- web -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!-- 用于上报节点信息 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    
    1. 配置文件
    server:
      port: 8020
    eureka:
      client:
        # 设置服务注册中心的url
        service-url:
          defaultZone: http://localhost:7900/eureka
      instance:
        instance-id: qiankai-client-8020 #显示此名字(默认是当前项目http://localhost:8001)
        prefer-ip-address: true #访问路径可以显示ip地址
    
    spring:
      application:
        name: openfeign-app
    
    1. 启动类上添加注解 @EnableFeignClients
    package com.qiankai.feign;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    /**
     * OpenFeign 客户端调用
     *
     * @author kai_qian
     * @version v1.0
     * @since 2020/09/16 09:15
     */
    @SpringBootApplication
    @EnableEurekaClient
    @EnableFeignClients // 激活Feign
    public class AppOpenFeign {
        public static void main(String[] args) {
            SpringApplication.run(AppOpenFeign.class, args);
        }
    }
    
    1. 编写客户端调用接口
    package com.qiankai.feign.client;
    
    import com.qiankai.common.dto.Person;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestParam;
    
    /**
     * OpenFeign 客户端调用演示
     *
     * @author kai_qian
     * @version v1.0
     * @since 2020/09/16 09:24
     */
    @FeignClient(value = "API-02-APPLICATION")  // value为 Eureka上的服务应用名,会自动去注册中心中查找对应的服务地址
    public interface PersonClient {
    
        @GetMapping("/getObjParam")
        Person getObjParam(@RequestParam("name") String name);  // GET 请求的参数一定要加上 @RequestParam注解,否则会报错
    
        @PostMapping("/postParam")
        Person postParam(@RequestBody String name);
    }
    
    
    1. 通过Feign客户端调用其他服务的接口
    package com.qiankai.feign.controller;
    
    import com.qiankai.common.dto.Person;
    import com.qiankai.feign.client.PersonClient;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * openFeign 客户端调用演示
     *
     * @author kai_qian
     * @version v1.0
     * @since 2020/09/16 09:27
     */
    @RestController
    public class PersonController {
    
        @Autowired
        private PersonClient personClient;
    
        @GetMapping("/testGetPerson")
        public void getPerson() {
            Person person = personClient.getObjParam("aaa");
            System.out.println(person.toString());
        }
    
        @GetMapping("/testPostPerson")
        public void postPerson() {
            Person person = personClient.postParam("bbb");
            System.out.println(person.toString());
        }
    }
    
    1. 结果展示

    依次访问接口 /testGetPerson, /testPostPerson,控制台打印返回的Person信息