SpringCloudAlibaba(7)openfeign

2020-06-19

创建alibaba-nacos-consumer项目

引入pom依赖

<?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">
    <!--POM文件的模型版本是4.0.0-->
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <!--组名一般是域名反写-->
        <groupId>top.misterzhang</groupId>
        <!--继承父级依赖-->
        <artifactId>alibaba-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../alibaba-dependencies/pom.xml</relativePath>
    </parent>

    <!--和下边的name一样最好是项目名-->
    <artifactId>alibaba-nacos-consumer</artifactId>
    <packaging>jar</packaging>

    <name>alibaba-nacos-consumer</name>
    <url>http://misterzhang.top</url>
    <inceptionYear>2020-Now</inceptionYear>

    <dependencies>
        <!-- SpringBoot项目的web场景启动器 -->
        <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>

        <!--SpringBoot测试场景启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

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

        <!--SpringCloud ailibaba nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!--启动类包全路径-->
                <configuration>
                    <mainClass>top.misterzhang.alibaba.nacos.consumer.NacosConsumerApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

通过 @EnableFeignClients 注解开启 Feign 功能

package top.misterzhang.alibaba.nacos.consumer;

/**
 * @author zhangfan
 * @date 2020/6/19 10:48 上午
 * 本服务为服务消费者
 */

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

/**
 * @SpringBootApplication       SpringBoot启动类
 * @EnableDiscoveryClient       开启Nacos客户端
 * @EnableFeignClients          开启远程调用openfeign
 */

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

创建application.yml

spring:
  application:
    # 服务名字
    name: alibaba-nacos-consumer
  cloud:
    nacos:
      discovery:
        #  nacos服务端地址
        server-addr: IP:8848

server:
  #  服务端口
  port: 9091

management:
  endpoints:
    web:
      exposure:
        #  默认只公开了/health和/info端点,要想暴露所有端点只需设置成星号即可
        include: "*"

创建 Feign 接口

package top.misterzhang.alibaba.nacos.consumer.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @author zhangfan
 * @date 2020/6/19 1:07 下午
 * @FeignClient(value = "调用某个服务的服务名")
 * @GetMapping(value = "调用某个服务的接口地址")
 */
@FeignClient(value = "misterzhang-nacos")
public interface PortService {

    @GetMapping(value = "/get/port{consumer}")
    String getPort(@PathVariable("consumer") String message);
}

Controller

package top.misterzhang.alibaba.nacos.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import top.misterzhang.alibaba.nacos.consumer.feign.PortService;

/**
 * @author zhangfan
 * @date 2020/6/19 1:14 下午
 */
@RestController
public class PortController {
    @Autowired
    private PortService portService;

    @GetMapping("/test")
    public String getProviderPort(String consumer){
        return portService.getPort(consumer);
    }
}

访问接口调用成功


标题:SpringCloudAlibaba(7)openfeign
作者:张范
地址:http://misterzhang.top/articles/2020/06/19/1592544303372.html