Eureka服务治理学习笔记(摘抄)
1.简介
EureKa在Spring Cloud全家桶中担任着服务的注册与发现的落地实现。Netflix在设计EureKa时遵循着AP原则,它基于R
EST的服务,用于定位服务,以实现云端中间层服务发现和故障转移,功能类似于Dubbo的注册中心Zookeeper。
2.实现原理

EureKa采用C-S的设计架构,即包括了Eureka Server(服务端),EureKa client(客户端)。
1.EureKa Server 提供服务注册,各个节点启动后,在EureKa server中进行注册;
2 EureKa Client 是一个Java客户端,用于和服务端进行交互,同时客户端也是一个内置的默认使用轮询负载均衡算法的负载均衡器。在应用启动后,会向Eueka Server发送心跳(默认30秒)。如果EUR额卡 Server在多个心跳周期内没有接受到某个节点的心跳,EureKa Server将会从服务注册表中将这个服务移出(默认90秒)。
3 Eureka是Spring Cloud Netflix微服务套件中的一部分,可以与Springboot构建的微服务很容易的整合起来。
Eureka包含了服务器端和客户端组件。服务器端,也被称作是服务注册中心,用于提供服务的注册与发现。Eureka支持高可用的配置,当集群中有分片出现故障时,Eureka就会转入自动保护模式,它允许分片故障期间继续提供服务的发现和注册,当故障分片恢复正常时,集群中其他分片会把他们的状态再次同步回来。
客户端组件包含服务消费者与服务生产者。在应用程序运行时,Eureka客户端向注册中心注册自身提供的服务并周期性的发送心跳来更新它的服务租约。同时也可以从服务端查询当前注册的服务信息并把他们缓存到本地并周期性的刷新服务状态。
关系调用说明:
- 服务生产者启动时,向服务注册中心注册自己提供的服务
- 服务消费者启动时,在服务注册中心订阅自己所需要的服务
- 注册中心返回服务提供者的地址信息个消费者
- 消费者从提供者中调用服务
3.简单实例
①注册中心
配置文件:
在默认情况下,服务注册中心也会把自己当做是一个服务,将自己注册进服务注册中心,所以我们可以通过配置来禁用他的客户端注册行为,在application.properties中添加如下配置:
#设置tomcat服务端口号
server.port=1111
#设置服务名称
spring.application.name=eureka-service eureka.instance.hostname=localhost
#注册中心不需要注册自己
eureka.client.register-with-eureka=false
#注册中心不需要去发现服务
eureka.client.fetch-registry=false
#设置服务注册中心的URL
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
/**
*
* @EnableEurekaServer
* 用来指定该项目为Eureka的服务注册中心
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaApp { public static void main(String[] args) {
SpringApplication.run(EurekaApp.class, args);
}
}
访问 http://localhost:1111/ Eureka信息面板
主程序类:
在Springboot项目中的main入口,添加@EnableEurekaServer注解,来开启服务注册中心.
在主类上添加@EnableEurekaClient注解以实现Eureka中的DiscoveryClient实现。
②服务端: 配置文件: server.port=9090
#设置服务名
spring.application.name=hello-service
#设置服务注册中心的URL,本服务要向该服务注册中心注册自己
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka 主程序:
/***
* @EnableDiscoveryClient
* 让服务使用eureka服务器
* 实现服务注册和发现
*/
@EnableDiscoveryClient
@SpringBootApplication
public class HelloApp {
public static void main(String[] args) {
SpringApplication.run (HelloApp.class, args);
}
}
controller类:
将org.springframework.cloud.client.discovery.DiscoveryClient;对象注入,并且在日志中打印出与服务相关的一些信息。
@RestController
public class HelloController {
Logger logger = LoggerFactory.getLogger (HelloController.class); @Autowired
DiscoveryClient discoveryClient; @RequestMapping("/hello")
public String hello() {
ServiceInstance instance = discoveryClient.getLocalServiceInstance ();
//打印服务的服务id
logger.info ("*********" + instance.getServiceId ());
return "hello,this is hello-service";
}
}
③消费端
配置文件
server.port=9999
spring.application.name=hello-consumer
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka
#这里的配置项目和服务提供者hello-service一样 主程序类
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApp {
//@Bean 应用在方法上,用来将方法返回值设为为bean
@Bean
@LoadBalanced //@LoadBalanced实现负载均衡
public RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class, args);
}
}
/*
这里也要用到@EnableDiscoveryClient, 让服务使用eureka服务器, 实现服务注册和发现*/
controller类:
@RestController
public class ConsumerController { //这里注入的restTemplate就是在com.sam.ConsumerApp中通过@Bean配置的实例
@Autowired
RestTemplate restTemplate; @RequestMapping("/hello-consumer")
public String helloConsumer() {
//调用hello-service服务,注意这里用的是服务名,而不是具体的ip+port
restTemplate.getForObject("http://hello-service/hello", String.class);
return "hello consumer finish !!!";
}
}
测试: 访问http://localhost:8088/hello可以在控制台中看到日志信息
Eureka服务治理学习笔记(摘抄)的更多相关文章
- 1 Spring Cloud Eureka服务治理
注:此随笔为读书笔记.<Spring Cloud微服务实战> 什么是微服务? 微服务是将一个原本独立的系统拆分成若干个小型服务(一般按照功能模块拆分),这些小型服务都在各自独立的进程中运行 ...
- 1 Spring Cloud Eureka服务治理(上)
注:此随笔为读书笔记.<Spring Cloud微服务实战>,想学习Spring Cloud的同伴们可以去看看此书,里面对源码有详细的解读. 什么是微服务? 微服务是将一个原本独立的系统拆 ...
- kratos微服务框架学习笔记一(kratos-demo)
目录 kratos微服务框架学习笔记一(kratos-demo) kratos本体 demo kratos微服务框架学习笔记一(kratos-demo) 今年大部分时间飘过去了,没怎么更博和githu ...
- Spring cloud Eureka 服务治理(注册服务提供者)
搭建完成服务注册中心,下一步可以创建服务提供者并向注册中心注册服务. 接下来我们创建Spring Boot 应用将其加入Eureka服务治理体系中去. 直接使用签名章节创建hello服务项目改造: 1 ...
- 笔记:Spring Cloud Eureka 服务治理
Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务 ...
- 1 Spring Cloud Eureka服务治理(下)
注:此随笔为读书笔记.<Spring Cloud微服务实战> 上篇主要介绍了什么是微服务以及微服务治理的简单实现,如微服务注册中心的实现.微服务注册的实现.微服务的发现和消费的实现.微服务 ...
- Spring Cloud 微服务架构学习笔记与示例
本文示例基于Spring Boot 1.5.x实现,如对Spring Boot不熟悉,可以先学习我的这一篇:<Spring Boot 1.5.x 基础学习示例>.关于微服务基本概念不了解的 ...
- Spring Cloud系列之Eureka服务治理
写在前面 Spring Cloud Eureka是基于Netflix Eureka做的二次封装.主要包含两部分: 服务注册中心 eureka server 服务提供者 eureka client ps ...
- Spring Cloud Eureka 服务治理
Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务 ...
随机推荐
- nutch2.2.1+mysql抓取数据
基本环境:linux centos6.5 nutch2.2.1 源码包, mysql 5.5 ,elasticsearch1.1.1, jdk1.7 1.下载地址http://mirror.bjtu. ...
- 网页开发人员收藏的16款HTML5工具
本文收集的20款优秀的 HTML5 Web 应用程序,值得添加到您的 HTML5 的工具箱中,他们能够帮助你开发前端项目更快.更容易. Initializr Initializr 是一个可以让你创建 ...
- sql(10) sum
SUM() 函数SUM 函数返回数值列的总数(总额).SQL SUM() 语法SELECT SUM(column_name) FROM table_name新建表 StudentSS_id Grade ...
- 关于jquery ajax项目总结
$.ajax({ url:"../action/BorrowAction.php?action=oready", async:true,//异步请求 ...
- https://vjudge.net/contest/321565#problem/C 超时代码
#include <iostream> #include <cstdio> #include <queue> #include <algorithm> ...
- 【JZOJ6350】考试(test)
description analysis 对于\(n=0\)的点,直接模拟就好了 状压\(DP\),设\(f[i][j][S]\)表示到第\(i\)题.连续\(GG\)了\(j\)题.喝的饮料集合为\ ...
- jQuery FormData使用方法
FormData的主要用途 将form表单元素的name与value进行组合,实现表单数据的序列化,从而减少表单元素的拼接,提高工作效率. 异步上传文件 注:FormData 对象的字段类型可以是 B ...
- C++ 贪吃蛇二维
#include <iostream> #include <conio.h> #include <windows.h> #include <time.h> ...
- 关于Unity中资源打包
资源包详细说明 Unity很智能只会打包用到的资源,比如sharedassets0.assets中的shader资源,如果场景中有OBJ用到了shader那么就会有shader打进这个包,如果没有就不 ...
- spring源码读书笔记
如果我们在web项目里面使用spring的话,通常会在web.xml里面配置一个listener. <listener> <listener-class> org.spring ...