Spring Cloud(四)服务提供者 Eureka + 服务消费者 Feign
上一篇文章,讲述了如何通过RestTemplate + Ribbon
去消费服务,这篇文章主要讲述如何通过Feign
去消费服务。
Feign简介
Feign
是一个声明式的伪Http
客户端,它使得写Http
客户端变得更简单。
使用Feign
,只需要创建一个接口并注解,它具有可插拔的注解特性,可使用Feign
注解和JAX-RS
注解,Feign
支持可插拔的编码器和解码器,Feign
默认集成了Ribbon
,
并和Eureka
结合,默认实现了负载均衡的效果。
Feign
具有如下特性:
- 可插拔的注解支持,包括
Feign
注解和JAX-RS
注解 - 支持可插拔的
HTTP
编码器和解码器 - 支持
Hystrix
和它的Fallback
- 支持
Ribbon
的负载均衡 - 支持
HTTP
请求和响应的压缩Feign
是一个声明式的Web Service
客户端,它的目的就是让Web Service
调用更加简单。它整合了Ribbon
和Hystrix
,从而不再需要显式地使用这两个组件。 Feign
还提供了HTTP
请求的模板,通过编写简单的接口和注解,就可以定义好HTTP
请求的参数、格式、地址等信息。接下来,Feign
会完全代理HTTP
的请求,我们只需要像调用方法一样调用它就可以完成服务请求。
简而言之:Feign
能干Ribbon
和Hystrix
的事情,但是要用Ribbon
和Hystrix
自带的注解必须要引入相应的jar
包才可以。
准备工作
Eureka Service
导入第三篇文章中的项目:作为服务注册中心
spring-cloud-eureka-service
Eureka Provider
导入第三篇文章中的项目:作为服务的提供者
spring-cloud-eureka-provider-1
spring-cloud-eureka-provider-2
spring-cloud-eureka-provider-3
Feign Consumer
服务消费者
添加依赖
新建项目 spring-cloud-feign-consumer
pom.xml
中引入需要的依赖内容:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
开启Feign
在工程的启动类中,通过@EnableFeignClients
注解开启Feign的功能:
package com.sc.robbin.consumer; 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 FeignConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(FeignConsumerApplication.class, args);
}
}
定义接口
通过@FeignClient("服务名")
,来指定调用哪个服务。
package com.sc.robbin.consumer; import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
* 描述: 指定这个接口所要调用的 提供者服务名称 "service-hi"
*
* @author
* @create 2019年7月12日 08:16:40
**/
@FeignClient("service-hi")
public interface HomeClient {
@GetMapping("/")
String consumer();
}
消费方法
写一个 Controller
,消费提供者的 home
方法
package com.sc.robbin.consumer; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; /**
* 描述:调用提供者的 `home` 方法
*
* @author
* @create 2017-12-05 18:53
**/
@RestController
public class ConsumerController {
@Autowired
private HomeClient homeClient; @GetMapping(value = "/hello")
public String hello() {
return homeClient.consumer();
}
}
添加配置
完整配置 application.yml
指定注册中心地址,配置自己的服务名称
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: feign-consumer
server:
port: 9001
测试服务
依次启动项目:
spring-cloud-eureka-service
spring-cloud-eureka-provider-1
spring-cloud-eureka-provider-2
spring-cloud-eureka-provider-3
spring-cloud-feign-consumer
启动该工程后,访问服务注册中心,查看服务是否都已注册成功:http://localhost:8761/
负载均衡响应
浏览器get
请求: http://localhost:9001/hello
Spring Cloud(四)服务提供者 Eureka + 服务消费者 Feign的更多相关文章
- Spring Cloud Netflix之Eureka服务消费者
Eureka服务消费者介绍 Eureka服务消费者用于发现服务和消费服务,发现服务通过Eureka Client完成,消费服务通过Ribbon完成,以实现负载均衡.在实际项目中,一个服务往往同时是服务 ...
- Spring Cloud(九)高可用的分布式配置中心 Spring Cloud Config 集成 Eureka 服务
上一篇文章,讲了SpringCloudConfig 集成Git仓库,这一篇我们讲一下SpringCloudConfig 配和 Eureka 注册中心一起使用 在分布式系统中,由于服务数量巨多,为了方便 ...
- 一起来学Spring Cloud | 第四章:服务消费者 ( Feign )
上一章节,讲解了SpringCloud如何通过RestTemplate+Ribbon去负载均衡消费服务,本章主要讲述如何通过Feign去消费服务. 一.Feign 简介: Feign是一个便利的res ...
- Spring Cloud 入门教程(二): 服务消费者(rest+ribbon)
在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...
- Spring Cloud(二)服务提供者 Eureka + 服务消费者(rest + Ribbon)
Ribbon是什么? Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起.Ribbon客户端组件提供一系列完善的配置项如连接超时 ...
- 2.Spring Cloud初相识--------Eureka服务注册与消费
前言: 1.Eureka介绍: Spring Cloud Eureka,使用Netflix Eureka来实现服务注册与发现,它既包含了服务端组件,也包含了客户端组件,并且服务端与客户端均采用Java ...
- Spring Cloud(Dalston.SR5)--Eureka 服务实例健康检查
默认情况下,Eureka 客户端每隔 30 秒会发送一次心跳给服务器端,告知正常存活,但是,实际环境中有可能出现这种情况,客户端表面上可以正常发送心跳,但实际上服务是不可用的,例如,一个需要访问数据的 ...
- Spring Cloud(Dalston.SR5)--Eureka 服务消费
服务被注册.发布到 Eureka 服务器后,需要有程序去发现他,并且进行调用,称为服务消费,一个服务可能会部署多个实例,调用过程可能涉及负载均衡.服务器查找等问题,这些问题 Netflix 项目已经帮 ...
- Spring Cloud系列之Eureka服务治理
写在前面 Spring Cloud Eureka是基于Netflix Eureka做的二次封装.主要包含两部分: 服务注册中心 eureka server 服务提供者 eureka client ps ...
随机推荐
- PostgreSql 使用自定义序列(Sequence)向表插入数据
最近公司使用到了PostgreSql,哈哈,这个SQL之前基本上没有用过,既然公司使用到了,那就学习一下吧,记一篇小笔记: 什么是PostgreSql:https://www.postgresql.o ...
- 小记LoadRunner 11 安装VC2005运行环境报错处理
这几天在做性能优化,需要在虚拟机里装个LoadRunner 11.从测试同学那里搞来安装包,按照文档提示安装系统运行环境,提示我要装VC2005 SP1. 安装程序自己安装,报错.截图如下. 于是我又 ...
- mysql删除一条记录
mysql如何删除一条记录 delete from 表名 where 条件 实例: use db1 delete from tb1 where id = 1;
- Example config file /etc/vsftpd.conf
# Example config file /etc/vsftpd.conf # # The default compiled in settings are fairly paranoid. Thi ...
- 纯css实现表单输入验证
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Class WriteGroupAttribute
[WriteGroup]可以排除在创建已声明写入同一组件的查询时未知的组件. 这允许安全地扩展组件系统而无需编辑先前存在的系统. 目标是为期望将数据从一组组件(输入)转换为另一组(输出[s])的系统能 ...
- 日志.VC
1. int WriteLog(char* _pcFullFileName, char* _pcWrite, int _iWriteLen, unsigned long * _pdwWritten) ...
- thinkPHP5 类库包注册
tp5的类库包注册分为自动注册和手动注册 自动注册 我们只需要把自己的类库包目录放入EXTEND_PATH目录(默认为extend,可配置),就可以自动注册对应的命名空间,例如: 我们在extend目 ...
- NDK学习笔记-增量更新
虽然现在有插件化开发和热修复,但为何还需要增量更新?插件化开发和热修复依赖于宿主程序,增量更新适合更新宿主程序. 差分包生成的前提 差分包的生成依赖于BsDiff开源项目,而BsDiff又依赖于Bzi ...
- new , delete常见用法和与malloc,free比较
new/delete是C++的运算符.malloc与free是C++/C语言的标准库函数,new/delete只能在C++中使用,malloc与free在C与C++中都能够使用,它们都可用于申请动态内 ...