一 Feign简介

Feign是一种声明式、模板化的HTTP客户端,也是netflix公司组件。使用feign可以在远程调用另外服务的API,如果调用本地API一样。
我们知道,阿里巴巴的doubbo采用二进制的RPC协议进行底层通讯,客户端可以使用类似本地方法一样调用。那么,虽然Feign同样可以有这种效果,但是底层还是通过HTTP协议调取restful的API的方式。
通过Feign, 我们能把HTTP远程调用对开发者完全透明,得到与调用本地方法一致的编码体验。

在实际开发中,对于服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以我们通常会针对各个微服务自行封装一些客户端类来包装这些依赖服务的调用,Spring Cloud Feign 在此基础上做了进一步的封装,由他来帮助我们定义和实现依赖服务接口的定义,我们只需要创建一个接口并用注解的方式来配置他,即可完成对服务提供方的接口绑定,简化了在使用 Spring Cloud Ribbon 时自行封装服务调用客户端的开发量。

搭建声明式服务Feign(feign-client)

接到上篇“SpringCloud之实现客户端的负载均衡Ribbon(二)

继续在springcloud工程中添加模块feign-client,也是通过start.spring.io提供的模板创建

新的目录

生成的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.xuan</groupId>
<artifactId>feign-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>feign-client</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.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>Finchley.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-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</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>

修改启动文件FeignClientApplication.java,增加相关注解。

package com.xuan.feignclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignClientApplication { public static void main(String[] args) {
SpringApplication.run(FeignClientApplication.class, args);
}
}

增加 @FeignClient 注解的接口来绑定具体的服务,增加服务HelloService.java

package com.xuan.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping; @FeignClient(value = "eureka-client")
public interface HelloService {
@RequestMapping(value = "/hello")
String hello();
}

@FeignClient可以使用name和url来绑定。 以前使用@FeignClient注解的时候使用url参数的使用就不需要使用name属性了,现在不然,需要在url属性的基础上也要使用name属性,此时的name属性只是一个标识。value和name互为别名,只需要设置一个就可以了。

比较有用的四个注解 name , url, fallback , path

  • name 指定微服务的实例名称,唯一,必填,通过实例名称可以得到实例对应的访问地址
  • fallback 配置熔断
  • url 配置一个绝对的地址访问,默认为空字符串,当其不空时,则使用该地址访问
  • path 配置一个所有方法级别的mappings 相当于在类上加 requestMapping, 例如上面的 UserServiceAPI 所有访问地址为 /user/xxx

增加测试的消费接口ConsumerController.java

package com.xuan.feign;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; @RestController
public class ConsumerController {
@Autowired
HelloService helloService; @RequestMapping(value = "feign-consumer", method = RequestMethod.GET)
public String helloConsumer(){
return helloService.hello();
}
}

修改配置文件”application.properties“,找到注册中心和定义自身的服务名和端口,

spring.application.name=feign-consumer
server.port=9991
eureka.client.service-url.defaultZone=http://localhost:8080/eureka/

添加完成后工程的目录结构为

分别启动模块了:

1.EurekaServerApplication

2.EurekaClientApplication,EurekaClientApplication1,EurekaClientApplication2

3.FeignClientApplication

启动后打开http://localhost:8080/显示如图:

访问Feign模块提供的接口http://localhost:9991/feign-consumer,刷新一次也会访问到不同的提供者上面去,原因是feign内部也使用了ribbon做负载均衡。

源码地址:https://gitee.com/xuantest/SpringCloud-Feign

SpringCloud之声明式服务调用 Feign(三)的更多相关文章

  1. SpringCloud 源码系列(6)—— 声明式服务调用 Feign

    SpringCloud 源码系列(1)-- 注册中心 Eureka(上) SpringCloud 源码系列(2)-- 注册中心 Eureka(中) SpringCloud 源码系列(3)-- 注册中心 ...

  2. SpringCloud开发学习总结(七)—— 声明式服务调用Feign(一)

    在实践的过程中,我们会发现在微服务架构中实现客户端负载均衡的服务调用技术Spring Cloud Ribbon<SpringCloud开发学习总结(四)—— 客户端负载均衡Ribbon> ...

  3. 004声明式服务调用Feign & 断路器Hystrix

    1.POM配置 和普通Spring Boot工程相比,添加了Eureka Client.Feign.Hystrix依赖和Spring Cloud依赖管理 <dependencies> &l ...

  4. 【Dalston】【第三章】声明式服务调用(Feign)

    当我们通过RestTemplate调用其它服务的API时,所需要的参数须在请求的URL中进行拼接,如果参数少的话或许我们还可以忍受,一旦有多个参数的话,这时拼接请求字符串就会效率低下,并且显得好傻.那 ...

  5. 声明式服务调用Feign

    什么是 Feign Feign 是种声明式.模板化的 HTTP 客户端(仅在 consumer 中使用).   什么是声明式,有什么作用,解决什么问题? 声明式调用就像调用本地方法一样调用远程方法;无 ...

  6. Spring Cloud第七篇 | 声明式服务调用Feign

    本文是Spring Cloud专栏的第七篇文章,了解前六篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cloud ...

  7. Spring Cloud 声明式服务调用 Feign

    一.简介 在上一篇中,我们介绍注册中心Eureka,但是没有服务注册和服务调用,服务注册和服务调用本来应该在上一章就应该给出例子的,但是我觉得还是和Feign一起讲比较好,因为在实际项目中,都是使用声 ...

  8. Spring Cloud Feign 1(声明式服务调用Feign 简介)

    Spring Cloud Feign基于Netflix Feign 同时整合了Spring Cloud Ribbon和Spring Cloud Hytrix,除了提供两者的强大功能外,它还提供了一种声 ...

  9. SpringCloud开发学习总结(七)—— 声明式服务调用Feign(三)

    Feign中的Ribbon配置 由于Spring Cloud Feign的客户端负载均衡是通过Spring Cloud Ribbon实现的,所以我们可以直接通过配置Ribbon客户端的方式来自定义各个 ...

随机推荐

  1. OSWATCH安装

    .解压安装包 afadb2:/home/oracle$tar -xvf oswbb732.tar (注:先将安装包权限修改为oracle:oinstall,或者对应权限,以ORACLE用户进行解压) ...

  2. 【leetcode】345. Reverse Vowels of a String

    problem 345. Reverse Vowels of a String class Solution { public: string reverseVowels(string s) { , ...

  3. [LeetCode&Python] Problem 824. Goat Latin

    A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and up ...

  4. Arcgis API for JS——打印控件乱码

    在通过Arcgis API for JS编写打印控件进行地图下载时,总发现地图字体乱码,如下图: 解决方法: 在装有ArcGIS Server,要调用服务的电脑或服务器上找到下图文件夹

  5. 05 面向对象:构造方法&static&继承&方法 &final

    构造方法及其重载: /* 构造方法格式特点 * a:方法名与类名相同(大小也要与类名一致) * b:没有返回值类型,连void都没有 * c:没有具体的返回值return; * 构造方法的重载 * 重 ...

  6. (15)javaScript入门

    什么是javaScript HTML用来做页面的架构,CSS用来做页面样式的布局 javaScript就是用来完成页面用户交互的,JS写的就是叫脚本 js就是弱语言类型,不同类型的时候可以相互转换 j ...

  7. 从简单的mongodb example 的观察

    https://github.com/no7dw/mongodb-example 这是最基础的连接查询.(branch master) var MongoClient = require('mongo ...

  8. 【HAOI2014】走出金字塔

    神奇…… 原题: 在探险的过程中,考古学家Dr. Kong 无意地被困在一个金字塔中.金字塔中的每个房间都是三角形.Dr. Kong可以破壁走到相邻的房间去. 例如,如果他目前处于三角形(2,2)房间 ...

  9. Next.js v4.1.4 文档中文翻译【转载】

    最近想稍稍看下 React的 SSR框架 Next.js,因为不想看二手资料, 所以自己跑到 Github上看,Next.js的文档是英文的,看倒是大概也能看得懂, 但有些地方不太确定,而且英文看着毕 ...

  10. c++简单的ATL COM开发和调用实例

    1.打开VS2010.新建ATL COM 项目.步骤:"文件" -->"新建" -->"项目",选择"Visual C ...