Spring Cloud Netflix之Euraka Server注册中心
Spring Cloud简介
Spring Cloud是基于Spring Boot的一套实现微服务架构的生态组件。生态组件中包含Spring Cloud NetFlix,Spring Cloud Feign,Spring Cloud Config,Spring Cloud CloudFoundry,Spring Cloud Bus,Spring Cloud Security,Spring Cloud Stream等生态组件,用于解决微服务架构中的一系列问题。Dubbo等组件也能实现微服务构建,但对比与Spring Cloud来说,Spring Cloud的生态组件较完善,不必花像Dubbo等构建要去花精力选择其余框架。Spring Cloud文档较为完善,且组件一直在优化。所以,当前对于构建微服务架构来说,Spring Cloud是较好的选择。
服务的注册中心
Spring Cloud Netflix提供Euraka Server来实现注册中心。注册中心在整个微服务架构中是最核心的模块,用于提供注册中心给微服务实例实现自动化注册与发现。为实现注册中心的高可用,一般会创建多个注册中心,并相互注册(一般创建2个),避免单点故障导致服务不可用。
以下介绍如何搭建高可用的服务注册中心,并实现实例下线时注册中心对实例列表的动态删除。
说明:搭建基于Spring Cloud Netflix 2.0.2版本,开发工具为Idea。
1、创建Euraka Server,配置pom.xml依赖(可以使用Spring Initializr来创建)
<?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.springcloud</groupId>
<artifactId>server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>SCServer</name>
<description>Demo project for Spring Cloud Server</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.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</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>
注意:在2.0版本中,依赖jar为 spring-cloud-starter-netflix-eureka-server,而在1.0版本中为 spring-cloud-starter-eureka-server。
2、配置application.yml
我们这里统一采用yml来作为配置文件,相对于properties来说,yml能支持自动补全提示,且格式更为清晰(使用yml是注意缩进)。
spring:
application:
name: eureka-server0
server:
port: 11110
eureka:
instance:
hostname: peer0
client:
register-with-eureka: false
service-url:
defaultZone: http://peer1:11111/eureka
fetch-registry: false
server:
enable-self-preservation: false
主要几个配置的说明:
- eureka.instance.hostname:注册中心的注册名。
- euraka.client.register-with-euraka:配置为false,表示不将自己注册到注册中心。
- euraka.client.serviceUrl.defaultZone:配置defaultZone中的注册地址,该项用于配置注册中心高可用时,需要向另一台注册中心注册自己。(此配置为key-value类型,defaultZone为默认Zone的key)
- euraka.client.fetch-registry:配置为false,表明该注册中心只负责管理实例,不负责去检索实例服务。
- euraka.server.enable-self-preservation:配置为false,用处是为了注册中心能及时删除下线服务,而不是保留,配置禁止其保护模式。(根据项目实际需求配置)
3、在入口程序中使用@EnableEurakaServer声明为euraka server
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer
@SpringBootApplication
public class ScServerApplication { public static void main(String[] args) {
SpringApplication.run(ScServerApplication.class, args);
}
}
说明:也可使用@EnableDiscoveryClient来注释,@EnableEurakaServer是使用Euraka作为注册中心时使用的注释,使用其它类型的注册中心,可使用@EnableDiscoveryClient。
4、同理配置另一个注册中心,application.yml如下(需要在hosts文件中增加127.0.0.1 peer0 127.0.0.1 peer1)。
spring:
application:
name: eureka-server1
server:
port: 11111
eureka:
instance:
hostname: peer1
client:
register-with-eureka: false
service-url:
defaultZone: http://peer0:11110/eureka
fetch-registry: false
server:
enable-self-preservation: false
5、启动程序,访问http://localhost:11110与http://localhost:11111可查看到两注册中心启动成功,且相互注册。至此,高可用的Eureka注册中心搭建完成。


Spring Cloud Netflix之Euraka Server注册中心的更多相关文章
- spring cloud 学习(2) - eureka server注册中心高可用及安全认证
接上节继续,注册中心单点肯定是不牢靠的,可以参考下面的方案做成注册中心集群: 弄成3个节点,每个节点向其它节点注册,这样只要集群中有一个节点正常工作即可.为了方便在本机弄出这种效果,我们先修改下hos ...
- Spring Cloud 系列之 Alibaba Nacos 注册中心(二)
本篇文章为系列文章,未读第一集的同学请猛戳这里:Spring Cloud 系列之 Alibaba Nacos 注册中心(一) 本篇文章讲解 Nacos 注册中心集群环境搭建. Nacos 集群环境搭建 ...
- spring cloud 专题一 (spring cloud 入门搭建 之 Eureka注册中心搭建)
一.前言 本文为spring cloud 微服务框架专题的第一篇,主要讲解如何快速搭建spring cloud微服务及Eureka 注册中心 以及常用开发方式等. 本文理论不多,主要是傻瓜式的环境搭建 ...
- Spring cloud搭建Eureka高可用注册中心
注册中心在微服务中是必不可少的一部分,主要用来实现服务自治的功能,本文则主要记载使用Netflix提供的Eureka作为注册中心,来实现服务自治的功能. 实际上Eureka的集群搭建方法很简单:每一台 ...
- spring cloud深入学习(二)-----服务注册中心spring cloud eureka
服务治理 主要用来实现各个微服务实例的自动化注册与发现,为啥需要这玩意呢?在一开始比如A系统调用B服务,可能通过手工维护B服务的实例,并且还得采用负载均衡等方式,这些全部都得需要手工维护,等后面系统越 ...
- SpringCloud微服务实战一:Spring Cloud Eureka 服务发现与注册中心(高可用实列为两个注册中心)
微服务架构: 微服务架构的核心思想是,一个应用是由多个小的.相互独立的.微服务组成,这些服务运行在自己的进程中,开发和发布都没有依赖.不同服务通过一些轻量级交互机制来通信,例如 RPC.HTTP 等, ...
- Spring Cloud 系列之 Alibaba Nacos 注册中心(一)
前言 从本章节开始,我们学习 Spring Cloud Alibaba 相关微服务组件. Spring Cloud Alibaba 介绍 Spring Cloud Alibaba 致力于提供微服务开发 ...
- 使用Spring Cloud搭建高可用服务注册中心
我们需要的,不仅仅是一个服务注册中心而已,而是一个高可用服务注册中心. 上篇博客[使用Spring Cloud搭建服务注册中心]中我们介绍了如何使用Spring Cloud搭建一个服务注册中心,但是搭 ...
- Spring Cloud之整合ZK作为注册中心
Eureka已经闭源了,用zk可以替代之 Eureka 作为注册中心 Dubbo也是zk作为注册中心的 Zookeeper简介 Zookeeper是一个分布式协调工具,可以实现服务注册与发现.注册中心 ...
随机推荐
- python中动态创建类
class Foo(Bar): pass Foo中有__metaclass__这个属性吗?如果是,Python会在内存中通过__metaclass__创建一个名字为Foo的类对象(我说的是类对象,请紧 ...
- Linux根目录下各目录含义
/boot:系统启动的相关文件,比如内核,grub /etc:配置文件 /dev:设备文件 /root:root用户的家目录 /home:用户家目录 /lib:库文件 /bin:用户的命令文件 /sb ...
- [日常] gitlab创建用户并把用户加入项目
在gitlab里创建用户 默认密码是要求创建的用户自己去邮箱重置,也可以创建完成后直接点击编辑,就可以更改密码了 创建完用户,用户登录的时候需要去重置密码 创建完项目,就可以去使用了 也可以为这个项目 ...
- 2019CCPC网络选拔赛 hdu6703 array(主席树+set)
题意 给你一个1~n的排列,由两种操作: 1 pos:将a[pos]+10 000 000 2 r k:求大于等于k且不等于a[1~r]的数的最小值. 强制在线. 思路 如果没有1操作,那么我们直接主 ...
- vue 表格组件 有事件交互(二)
04==>v-if下面可以嵌套 同级的 v-if 和v-node如下若是第一个v-if没有下面的就不可能显示出来的. <span v-if="!single" @cli ...
- Vuex简介
一. 什么是Vuex? Vuex Vuex是一个专门为Vue.js应用程序开发的状态管理模式, 它采用集中式存储管理所有组件的公共状态, 并以相应的规则保证状态以一种可预测的方式发生变化. Vue ...
- Matlab各种拟合
作者:Z-HE链接:https://zhuanlan.zhihu.com/p/36103034来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 1) polyfit 代码 ...
- SecureCRT 8.1工具下载和破解附Xshell6
附教程:https://jingyan.baidu.com/article/eae078275917861fec548592.html 这一篇教程实际上已经说得非常明确了,只需要把注册机放在和secu ...
- Windows Azure Virtual Machine (39) 清除Linux挖矿病毒
<Windows Azure Platform 系列文章目录> 1.之前客户遇到了Azure Linux CPU 100%,症状如下: 2.SSH登录到Linux,查看crontab,有从 ...
- LeetCode 160: 相交链表 Intersection of Two Linked Lists
爱写Bug(ID:iCodeBugs) 编写一个程序,找到两个单链表相交的起始节点. Write a program to find the node at which the intersectio ...