Spring Cloud 微服务架构学习

Spring Cloud构建微服务架构之服务消费(三)

2018-07-02  本文已影响0人  SnowZhang

上一篇Spring Cloud构建微服务架构之服务消费(二)介绍了如何使用Ribbon来消费微服务提供的接口。本文我们将继续介绍Spring Cloud中的另外一个服务消费的工具:Spring Cloud Feign。

Spring Cloud Feign

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

下面的代码将演示如何使用feign来消费微服务提供的接口

<?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">
    <parent>
        <artifactId>services</artifactId>
        <groupId>spring-cloud-demos</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>customer-service-feign</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.7.RELEASE</version>
        </dependency>
        <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>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
package com.snow.spring.cloud;

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

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeignApplication {
    public static void main(String[] args){
        SpringApplication.run(FeignApplication.class,args);
    }
}

package com.snow.spring.cloud.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;

@FeignClient("biz-provider-service1")
@Component
public interface SP1Client {
    @PostMapping("/getUserInfo")
    String getInfo(String userName);
}
package com.snow.spring.cloud.controller;

import com.snow.spring.cloud.service.SP1Client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CustomerController {

    Logger logger = LoggerFactory.getLogger(CustomerController.class);

    @Autowired
    SP1Client sp1Client;

    @GetMapping("/getUserInfo")
    public String getUserInfo(@RequestParam String userName){
        logger.info("get request:" + userName);
        return sp1Client.getInfo(userName);
    }
}

spring:
  application:
    name: customer-serice-feign
server:
  port: 9002
eureka:
  instance:
    prefer-ip-address: true
  client:
    serviceUrl:
      defaultZone: http://localhost:8762/eureka/

services:
  urls:
    userInfoUrl: http://biz-provider-service1/

启动FeignApplication,然后再浏览器中访问:http://localhost:9002/getUserInfo?userName=snow
我们可以看到,请求到达CustomerController,并通过FeignClient声明的SP1Client接口来调用指定的微服务。刷新浏览器,通过日志我们发现请求也是均匀分发的,FeignClient集成了Ribbon负载均衡。
至此,FeignClient的方式消费微服务提供的接口演示完毕。
作者水平有限,说的不好的地方还请包含。

文章中所有的实例代码均可以在下面的连接上获取参考。谢谢。
码市:spring-cloud-demos

上一篇下一篇

猜你喜欢

热点阅读