• 【SpringCloud】第三篇: 服务消费者(Feign)


    前言:

    必需学会SpringBoot基础知识

    简介:

    spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑。

    工具:

    JDK8

    apache-maven-3.5.2

    IntelliJ IDEA 2017.3 x64

    一、Feign简介

    Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。

    简而言之:

                     Feign 采用的是基于接口的注解

                     Feign 整合了ribbon

    二、准备工作

    1. 运行 eureka-server:8761;

    2. 运行 eureka-client 两次,端口分别为8762 、8773

    三、创建一个feign的服务

    3.1 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.lwc</groupId>
    	<artifactId>eureka-feign</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    
    	<name>eureka-feign</name>
    	<description>Demo project for Spring Boot</description>
    
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>1.5.10.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.SR2</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-server</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-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>

    3.2 通过 application.yml 跳转 application-dev.yml 配置程序名, 端口 和指定注册中心 如下:

    spring:
      application:
          name: eureka-feign
      profiles:
        active: ${@profileActiv@:dev}
    
    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8761/eureka/
    server:
      port: 8765
    

    3.3 启动类添加@EnableFeignClients注解开启Feign的功能:

    package com.lwc;
    
    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;
    
    /**
     * @author Eddie
     */
    @EnableFeignClients
    @EnableDiscoveryClient
    @SpringBootApplication
    public class EurekaFeignApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaFeignApplication.class, args);
        }
    }
    
    

    3.4 定义一个feign接口,通过@ FeignClient(“服务名”), 通过服务名找对应服务 如下:

    package com.lwc.service;
    
    import org.springframework.cloud.netflix.feign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    /**
     * @author eddie.lee
     * @Package com.lwc.service
     * @ClassName FeignClientService
     * @description
     * @date created in 2018-03-27 10:43
     * @modified by
     */
    @FeignClient("eureka-client")
    public interface FeignClientService {
    
        @GetMapping("/eureka/client")
        String feignToClientByName(@RequestParam("name") String name);
    
    }

    3.5 创建controller层,通过API接口,来消费Feign客户端服务. 如下:

    package com.lwc.controller;
    
    import com.lwc.service.FeignClientService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author eddie.lee
     * @Package com.lwc.controller
     * @ClassName FeignController
     * @description 服务消费者(Feign)
     * @date created in 2018-03-27 10:50
     * @modified by
     */
    @RestController
    @RequestMapping("/eureka")
    public class FeignController {
    
        @Autowired
        private FeignClientService feignClientService;
    
        @GetMapping("/feign")
        public String feign(@RequestParam("name") String name) {
            return feignClientService.feignToClientByName(name);
        }
    
    }

    四、参考资料

    eureka-feign
    http://localhost:8765/eureka/feign?name=eddie

    
    

    标签

    3-1

    源码下载
    https://github.com/eddie-code/SpringCloudDemo#springclouddemo

    疑惑: eureka-ribbon, eureka-feign 两个功能相同, 区别在哪里?

    答: feign是基于ribbon衍生出来, feign采用接口方式, 更简洁;

  • 相关阅读:
    innodb buffer pool简介
    按 host 分组统计视图
    show engine innodb status事务模块简介
    等待事件统计视图
    MySQL MGR新增成员xtrabackup
    按 user 分组统计视图
    按 file 分组统计视图
    jdk9下载
    idea怎样自动补全分号
    idea创建的项目中,没有out文件
  • 原文地址:https://www.cnblogs.com/EddieBlog/p/8657073.html
Copyright © 2020-2023  润新知