首页 > 其他 > 详细

OpenFeign - 使用

时间:2020-09-16 14:37:43      阅读:47      评论:0      收藏:0      [点我收藏+]

踩坑说明:
通过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信息

技术分享图片

OpenFeign - 使用

原文:https://www.cnblogs.com/codeclock/p/13678745.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!