第十节 2.3.7版本的OpenFeign使用

亮子 2022-07-05 07:41:24 17271 0 0 0

1、添加依赖

  • 指定版本
        <!-- Feign Client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
  • 系统自动
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
        <spring-cloud-alibaba.version>2.2.2.RELEASE</spring-cloud-alibaba.version>
        <spring-cloud.version>Hoxton.SR9</spring-cloud.version>
    </properties>

    <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>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

图片alt

2、启动类上添加注解

@SpringBootApplication
@EnableFeignClients
public class ServerShopWareApplication {

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

}

3、定义接口

package com.shenmazong.ware.service;


import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.MessageVo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@FeignClient(value = "server-shop-mq")
@Component
public interface MessageService {

    @PostMapping(value = "/message/sendMessage")
    ResultResponse sendMessage(@RequestBody MessageVo messageVo);

}

4、写一个controller

@RestController
@Slf4j
@RequestMapping(value = "/msg")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @PostMapping(value = "/msg")
    public ResultResponse test(@RequestParam("msg") String msg) {
        MessageVo messageVo = new MessageVo();
        messageVo.setId(IdWorker.getId());
        messageVo.setType(1);
        messageVo.setData(msg);
        return messageService.sendMessage(messageVo);
    }
}