新建ums工程(使用OpenFeign)

关系图

创建 module

在父工程中新建 module ums_openfeign

添加依赖

添加 openfeign 依赖

修改pom.xml文件,添加依赖

<!-- openfeign依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

完整依赖

<dependencies>
    <!-- openfeign依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>top.malaoshi</groupId>
        <artifactId>common</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <!-- springboot依赖,不写版本号表示继承父工程 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- springboot依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

application.yml

从ums工程中,复制application.yml粘贴到该工程中

主启动类

要加上@EnableFeignClients

不需要 @EnableEurekaClient

@SpringBootApplication
@EnableFeignClients
public class UMSOpenFeignMain {
    public static void main(String[] args) {
        SpringApplication.run(UMSOpenFeignMain.class,args);
    }
}

创建接口

OpenFeign 通过接口和注解即可实现web service client,简化操作,注解中的url 就是 原Controller 中的url:

package malaoshi.ums.clients;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import top.malaoshi.entities.Notice;
import top.malaoshi.json.CommonResult;

@Component
@FeignClient(value="NOTICE-SERVICE") //notice微服务名称
public interface NoticeClient {
    //notice服务的url
    @GetMapping(value="/getNoticeById/{id}")
    public CommonResult<Notice> getNoticeById(@PathVariable("id") String id);

    //notice服务的url
    @PostMapping(value="/sendNotice")
    public CommonResult sendNotice(@RequestBody Notice notice);
}
解释:
  • @FeignClient(value="NOTICE-SERVICE"):注解中是eureka server中微服务的名字,如下图:

  • 方法名及注解,是从NOTICE-SERVICE服务的controller中复制过来的,如下图:

创建controller

package malaoshi.ums.controllers;

import lombok.extern.slf4j.Slf4j;
import malaoshi.ums.clients.NoticeClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import top.malaoshi.entities.Notice;
import top.malaoshi.json.CommonResult;

import javax.annotation.Resource;

@RestController
@Slf4j
public class FollowController {

    @Resource
    private NoticeClient noticeClient;

    @PostMapping(value="/follow")
    public CommonResult follow(Notice notice){

        return noticeClient.sendNotice(notice);
    }

    @GetMapping(value="/getNotice/{id}")
    public CommonResult getNotice(@PathVariable("id") String id){
        log.info("---"+id);
        return noticeClient.getNoticeById(id);
    }
}