Spring Cloud feign使用


  • 前言
  • 环境准备
  • 应用模块
  • 应用程序
  • 应用启动
  • feign特性
  • 综上

1. 前言


我们在前一篇文章中讲了一些我使用过的一些http的框架 
服务间通信之Http框架,其实最终还是准备讲述spring cloud fegin,使用spring cloud fegin完成更为优雅的http的调用方式,以及在服务之间的调用与远程调用的注意上,还有一些使用的问题。

2. 环境准备


这回搭建的一个完整的应用不再新建maven工程,改用gradle,spring boot工程推荐的还是使用gradle来构建,并且gradle相对于maven来说优势较多,应用的也越来越广,因此这回尝试一下,使用一回gradle构建spring cloud应用,具体采用的环境、工具等如下:

  • intellij 2016: 编写java程序。

  • jdk8 : 采用java8来编写应用,可以使用java8中一些特性编写更为优雅的代码和运行更为高效的程序。

  • gradle: 用于我们工程的构建、依赖管理、工程打包等等。

3. 应用模块


采用feign来进行服务之间的调用,一般都是需要一个注册中心,这回就采用eureka作为注册中心,以供于两个服务进行服务注册和服务发现,总体服务列别如下:

  • eureka-server: 服务注册中心,用于服务注册和服务发现。

  • service-a: a服务,服务消费者。

  • service-b: b服务,服务提供者。

4. 应用程序


gradle工程中的build.gradle等一些脚本不再展示出来,在这仅仅展示一下工程结构,如下:

.
├── build.gradle
├── eureka-server
│   ├── build.gradle
│   └── src
│   └── main
│   ├── java
│   │   └── cn
│   │   └── com
│   │   └── enreka
│   │   └── EurekaServerApplication.java
│   └── resources
│   └── bootstrap.yml
├── gradlew
├── gradlew.bat
├── service-a
│   ├── build.gradle
│   └── src
│   └── main
│   ├── java
│   │   └── cn
│   │   └── com
│   │   └── devh
│   │   ├── A1ServiceApplication.java
│   │   ├── controllers
│   │   │   └── AServiceController.java
│   │   └── fegin
│   │   └── ServiceBClient.java
│   └── resources
│   ├── application.yml
│   └── bootstrap.yml
├── service-b
│   ├── build.gradle
│   └── src
│   └── main
│   ├── java
│   │   └── cn
│   │   └── com
│   │   └── devh
│   │   ├── B1ServiceApplication.java
│   │   └── controllers
│   │   └── ServiceB1Controller.java
│   └── resources
│   ├── application.yml
│   └── bootstrap.yml
├── settings.gradle
└── zuul
├── build.gradle
└── src
└── main
├── java
│   └── cn
│   └── com
│   └── zuul
│   └── ZuulApplication.java
└── resources

工程结构大致和maven项目中差不多,zuul是api网关模块,这节暂时不讲,留在后期进行讲述。

4.1 eureka-server


eureka server采用的是无中心化的架构,无master/slave区分,每一个server都是对等的,既是Server又是Client,所以其集群方式可以自由发挥,可以各点互连,也可以接力互连采用eureka作为注册中心,在这里一个简单的应用中,我就采用了一个节点当做注册中心,eureka集群可以看看我先前写的这一篇文章Eureka的高可用以及服务提供者、服务消费者集群之间的调用方式,但是eureka作为注册中心也是存在着许多问题的,在随后的文章中进行讲述,同时也讲述zookeeper、etcd、consul和eureka之间的优劣势,话不多说,还是回到eureka server服务上来,单节点的eureka使用还是比较简单的,程序如下:

bootstrap.yml:

server:
port: 8761 spring:
application:
name: eureka-server eureka:
instance:
hostname: localhost
lease-expiration-duration-in-seconds: 30
lease-renewal-interval-in-seconds: 30
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
enable-self-preservation: false

EurekaServerApplication:

package cn.com.enreka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /**
* Created by xiaxuan on 17/8/27.
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication { public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}

如此,一个简单的eureka就编写完成了,到时直接启动主程序,注册中心就可以正常启动起来。

4.2 服务提供者service-b


service-b是我们的服务提供者,应用比较简单,仅仅是提供一个一个接口以供消费者调用,具体应用程序如下:

配置文件bootstrap.yml:

server:
port: 8070 spring:
application:
name: service-b eureka:
instance:
hostname: localhost
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/

配置文件application.yml:

msg: Hello

配置文件bootstrap.yml和application.yml都可以用来配置参数, 
bootstrap.yml可以理解成系统级别的一些参数配置,这些参数一般是不会变动的。 
application.yml 可以用来定义应用级别的,如果搭配spring-cloud-config使用 application.yml里面定义的文件可以实现动态替换。spring-cloud-config在后期会进行一些讲述,到时再提一下另外一套配置中心,携程的apoll,挺不错。

B1ServiceApplication:

package cn.com.devh;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /**
* Created by xiaxuan on 17/8/26.
*/
@SpringBootApplication
@EnableDiscoveryClient
public class B1ServiceApplication { public static void main(String[] args) {
SpringApplication.run(B1ServiceApplication.class, args);
}
}

和普通的spring boot启动程序没有太多的不同,仅仅只是加了一个@EnableDiscoveryClient注解,便于服务注册和服务发现,同时还可以使用的是另外一个注解,为@EnableEurekaClient,两个的用法基本相同,下次有空讲讲另外一个注解的用途。

提供服务的controller,ServiceB1Controller:

package cn.com.devh.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; /**
* Created by xiaxuan on 17/8/28.
*/
@RestController
public class ServiceB1Controller { @Autowired
DiscoveryClient discoveryClient; @Value("${msg:unknown}")
private String msg; @RequestMapping(value = "/", method = RequestMethod.GET)
public String printServiceB() {
ServiceInstance serviceInstance = discoveryClient.getLocalServiceInstance();
return serviceInstance.getServiceId() + " (" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + ")" + "===>Say " + msg;
}
}

提供的接口也比较简单,就是返回当前的serviceId、host、port和一条hello语句。

4.3 服务消费者service-a


service-a是服务消费者,大致和service-b相同,但是还包括feign来调用server-b提供的服务,配置文件不再展示出来,最后会有整体的代码的下载路径,其他具体代码如下:

A1ServiceApplication:

package cn.com.devh;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /**
* Created by xiaxuan on 17/8/25.
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class A1ServiceApplication { public static void main(String[] args) {
SpringApplication.run(A1ServiceApplication.class, args);
}
}

启动程序与与service-b没有区别,都是加上了一个注解@EnableDiscoveryClient用于服务注册和服务发现使用,然后还单独加上了一个注解@EnableFeignClients,这个注解就是确保feign可以正常使用。

ServiceBClient:

package cn.com.devh.fegin;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; /**
* Created by xiaxuan on 17/8/26.
*/
@FeignClient(name = "service-b")
public interface ServiceBClient { @RequestMapping(value = "/", method = RequestMethod.GET)
String printServiceB();
}

fallback

在service层上实现接口,这里注意value可以用serviceId代替,但是最好用value来指定要调用的服务。

fallback是当程序错误的时候来回调的方法

方法中要用@PathVariable要注解参数

1 @FeignClient(value = "hap-user-admin-service", fallback = OrganizationLabelFeignClientFallback.class)
2 public interface OrganizationLabelFeignClient {
3 @RequestMapping(value = "/v1/organizations/{id}",method = RequestMethod.GET)
4 Organization queryOrgLabel(@PathVariable(name="id") Long id);
5 }

编写程序错误时的回调类,实现接口,在错误时回调

1 @Service
2 public class OrganizationLabelFeignClientFallback implements OrganizationLabelFeignClient {
3 @Override
4 public Organization queryOrgLabel(Long id) {
5 return null;
6 }
7 }

对service-b编写的feignClient,到时用来直接调用service-b提供的服务,而之所以能够正常调用到service-b的服务,就是通过feign中的name字段,会通过注册中心找到对应的服务。

controller,AServiceController:

package cn.com.devh.controllers;

import cn.com.devh.fegin.ServiceBClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* Created by xiaxuan on 17/8/26.
*/
@RestController
public class AServiceController { @Value("${name:unknown}")
private String name; @Autowired
private ServiceBClient serviceBClient; @Autowired
private DiscoveryClient discoveryClient; @RequestMapping("/")
public String printServiceA() {
ServiceInstance serviceInstance = discoveryClient.getLocalServiceInstance();
return serviceInstance.getServiceId() + " (" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + ")" + "===>name:" + name + "<br/>" + serviceBClient.printServiceB();
}
}

提供的服务也是比较简单,输出当前服务在注册中心中的serviceId、host、port和使用feign调用service-b的服务。

以上就是我们整个应用中的三个服务,注册中心eureka-server,服务提供者service-b,服务消费者service-a.

5. 应用启动


分别启动三个服务,注册中心eureka-server,服务提供者service-b,服务消费者service-a,在浏览器中输入http://localhost:8761,观察服务状况,如下图:

两个服务都正常启动,现在直接在浏览器中输入http://localhost:8080/,调用service-a提供的接口,结果如下图:

成功输出当前服务在注册中心的信息并且成功调用service-b提供的服务。

6. feign特性


在这个简单的应用中,service-a通过feign调用service-b的服务,并输出service-a和service-b在注册中心中的信息,整个应用编写的还是比较简单,而且feign还有许多其他有意思的特性。

feign不仅有一个name属性,还有一个url属性,如果指定name属性的话,会直接调用在注册中心中注册的本地服务,如果是还指定了url属性的话,就可以直接调用远程的非注册中心的服务,这样在调用其他服务的时候就非常方便,只要url相同,方法签名中的参数相同,就可以成功的调用对方的服务。

同时feign还可以设置断路由,如果服务调用失败的话,可以调用本地写的failback方法,返回一些默认信息或者抛错之类,给了非常灵活的自由度。

另外feign还有重试次数、超时设置、更换底层使用的httpClient框架等等,都非常有意思,有兴趣的可以google看看。

Spring Cloud feign的更多相关文章

  1. 笔记:Spring Cloud Feign Ribbon 配置

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

  2. 笔记:Spring Cloud Feign Hystrix 配置

    在 Spring Cloud Feign 中,除了引入了用户客户端负载均衡的 Spring Cloud Ribbon 之外,还引入了服务保护与容错的工具 Hystrix,默认情况下,Spring Cl ...

  3. 笔记:Spring Cloud Feign 其他配置

    请求压缩 Spring Cloud Feign 支持对请求与响应进行GZIP压缩,以减少通信过程中的性能损耗,我们只需要通过下面二个参数设置,就能开启请求与响应的压缩功能,yml配置格式如下: fei ...

  4. 笔记:Spring Cloud Feign 声明式服务调用

    在实际开发中,对于服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以我们通常会针对各个微服务自行封装一些客户端类来包装这些依赖服务的调用,Spring Cloud Feign 在此基础上做了进 ...

  5. 第六章:声明式服务调用:Spring Cloud Feign

    Spring Cloud Feign 是基于 Netflix Feign 实现的,整合了 Spring Cloud Ribbon 和 Spring Cloud Hystrix,除了提供这两者的强大功能 ...

  6. Spring Cloud Feign Ribbon 配置

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

  7. 微服务架构之spring cloud feign

    在spring cloud ribbon中我们用RestTemplate实现了服务调用,可以看到我们还是需要配置服务名称,调用的方法 等等,其实spring cloud提供了更优雅的服务调用方式,就是 ...

  8. Spring Cloud Feign 在调用接口类上,配置熔断 fallback后,输出异常

    Spring Cloud Feign 在调用接口类上,配置熔断 fallback后,出现请求异常时,会进入熔断处理,但是不会抛出异常信息. 经过以下配置,可以抛出异常: 将原有ErrorEncoder ...

  9. RestTemplate OR Spring Cloud Feign 上传文件

    SpringBoot,通过RestTemplate 或者 Spring Cloud Feign,上传文件(支持多文件上传),服务端接口是MultipartFile接收. 将文件的字节流,放入ByteA ...

随机推荐

  1. HttpClient连接池

    HttpClient连接池,发现对于高并发的请求,效率提升很大.虽然知道是因为建立了长连接,导致请求效率提升,但是对于内部的原理还是不太清楚.后来在网上看到了HTTP协议的发展史,里面提到了一个属性C ...

  2. 《Wrox.Professional.Hadoop.Solutions》中文目录全稿

    前言:最近有朋友给推荐一本书,英文原版<Wrox.Professional.Hadoop.Solutions>,感觉很好打算翻译成中文,共享给朋友,时间关系,不知能否成行,先干着吧.以下部 ...

  3. 胡乱摸的NOIP2017游记和总结

    来自YZK的总结 本篇总结主要分成两部分:NOI Professional游记和平日的刷题训练. 今年的NOI Professional TG的难度在洛谷上标记为:二黄一绿三紫.恭喜NOIP今年全面脱 ...

  4. 操作系统-百科:Kylin (中国自主知识产权操作系统)

    ylbtech-操作系统-百科:Kylin (中国自主知识产权操作系统) Kylin操作系统是国家高技术研究发展计划(863计划)的重大成果之一,是以国防科技大学为主导,与中软.联想等单位联合设计和开 ...

  5. gradle使用心得

    gradle是语言式构建,和maven配置型还是差别挺大,琢磨了2天 1.在解析setting.gradle之后,开始解析build.gradle之前,这里如果要干些事情(更改build.gradle ...

  6. Flex 学习

    Flex案例一: <html> <head> <meta http-equiv="Content-Type" content="text/h ...

  7. 使用V$SQL_PLAN视图获取曾经执行过的SQL语句执行计划

    通常我们查看SQL语句的执行计划都是通过EXPLAIN PLAN或者AUTOTRACE来完成.但是这些查看方法有一个限制,它们都是人为触发而产生的,无法获得数据库系统中曾经执行过的SQL语句执行计划. ...

  8. redis windows dll 下载

    https://pecl.php.net/package/redis http://blog.csdn.net/leesin2011/article/details/72801629 http://w ...

  9. linux 定时

    http://blog.csdn.net/jingxiangren/article/details/4745631

  10. Android SDK的安装与环境配置

    一.Android SDK工具下载.安装 Android SDK工具下载:http://www.androiddevtools.cn/ SDK下载页面如下,由于电脑Windows系统所以下载的Wind ...