美文网首页Spring Cloud
声明式客户端OpenFeign(一):基础应用

声明式客户端OpenFeign(一):基础应用

作者: JBryan | 来源:发表于2020-07-05 16:12 被阅读0次

    OpenFeign简介

    OpenFeign是一个声明式RESTful网络请求客户端。OpenFeign会根据带有注解的函数信息构建出网络请求的模板,在发送网络请求之前,OpenFeign会将函数的参数值设置到这些请求模板中。虽然OpenFeign只能支持基于文本的网络请求,但是它可以极大简化网络请求的实现,方便编程人员快速构建自己的网络请求应用。

    如图所示,使用OpenFeign的Spring应用架构一般分为三个部分,分别为服务注册中心、服务提供者和服务消费者。服务提供者向服务注册中心注册自己,然后服务消费者通过OpenFeign发送请求时,OpenFeign会向服务注册中心获取关于服务提供者的信息,然后再向服务提供者发送网络请求。


    OpenFeign架构.jpg

    代码示例

    1. 服务注册中心control-eureka-server

    OpenFeign可以配合Eureka等服务注册中心同时使用。Eureka作为服务注册中心,为OpenFeign提供服务端信息的获取,比如说服务的IP地址和端口。

    参见:https://www.jianshu.com/p/9223970fc59c

    2. 服务提供者control-user

    Spring Cloud OpenFeign是声明式RESTful网络请求客户端,所以对服务提供者的实现方式没有任何影响。也就是说,服务提供者只需要提供对外的网络请求接口就可,至于其具体实现既可以使用Spring MVC,也可以使用Jersey。只需要确保该服务提供者被注册到服务注册中心上即可。

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.6.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.ljessie</groupId>
        <artifactId>control-user</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>control-user</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Hoxton.SR4</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    
        <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>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    application.yml

    eureka:
      instance:
        instance-id: server:1
      client:
        service-url: 
          defaultZone: http://localhost:8761/eureka/
    server:
      port: 8762
    spring:
      application:
        name: userServer
    

    TestController

    @RestController
    @RequestMapping("test")
    public class TestController {
        @RequestMapping("test_feign")
        public String testFeign(String param){
            return "Hello "+param;
        }
    }
    
    3. 服务消费者control-personnel

    pom.xml文件中添加Eureka和OpenFeign相关的依赖。

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.7.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <groupId>com.ljessie</groupId>
        <artifactId>control-personnel</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>control-personnel</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Hoxton.SR4</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency> 
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    
        <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>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    

    application.yml

    eureka:
      instance:
        hostname: personnel
        instance-id: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/
    server:
      port: 8764
    spring:
      application:
        name: personnelServer
    

    入口类ControlPersonnelApplication添加@EnableFeignClients注解

    package com.ljessie.controlpersonnel;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    @SpringBootApplication
    @EnableFeignClients
    public class ControlPersonnelApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ControlPersonnelApplication.class, args);
        }
    
    }
    

    新建UserService接口访问消费提供者,类上面添加@FeignClient注解,参数为服务提供者application.yml里面注册的spring.application.name,RequestMapping参数即为服务提供者开放接口的路径。

    package com.ljessie.controlpersonnel.service;
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    @FeignClient("userServer")
    public interface UserService {
    
        @RequestMapping("test/test_feign")
        String testFeign(@RequestParam("param")String param);
    }
    
    

    新建TestController,对外提供测试接口:

    package com.ljessie.controlpersonnel.controller;
    
    import com.ljessie.controlpersonnel.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("test")
    public class TestController {
    
        @Autowired
        UserService userService;
    
        @RequestMapping("test_feign")
        public String testFeign(String param){
            return userService.testFeign(param);
        }
    
    }
    

    依次启动注册中心control-eureka-server,服务提供者control-user,服务消费者control-personnel。

    访问http://localhost:8764/test/test_feign?param=Jessie

    测试结果.jpg

    相关文章

      网友评论

        本文标题:声明式客户端OpenFeign(一):基础应用

        本文链接:https://www.haomeiwen.com/subject/rgevqktx.html