SpringCloudNetflix(6)服务消费者(feign)

2020-04-16

Feign 是一个声明式的伪 Http 客户端,它使得写 Http 客户端变得更简单。使用
Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用 Feign 注解和 JAX-RS 注解。Feign
支持可插拔的编码器和解码器。Feign 默认集成了 Ribbon,并和 Eureka 结合,默认实现了负载均衡的效果

  1. Feign 采用的是基于接口的注解
  2. Feign 整合了 ribbon

创建服务消费者

创建一个工程名为 hello-spring-cloud-web-admin-feign 的服务消费者项目,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">
    <!--POM文件的模型版本是4.0.0-->
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>top.misterzhang</groupId>
        <!--继承谁的依赖-->
        <artifactId>dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <!--父级依赖的相对路径-->
        <relativePath>../dependencies/pom.xml</relativePath>
    </parent>

    <!--最好和项目名一样-->
    <artifactId>feign-server</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <!--tomcat的场景启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>


        <!-- 允许使用非严格的 HTML 语法 -->
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
        </dependency>
        <!-- Web场景启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Thymeleaf 自动配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</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-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <!-- 注册到注册中心 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <!--服务消费者feign场景启动器-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>top.misterzhang.feign.server.FeignServerApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

主要是增加了 Feign 的依赖

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

Application

package top.misterzhang.feign.server;

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

/**
 * @author zhangfan
 * @date 2020/4/16
 * @SpringBootApplication       说明是SpringBoot程序
 * @EnableDiscoveryClient       注册到服务中心
 * @EnableFeignClients          开启feign功能
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(FeignServerApplication.class,args);
    }
}

application.yml

spring:
  application:
    #服务名称
    name: feign-server
  thymeleaf:
    # 开发时关闭缓存,不然没法看到实时页面
    cache: false
    # 用非严格的 HTML
    mode: LEGACYHTML5
    #使用UTF-8编码
    encoding: UTF-8
    servlet:
    #写入HTTP响应的Content-Type值
      content-type: text/html

server:
  #端口号
  port: 8765

eureka:
  client:
    serviceUrl:
      #注册到注册中心
      defaultZone: http://localhost:8761/eureka/

创建 Feign 接口

通过 @FeignClient("服务名") 注解来指定调用哪个服务。代码如下:

package top.misterzhang.feign.server.service;

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

/**
 * @author zhangfan
 * @date 2020/4/16
 * @FeignClient("服务名") 注解来指定调用哪个服务
 */
@FeignClient(value = "service-getip")
public interface GetIpService {

    @GetMapping("/get/ip")
    public String getIp(@RequestParam(value = "message") String message);
}

创建测试用的 Controller

package top.misterzhang.feign.server.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.feign.server.service.GetIpService;

/**
 * @author zhangfan
 * @date 2020/4/16
 */
@RestController
public class GetIpController {

    @Autowired
    private GetIpService getIpService;

    @GetMapping("/get/ip")
    public String getIp(String message){
        return getIpService.getIp(message);
    }
}

### 测试访问
在浏览器上多次访问 http://localhost:8765/hi?message=HelloFeign
浏览器交替显示:

Hi,your message is :"HelloFeign" i am from port:8762
Hi,your message is :"HelloFeign" i am from port:8763

请求成功则表示我们已经成功实现了 Feign 功能来访问不同端口的实例

标题:SpringCloudNetflix(6)服务消费者(feign)
作者:张范
地址:http://misterzhang.top/articles/2020/04/09/1586398784057.html