Spring Cloud Openfeign调用微服务

这篇文章介绍spring cloud openfeign调用微服务
前提是微服务已经搭建好,并正常运行中。

  1. 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>com.lbj</groupId>
    <artifactId>test-feign</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/>
    </parent>
    <properties>
        <mapper.version>3.4.0</mapper.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compile.source>1.8</maven.compile.source>
        <maven.compile.target>1.8</maven.compile.target>
        <spring.cloud.version>2.0.2.RELEASE</spring.cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>${spring.cloud.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>${spring.cloud.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            <version>${spring.cloud.version}</version>
        </dependency>

        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.11.0</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
  1. 编写一个Client
package com.lbj.feign.service;

import com.lbj.feign.dto.BaseResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @Auther: lbj
 * @Date: 2019/1/24 09:22
 * @Description:
 */
@Service
@FeignClient(name = "prompt-message")    // 填写微服务名称,会去serviceUrl.defaultZone里寻找prompt-message这个微服务
public interface ControlPromptServiceClient {

    @GetMapping("/query/singleControlPrompt")
    BaseResult singleControlPrompt(@RequestParam("ctrlCode") String ctrlCode);

}

  1. 编写controller
package com.lbj.feign.controller;

import com.lbj.feign.dto.BaseResult;
import com.lbj.feign.service.ControlPromptServiceClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Auther: lbj
 * @Date: 2019/1/24 11:02
 * @Description:
 */
@RestController
public class IndexController {
    @Resource
    private ControlPromptServiceClient controlPromptServiceClient;
    @GetMapping("/index")
    public BaseResult index(String ctrlCode) {
        BaseResult baseResult = controlPromptServiceClient.singleControlPrompt(ctrlCode);
        System.out.println("baseResult ==== " + baseResult);
        return baseResult;
    }

}

  1. yml文件
feign:
  okhttp:
    enabled: true

server:
  port: 9002
spring:
  application:
    name: test-feign

eureka:
  client:
    register-with-eureka: false
    fetch-registry: true
    serviceUrl:
      defaultZone: http://10.1.2.136:8761/eureka/

ribbon:
  eureka:
    enabled: true
  1. 启动类
package com.lbj.feign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @Auther: lbj
 * @Date: 2019/1/24 10:25
 * @Description:
 */
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class ApplicationStart {

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

}

  1. 在浏览器访问,正常。
    在这里插入图片描述
  2. 容易出错的地方
    如果feign调用的微服务是GET方法的话,要写上@RequestParam注解,否则会报如下错误
    “status”:405,“error”:“Method Not Allowed”,“message”:“Request method ‘POST’ not supported”
    解决方法:
    在@FeignClient接口方法中添加@RequestParam注解
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/haidian_fengyu/article/details/86625693