SpringCloud系列-服务消费(Feign)(四)

Spring Cloud Feign简介

Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端。

spring cloud对微服务的调用进行了组件化,用feigin组件进行微服务调用。 
feign是一个声明式的WebService客户端,它使得编写Web服务客户端变得更加简单。使用Feign能让编写WebService更加简单,它的使用方法是定义一个http接口,然后在接口上添加注解(它具备可插拔的注解支持),同时也支持JAX-RS标准的注解。feign也支持可插拔式的编码器和解码器。spring cloud对feign进行了封装,使其支持springMVC标注注解和HttpMessageConverters。feign可以与eureka和ribbon组合使用以支持负载均衡的HTTP客户端实现

可以简单的说:Feign是一个超级方便的调用Spring-Cloud远程服务的框架/工具,帮助开发者以更少耦合更少代码更快更兼容的方法进行远程服务调用。

实战开干

利用之前构建的eureka-server作为服务注册中心、eureka-client作为服务提供者作为基础。

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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.scp</groupId>
    <artifactId>eureka-consumer-feign</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>eureka-consumer-feign</name>
    <description>Spring Cloud Feign</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application 

通过@EnableFeignClients注解开启扫描Spring Cloud Feign客户端的功能

package com.scp;

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;

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		new SpringApplicationBuilder(Application.class).web(true).run(args);
	}

}

DcClient

创建一个Feign的客户端接口定义。使用@FeignClient注解来指定这个接口所要调用的服务名称,接口中定义的各个函数使用Spring MVC的注解就可以来绑定服务提供方的REST接口,比如下面就是绑定eureka-client服务的/dc接口的例子:

package com.scp;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

/**
 */
@FeignClient("eureka-client")
public interface DcClient {

    @GetMapping("/dc")
    String consumer();

}

DcController

通过定义的feign客户端来调用服务提供方的接口

package com.scp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 */
@RestController
public class DcController {

    @Autowired
    DcClient dcClient;

    @GetMapping("/consumer")
    public String dc() {
        return dcClient.consumer();
    }

}

application.yml

spring:
   application:
    name: eureka-consumer
server:
  port: 2101
eureka:
  client:
    service-url:
         defaultZone: http://localhost:1001/eureka/
logging:
  file: ${spring.application.name}.log



#server.tomcat.port-header=
#server.port=2101
#
#eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
#
#logging.file=${spring.application.name}.log

通过Spring Cloud Feign来实现服务调用的方式更加简单了,通过@FeignClient定义的接口来统一的生命我们需要依赖的微服务接口。而在具体使用的时候就跟调用本地方法一点的进行调用即可。由于Feign是基于Ribbon实现的,所以它自带了客户端负载均衡功能,也可以通过Ribbon的IRule进行策略扩展。另外,Feign还整合的Hystrix来实现服务的容错保护

将eureka-server、eureka-client、eureka-consumer-feign都启动起来,然后访问http://localhost:2101/consumer

来跟踪观察eureka-consumer-feign服务是如何消费eureka-client服务的/dc接口的,并且也可以通过启动多个eureka-client服务来观察其负载均衡的效果。

http://localhost:1001/

注册调用流程图

参考:https://github.com/dyc87112/SpringCloud-Learning

猜你喜欢

转载自blog.csdn.net/wypersist/article/details/81165156