Spring Cloud:服务消费(Feign)【Dalston版】

Spring Cloud Feign

Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端。它使得编写Web服务客户端变得更加简单。咱们只须要经过建立接口并用注解来配置它既可完成对Web服务接口的绑定。它具有可插拔的注解支持,包括Feign注解、JAX-RS注解。它也支持可插拔的编码器和解码器。Spring Cloud Feign还扩展了对Spring MVC注解的支持,同时还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。web

实战:spring

利用以前博客中构建的eureka-server做为服务注册中心、eureka-client做为服务提供者做为基础apache

构建项目,命名为:eureka-consumer-ribbon。在pom.xml中增长下面的依赖:app

<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>springCloud</groupId>
    <artifactId>eureka-consumer-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>负载均衡

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>maven

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</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>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</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>
    <profiles>
        <profile>
            <id>jdk-1.8</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
            </activation>
            <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
            </properties>
        </profile>
    </profiles>
</project>spring-boot

资源文件application.properties微服务

spring.application.name=eureka-feign
server.port=2102测试

eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

修改应用主类。经过@EnableFeignClients注解开启扫描Spring Cloud Feign客户端的功

package com.cloud;

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;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ApplicationFeign {

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

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

package com.cloud.service;

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

@FeignClient("eureka-client")
public interface DcClient {
    @GetMapping("/dc")
    String consumer();
}
修改Controller。经过定义的feign客户端来调用服务提供方的接口

package com.cloud.controller;

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

import com.cloud.service.DcClient;

@RestController
public class DcController {
    @Autowired
    DcClient dcClient;

    @GetMapping("/consumer")
    public String dc() {
        return dcClient.consumer();
    }
}
经过Spring Cloud Feign来实现服务调用的方式更加简单了,经过@FeignClient定义的接口来统一的生命咱们须要依赖的微服务接口。而在具体使用的时候就跟调用本地方法一点的进行调用便可。因为Feign是基于Ribbon实现的,因此它自带了客户端负载均衡功能,也能够经过Ribbon的IRule进行策略扩展。另外,Feign还整合的Hystrix来实现服务的容错保护

能够在建立一个客户端其名称eureka-client,修改端口号,资源文件入下

spring.application.name=eureka-client
server.port=2002
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/,

启动2个客户端,能够测试负载均衡。

相关文章
相关标签/搜索