zoukankan      html  css  js  c++  java
  • 08 在Spring Cloud中使用Feign

      Spring Cloud对Feign进行了封装,本例将演示如何在Spring Cloud中使用Feign。

    1.准备SpringCloud的测试项目

      测试项目主要有三个,

      a.spring-feign-server: Eureka的服务器项目,端口为8761;

      b.spring-feign-provider: 服务提供者,该项目通过在控制台输入对应的端口号启动两个实例,分别是8080和8081端口的实例,这两个实例提供如下服务

       第一个地址为person/{personId}的服务,请求后返回Person实例,Person的message属性请求的是URL。

      c.spring-feign-invoker: 服务调用者,该项目对外端口是9000,本例主要是在该项目下使用Feign。

      由于Eureka服务器项目以及服务提供者项目不是本文的重点,此处不做详细介绍,详情可参考前面的博文。本文将重点介绍服务调用者项目,项目的目录结构如下

     

    2.Spring Cloud中整合Feign

      在服务调用者(spring-feign-invoker)的pom.xml文件中引入相关依赖,代码清单如下

      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.triheart</groupId>
        <artifactId>springfeigninvoker</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Dalston.SR1</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-ribbon</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-feign</artifactId>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.16.18</version>
            </dependency>
        </dependencies>
        
    </project>
    View Code

      在服务调用者启动类中打开Feign开关,代码清单如下

      InvokerApp.java

    package com.triheart.firstekserviceinvoker;
    
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.netflix.feign.EnableFeignClients;
    
    /**
     * @author 阿遠
     * Date: 2018/8/23
     * Time: 17:51
     */
    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableFeignClients
    public class InvokerApp {
    
        public static void main(String[] args) {
            new SpringApplicationBuilder(InvokerApp.class).run(args);
        }
    }

      接下来,编写客户端接口,与单独直接使用Feign类似,代码清单如下

      PersonClient.java

    package com.triheart.firstekserviceinvoker;
    
    import org.springframework.cloud.netflix.feign.FeignClient;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    /**
     * @author 阿遠
     * Date: 2018/8/29
     * Time: 9:46
     */
    @FeignClient("spring-feign-provider") //调用服务提供者的服务名称
    public interface PersonClient {
        @RequestMapping(method = RequestMethod.GET, value = "/hello")
        String hello();
    }

      与单独使用Feign不同的是,接口使用了@FeignClient注解来修饰,并且声明了需要调用的服务名称。另外接口方法使用了@RequestMapping来修饰,这是因为Feign的“翻译器”的功能可以让Feign知道第三方注解的含义,Spring Cloud也提供翻译器,将@RequestMapping注解的含义告知Feign,因此我们的服务接口就可以直接使用该注解。除了该注解外,默认还支持@RequestParam、@RequestHeader、@PathVariable这三个参数注解。需要注意的是,使用Spring Cloud的“翻译器”后,将不能再使用Feign的默认注解。接下来,在控制器中调用接口方法,代码清单如下

    package com.triheart.firstekserviceinvoker;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author 阿遠
     * Date: 2018/8/23
     * Time: 17:56
     */
    @RestController
    @Configuration
    public class InvokerController {
    
        @Autowired
        private PersonClient personClient;
    
        @RequestMapping(value = "/invokeHello", method = RequestMethod.GET)
        public String invokeHello() {
            return personClient.hello();
        }
    }

      在控制器在中,注入了PersonClient的Bean,不难看出,客户端实例的创建及维护,Spring容器都帮我们实现了。

      启动Eureka服务器。

      启动两个服务提供者,分别在8080和8081端口启动。

      启动服务调用者,端口为9000。

      在浏览器中输入http://localhost:9000/invokeHello,我们可以看到,服务提供者的/hello服务已经被调用。

    3.Feign负载均衡

       我们知道,如果结合Ribbon的使用,Spring Cloud的客户端就会拥有负载均衡的功能,因此,Spring Cloud实现的Feign客户端也拥有负载均衡的功能。下面我们测试其负载均衡功能

      在PersonClient .java接口中添加如下代码

     @RequestMapping(method = RequestMethod.GET, value = "/person/{personId}")
        Person getPerson(@PathVariable("personId") Integer personId);

      在InvokerController.java中添加如下代码

     @RequestMapping(value = "/router", method = RequestMethod.GET,
                produces = MediaType.APPLICATION_JSON_VALUE)
        public String router() {
            // 调用服务提供者的接口
            Person p = personClient.getPerson(2);
            return p.getMessage();

       重新启动服务调用者,在浏览器中输入http://localhost:9000/router,刷新,可以看到8080与8081端口被循环调用。

  • 相关阅读:
    channel分析
    Nginx|基础
    item2
    搜索引擎技巧
    计算机网络|概述
    操作系统|进程
    分布式事务一致性
    画图工具StartUML
    内存分配
    MPG分析
  • 原文地址:https://www.cnblogs.com/a-yuan/p/9552941.html
Copyright © 2011-2022 走看看