转载

SpringCloud(六):服务消费者Feign 上

[TOCM]

[TOC]

源码地址:https://github.com/IsResultXaL/springcloud

通过上一节内容介绍与实践,我们已经搭建起微服务架构中的核心组件

  • 服务注册中心

  • 服务提供者

  • 服务消费者

  • 断路器Hystrix

  • Hystrix仪表板

  • Turbine集群监控

SpringCloud(五):Turbine集群监控

以下Model基于上一节项目新增

###Spring Cloud Feign

Feign是一个声明式Web Service客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。

  • 创建一个model项目(feign-consumer)

  • feign-consumer项目的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.caogen</groupId>
      	<artifactId>feign-consumer</artifactId>
      	<version>0.0.1-SNAPSHOT</version>
      	<packaging>jar</packaging>
    
      	<name>feign-consumer</name>
      	<description>Demo project for Spring Boot</description>
    
      	<parent>
      		<groupId>org.springframework.boot</groupId>
      		<artifactId>spring-boot-starter-parent</artifactId>
      		<version>1.5.9.RELEASE</version>
      		<relativePath/> <!-- lookup parent from repository -->
      	</parent>
    
      	<properties>
      		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      		<java.version>1.8</java.version>
      		<spring-cloud.version>Edgware.RELEASE</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-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-actuator</artifactId>
      		</dependency>
    
      		<dependency>
      			<groupId>org.springframework.boot</groupId>
      			<artifactId>spring-boot-starter-test</artifactId>
      			<scope>test</scope>
      		</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>
  • 在应用主类加上@EnableFeignClients注解,开启Feign的支持功能。

    package com.caogen.feignconsumer;
    
      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			//开启Feign的支持功能
      @EnableDiscoveryClient    	//表明该应用是Eureka客户端
      @SpringBootApplication
      public class FeignConsumerApplication {
    
      	public static void main(String[] args) {
      		SpringApplication.run(FeignConsumerApplication.class, args);
      	}
      }
  • 配置application.yml

    # 端口设置
      server:
        port: 0
    
      # 应用名称设置
      spring:
        application:
      	name: feign-consumer
    
      # 注册中心地址
      eureka:
        client:
      	serviceUrl:
      	  defaultZone: http://localhost:1111/eureka/
        instance:
      	  instance-id: ${spring.application.name}:${random.int[10000,19999]}    # 随机数配置实例ID
    
      #开启hystrix(要不然降级失败)
      feign:
        hystrix:
      	enabled: true
  • 添加ConsumerService接口,通过@FeignClient注解指定服务名来绑定服务,然后再使用Spring MVC的注解来绑定服务提供的REST接口

    package com.caogen.feignconsumer.service;
    
      import org.springframework.cloud.netflix.feign.FeignClient;
      import org.springframework.web.bind.annotation.*;
    
      @FeignClient(name = "eureka-client")   //通过@FeignClient注解指定服务名来绑定服务
      public interface ConsumerService {
    
      	@RequestMapping(value = "/hello", method = {RequestMethod.GET})   //绑定REAT接口
      	String hello();
    
      }
  • 添加一个ConsumerController控制器来实现对Feign客户端的调用

    package com.caogen.feignconsumer.controller;
    
      import com.caogen.feignconsumer.service.ConsumerService;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.RestController;
    
      import javax.annotation.Resource;
    
      @RestController
      public class ConsumerController {
    
      	@Resource
      	ConsumerService consumerService;
    
      	@GetMapping(value = "/feign-consumer")
      	public String helloConsumer() {
      		return consumerService.hello();
      	}
    
      }
  • 启动以下应用

    • 服务注册中心 eureka-server 端口 1111

    • 服务提供实例 eureka-client 端口 1112 和 1113

    • 服务消费实例 feign-consumer 端口 1115

SpringCloud(六):服务消费者Feign 上

发送几次请求到 http://localhost:1115/feign-consumer ,可以得到如之前Ribbon实现一样的效果,正确返回

Hello,World! port:1112

Hello,World! port:1113

我们可以看到Feign实现的消费者,是利用Ribbon维护了针对eureka-client的服务列表信息,并且通过轮询实现了客户端负载均衡。不过Feign只需要定义服务绑定接口,以声明式的方法,更优雅简单的实现了服务调用。

原文  https://kcaogen.com/blog/info/25
正文到此结束
Loading...