Spring Cloud源码分析之eureka+feign远程调用

Eureka搭建与Feign实现服务调用 一、Eureka基本概念 Eureka是Netflix开发的服务发现框架,SpringCloud将它集成在自己的子项目 spring-cloud-netflix中,实现SpringCloud的服务发现功能。Eureka包含两个组件: Eureka ServeEureka Client。 Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册,这样Eu... 阅读详情

是什么

Eureka是一个REST (Representational State Transfer)服务,用于定位服务,以实现中间层服务器的负载平衡和故障转移,我们称此服务为Eureka服务器。Eureka还有一个基于java的客户端组件,Eureka客户端,这使得与服务的交互更加容易,同时客户端也有一个内置的负载平衡器,它执行基本的循环负载均衡。

Feign 是一种声明式服务调用组件,它在 RestTemplate 的基础上做了进一步的封装。通过 Feign,我们只需要声明一个接口并通过注解进行简单的配置(类似于 Dao 接口上面的 Mapper 注解一样)即可实现对 HTTP 接口的绑定。

通过 Feign,我们可以像调用本地方法一样来调用远程服务,而完全感觉不到这是在进行远程调用。

为什么

eureka解决了什么问题?为什么要用eureka?

eureka为分布式环境引入了服务注册与发现的能力,如果没有服务注册与发现,我们便需要手动去维护一大堆服务的地址信息,而当服务的状态变化发生变化,比如有新的服务加入或某些现有服务突然不可用时,现存的服务也无法感知到并及时切换到可用的服务上去。

相对其他提供服务注册与发现的组件(比如说zk),Eureka更偏向于AP。如果分布式系统对可用性的要求更高,那么,Eureka会是一个不错的选择。

另外,feign屏蔽了请求构建、发送、重试、负载均衡等相关细节,让我们能够从重复的工作中解放出来,更专注于业务本身。

架构图

在这里插入图片描述
在这里插入图片描述

案例

eureka server

引入依赖

implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'

启动类

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
   
   

	public static void main(String[] args) {
   
   
		SpringApplication.run(EurekaServerApplication.class, args);
	}
}

配置文件

server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

service consumer

依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

启动类

@SpringBootApplication
@EnableDiscoveryClient
@RestController
@EnableFeignClients
public class HelloClientApplication {
   
   
	@Autowired
	HelloClient client;

	@RequestMapping("/")
	public String hello() {
   
   
		return client.hello();
	}

	public static void main(String[] args) {
   
   
		SpringApplication.run(HelloClientApplication.class, args);
	}

	@FeignClient("HelloServer")
	interface HelloClient {
   
   
		@RequestMapping(value = "/", method = GET)
		String hello();
	}
}

配置文件

spring:
  application:
    name: HelloClient

server:
  port: 7211

eureka:
  password: password
  client:
    serviceUrl:
      defaultZone: http://user:${
   
   eureka.password}@localhost:8761/eureka/
  instance:
    leaseRenewalIntervalInSeconds: 10
    metadataMap:
      instanceId: ${
   
   vcap.application.instance_id:${
   
   spring.application.name}:${
   
   spring.application.instance_id:${
   
   server.port}}}

endpoints:
  restart:
    enabled: true

service provider

依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

启动类

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class HelloServerApplication {
   
   
	@Autowired
	DiscoveryClient client;

	@RequestMapping("/")
	public String hello() {
   
   
		List<ServiceInstance> instances = client.getInstances("HelloServer");
		ServiceInstance selectedInstance = instances
				.get(new Random().nextInt(instances.size()));
		return "Hello World: " + selectedInstance.getServiceId() + ":" + selectedInstance
				.getHost() + ":" + selectedInstance.getPort();
	}

	public static void main(String[] args) {
   
   
		SpringApplication.run(HelloServerApplication.class, args
Eureka结合Feign使用案例 //使用eureka实现服务发现功能 Feign是声明式web服务客户端。 (1)启动eureka服务器 @SpringBootApplication @EnableEurekaServer public class ApplicationEurekaServer { public static void main(String[] args) { new SpringApplicationBuilder(ApplicationEurekaServer.class) .web(tru. 阅读详情

相关推荐

微服务搭建(eureka+feign)打印helloworld

目录 1、概念 1)、springcloud 2)、eureka 3)、feign 2、创建eureka注册中心 3、创建client客户端 1)、创建client1、client2服务 2)、编写client1服务端业务代码 3)、编写client2服务端业务代码 4、服务调用 由client2调用client1的代码,并在client1控制台打印helloworld。 一、概念 需要自行了解springcloudeurekafeign的基本知识,此处只记录个人简单理

weixin_41867358的博客 487

Sping Cloud Feign 配置、使用源码分析

Sping Cloud Feign 配置、使用源码分析1. 简介2. 基本使用2.1 Feign 依赖2.2 Feign 注解2.3 Feign 测试2.4 Feign 配置(可选)3. 源码分析3.1 原理源码详解3.2 请求是如何转到 Feign 的?3.3 Feign 是怎么工作的?3.4 Feign 的负载均衡策略? 官网文档:https://spring.io/projects/spring-cloud-openfeign 1. 简介 Spring Cloud OpenFeign : Decl

Hello, New World! 700

服务治理:Spring Cloud Eureka

文章目录服务治理:Spring Cloud EurekaEureka服务治理构建服务注册中心服务注册与服务发现**==总结==**:就是如果选用的注册中心是eureka,那么就推荐@EnableEurekaClient, 如果是其他的注册中心,那么推荐使用@EnableDiscoveryClient。Eureka详解:服务治理机制服务提供者服务消费者服务注册中心源码分析配置详解服务注册类配置服务实...

white_while的博客 427

Spring Cloud 微服务开发:入门、进阶与源码剖析 —— 通篇概述

前因后果 关注小编的同学应该知道,小编去年出版社约稿写过一本书,后面种种原因放弃掉最后的审校过程了,小编说过会把这本书发出来,这次选择了 CSDN 的平台也是因为能赚点小钱,价格也不贵,9.9 元,买不了吃亏买不了上当,不用担心小编烂尾或者书没写完(只是没有经过审校,可能错别字会有不少),全部内容统计将近 18w 字(未计算代码内容),预计一周发两篇到三篇左右的样子(不要问为啥不一周发十几篇,因...

极客挖掘机 6183

eureka入门与实践(含feign实现微服务调用)

1.什么是eureka eureka(服务发现框架)是Netflix开源的服务发现组件,本身是一个基于rest(一种软件架构风格)的服务。它包含serverclient两部分。springcloud将它集成在子项目springcloudNetflix中。 功能:微服务的注册与发现 2.eureka原理 我们可以看下eureka的架构图 其中 application service可以理解为服务提供者,application client为服务消费者,make remote call为调用re

ZeronGod的博客 2002

Eureka 实现feign

其中重要的连接点为在Eureka注册中心注册客户端,其中包括消费者端提供者端,消费者为进行调用的服务,提供者端顾名思义为具体接口的实现方。这个时候可能会报错连接不到https://start.spring.io,解决办法:自行百度,我也是试了几个才成功,我用的办法是在setting中的http代理里进行检查连接之后成功的,但是不一定都是这样的情况。消费端实现调用提供端接口的功能,模块搭建与提供端一摸一样,不重复描述了,该模块中主要是创建一个接口类,通过这个接口类进行调用提供端的feign接口。

little_fairy66的博客 750

Springboot整合 openfeign(基于最新Hoxton.SR8)

文章目录项目整体结构依赖openfeign的一些配置order-service-apiorder-servicepay-service测试源码下载 项目整体结构 说明: 所有公共依赖都放在了父pom中,API接口抽离放在单独模块 依赖 <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR8</spring-cloud.version

念念不忘,必有回响 4949

Spring Cloud部分源码分析Eureka,Ribbon,Feign,Zuul

Eureka SpringCloud Eureka使用NetFlix Eureka来实现的,它包括了服务端组件客户端组件,并且都是用java 编写的。 Eureka服务端就是服务注册中心, Eureka客户端主要处理服务的注册发现,通过注解参数配置的方式,客户端向注册中心注册,并且周期性的发送心跳维护一个可用列表。 服务注册中心,用户服务注册发现功能。 ![img](file:///C:\...

0102的博客 529

如何快速入门Spring Cloud

想要入门Spring Cloud首先得了解Spring Cloud是什么? Spring Cloud是什么? Spring Cloud为开发人员构建微服务架构提供了完整的解决方案,SpringCloud是若干个框架的集合,它包括spring-cloud-config、spring-cloud-bus等近20个子项目,它提供了服务治理、服务网关、智能路由、负载均衡、断路器、监控跟踪、分布式消息队列、配置管理等领域的解决方案。 Spring cloud的优点: 服务拆分粒度更细,有利于资源重复利用,有

wdj_yyds的博客 2898

Spring Cloud:深入浅出服务调用组件Feign原理、源码分析

Feign 是一个声明式 Web服务客户端,使用它创建一个接口并注解,使得编写 Web服务客户端变得更加容易。 它支持可插拔注解,包括 Feign 注解 JAX-RS 注解,还支持可插播得编码、解码器。 Cloud 增加了对 Spring MVC 注解的支持,默认使用 httpmessageconverter 的支持。 Cloud 集成了 Ribbon Eureka以及 BalanceLoad,使得在使用 Feign 时支持 Http 客户端的负载均衡。 Feign 自定义的客户端配置不需要使用 @C

Ramboy's Blogs 536

Spring Cloud Eureka

一、Eureka简介: 从现在微服务框架的使用来看,毫无疑问,Spring Cloud 是目前微服务架构领域的翘楚。那么,对于他,你了解多少呢? 实际上,Spring Cloud 是一个全家桶式的技术栈,基于Spring Boot进行开发,它包含了很多组件。其中有5个最核心的组件 Eureka、Ribbon、Feign、Hystrix、Zuul。本文主讲EurekaEureka是Netflix开源的服务发现组件,Spring Cloud将其集成在Spring Cloud Netfli...

qinwenjng120的博客 540

Spring Cloud Bamboo源码分析

一、项目简介Spring cloud bamboo是spring cloud中国社区推出的一个多版本控制插件,它通过扩展spring-cloud-ribbon实现了多版本调用,地址为https://github.com/SpringCloud/spring-cloud-gray/tree/master/spring-cloud-bamboo。二、思路分析在spring cloud微服务体系中,服务...

三分之一程序员 2248

搭建Eureka服务并调用Feign及删除业务讲解

Eureka Server之间通过复制的方式完成数据的同步,Eureka还提供了客户端缓存机制,即使所有的Eureka Server都挂掉,客户端依然可以利用缓存中的信息消费其他服务的API。Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。但是如果在保护期间,实例出现问题,那么客户端很容易拿到实际已经不存在的服务实例,会出现调用失败。

怀揣梦想,一颗执着于技术的心从未磨灭,内心住着一颗顽强的小强时刻提醒自己层层突破自我,同时也成就他人 1520

demo(四)eureka&feign----声明式服务注册、提供与消费

下面写一个简单的demo验证下eurekafeign,实现服务注册、服务发现负载均衡。 一、api: 封装其他组件需要共用的dto 二、eureka-service服务注册中心: 1、pom: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/PO...

w_t_y_y的博客 1453

13.0、springcloud-Feigneureka整合feign、以及feign使用接口方式调用服务

13.0、springcloud-Feigneureka整合feign、以及feign-使用接口方式调用服务 简介: feign是声明式的 web service 客户端,他让微服务之间的调用变得更简单了,类似 controller调用 service。Spring cloud 集成了 Ribbon Eureka ,可以在使用 Feign 时提供负载均衡的 http 客户端 只需要创建一个接口,然后添加注释即可 Feign ,主要是社区...

m0_52433668的博客 1447

1.eurekafeign

留着

不忘初心 3246

微服务服务之间远程调用的几种方式(RestTemplate、Eureka、Nacos、feign

微服务服务之间远程调用的几种方式(RestTemplate、Eureka、Nacos、feign

qq_43305367的博客 1万+

SpringCloud学习day03——Eureka注册中心+OpenFeign服务调用

在B站学习的springcloud的学习记录,第二天,最难不过坚持。本文章只用于学习分享,欢迎大家的建议,讨论指点。

weixin_52896580的博客 1108
上一篇: Spring源码分析之事物管理
下一篇: MySQL浅析之架构概览
穹柏
博客等级 码龄9年 11粉丝 36原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值