注册中心在微服务中是必不可少的一部分,主要用来实现服务自治的功能,本文则主要记载使用Netflix提供的Eureka作为注册中心,来实现服务自治的功能。

实际上Eureka的集群搭建方法很简单:每一台Eureka只需要在配置中指定另外多个Eureke的地址,就可以实现一个集群的搭建了

例如:

两节点

  -- 节点1注册到节点2

  --节点2注册到节点1

三节点

  --节点1注册到节点2,3

  --节点2注册到节点1,3

  --节点3注册到节点1,2

我做的是两个Eureka节点构建注册中心,然后一个producer和一个customer分别进行提供服务和请求服务:见图

节点1:

先新建一个Maven项目,添加依赖(基于jdk11,如果不是jdk11,可以不添加)

pom.xml主要代码

 <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/>
</parent> <properties>
<java.version>11</java.version>
</properties> <!-- 依赖 -->
<dependencies>
<!-- Eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency> <!--java 11 缺少的模块 javax.xml.bind-->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency> <!-- 添加 Spring-Security 不需要则不添加-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies> <!-- Spring Cloud -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

application.yml

---
server:
###启动端口
port:
spring:
###启动的配置文件名
profiles: eureka1
###应用名
application:
name: master
eureka:
instance:
hostname: eureka1
client:
###不向注册中心注册自己
register-with-eureka: false
###不需要检索服务
fetch-registry: false
serviceUrl:
defaultZone: http://127.0.0.1:8762/eureka/
server:
###关闭自我保护模式
enable-self-preservation: false
###清理的间隔为5s
eviction-interval-timer-in-ms:
security:
basic:
###暂时关闭安全认证
enabled: false ---
server:
###启动端口
port:
spring:
###启动的配置文件名
profiles: eureka2
application:
###应用名
name: slaveone
eureka:
instance:
hostname: eureka2
client:
###不向注册中心注册自己
register-with-eureka: false
###不需要检索服务
fetch-registry: false
serviceUrl:
defaultZone: http://127.0.0.1:8761/eureka/
server:
###关闭自我保护模式
enable-self-preservation: false
###清理的间隔为5s
eviction-interval-timer-in-ms:
security:
basic:
###暂时关闭安全认证
enabled: false

启动类:EurekaServerApplication.class

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

启动参数 IDEA设置    --spring.profiles.active=eureka1

点击这个:

同理另外一个节点的启动类,application.yml和pom.xml文件与节点1相同

启动参数改为--spring.profiles.active=eureka2

此时,浏览器输入 http://127.0.0.1:8761/发现如下:

至此注册中心已经搭建完成

producer服务注册:

主要的pom.xml文件

     <properties>
<java.version>11</java.version>
</properties> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/>
</parent> <!-- 依赖 -->
<dependencies>
<!-- Eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--java 11 缺少的模块 javax.xml.bind-->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency> <!-- Actuator 健康检查 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies> <!-- Spring Cloud -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

application.properties文件:

spring.application.name=producer
server.port=8081
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/,http://127.0.0.1:8762/eureka/ ### 配置健康检查的信息
eureka.client.healthcheck.enabled=true
### 默认30s
eureka.instance.lease-renewal-interval-in-seconds=5
### 默认90秒
eureka.instance.lease-expiration-duration-in-seconds=5
 @RestController
@RequestMapping("/home")
public class HomeController { @GetMapping("/hello")
public String hello(){
return "hello";
}
}

基本的control类和启动类

 @SpringBootApplication
@EnableDiscoveryClient
public class ProduceServiceApplication {
public static void main(String[] args){
SpringApplication.run(ProduceServiceApplication.class, args);
}
}

启动之后再看Eureka注册中心:两个节点应该都有

访问   http:127.0.0.1:8081/home/hello  发现正常返回字符串

此时

搭建customer请求服务

pom.xml 和application.properties文件

<properties>
<java.version>11</java.version>
</properties> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/>
</parent> <!-- 依赖 -->
<dependencies>
<!-- Eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--java 11 缺少的模块 javax.xml.bind-->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency> <!-- Actuator 健康检查 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies> <!-- Spring Cloud -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
spring.application.name=customer
server.port=
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/,http://127.0.0.1:8762/eureka/ ### 配置健康检查的信息
eureka.client.healthcheck.enabled=true
### 默认30s
eureka.instance.lease-renewal-interval-in-seconds=
### 默认90秒
eureka.instance.lease-expiration-duration-in-seconds=

获取RestTemplate的类:

 @Configuration
public class BeanConfiguration { @Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}

control类

 @RestController
@RequestMapping("/customer/")
public class CustomerServiceController { @Autowired
private RestTemplate restTemplate; @GetMapping("/callHello")
public String callHello(){
return restTemplate.getForObject("http://producer/home/hello", String.class);
}
}

启动类:

 @SpringBootApplication
@EnableDiscoveryClient
public class CustomerMainApplication {
public static void main(String[] args){
SpringApplication.run(CustomerMainApplication.class, args);
}
}

首先,正常情况下访问   http://localhost:8082/customer/callHello

然后停掉节点1,继续访问看是否成功:

请求正常

重新启动节点1,查看节点2的日志:

节点2在丢失节点1之后会不断重试

直到找到节点1,高可用重新生成

如果有什么错误或者纰漏,恳请指正,谢谢

Spring cloud搭建Eureka高可用注册中心的更多相关文章

  1. spring cloud 系列第2篇 —— eureka 高可用注册中心的搭建 (F版本)

    源码仓库地址:https://github.com/heibaiying/spring-samples-for-all 一.项目结构 eureka-server为服务注册中心,负责服务的管理: eur ...

  2. eureka高可用注册中心

    Eureka高可用注册中心 两个配置文件: application-peer1.properties application-peer2.properties 都需要加上 eureka.client. ...

  3. 笔记:Spring Cloud Eureka 高可用注册中心

    在微服务架构这样的分布式环境中,我们需要充分考虑发生故障的情况,所以在生产环境中必须对各个组件进行高可用部署,对与微服务和服务注册中心都需要高可用部署,Eureka 高可用实际上就是将自己作为服务向其 ...

  4. 从零开始学spring cloud(八) -------- Eureka 高可用机制

    一.Eureka高可用机制介绍 Eureka服务器没有后端存储,但注册表中的服务实例都必须发送心跳以使其注册保持最新(因此可以在内存中完成). 客户端还有一个Eureka注册的内存缓存(因此,他们不必 ...

  5. Spring Cloud Eureka 高可用注册中心

    参考:<<spring cloud 微服务实战>> 在微服务架构这样的分布式环境中,各个组件需要进行高可用部署. Eureka Server 高可用实际上就是将自己作为服务向其 ...

  6. spring cloud 使用Eureka作为服务注册中心

    什么是Eureka?  Eureka是在AWS上定位服务的REST服务. Eureka简单示例,仅作为学习参考 在pom文件引入相关的starter(起步依赖) /*定义使用的spring cloud ...

  7. springcloud搭建高可用注册中心的时候注册中心在unavailable-replicas中的问题

    在搭建springcloud eureka高可用注册中心时,发现另一个注册中心一直在unavailable-replicas不可用分片,原因为原来为单个注册中心的时候,禁止了注册中心自主注册为服务和检 ...

  8. (2-1)SpringCloue-Eureka实现高可用注册中心

    高可用注册中心 在微服务架构这样的分布式环境中,我们需要充分考虑发生故障的情况,所以在生产环境中必须对各个组件进行高可用部署.在eureka-server中的application.yml中我们还记得 ...

  9. spring cloud(学习笔记)高可用注册中心(Eureka)的实现(一)

    最近在学习的时候,发现微服务架构中,假如只有一个注册中心,那这个注册中心挂了可怎么办,这样的系统,既不安全,稳定性也不好,网上和书上找了一会,发现这个spring cloud早就想到了,并帮我们解决了 ...

随机推荐

  1. OpenCv的python环境搭建

    1.python的安装参看 http://www.cnblogs.com/samo/p/6734403.html 2.OpenCv安装.opencv2.4.10可以支持vc10/vc11/vc12,o ...

  2. Adobe cs6 全系列软件绿色破解版-一键安装

    下载地址: 链接:https://pan.baidu.com/s/1THssmSS-SnyNc2DW7Wr8cA 提取码:y3tq 软件介绍 作为全球领先的多媒体设计软件供应商,Adobe Syste ...

  3. Python 爬虫从入门到进阶之路(八)

    在之前的文章中我们介绍了一下 requests 模块,今天我们再来看一下 Python 爬虫中的正则表达的使用和 re 模块. 实际上爬虫一共就四个主要步骤: 明确目标 (要知道你准备在哪个范围或者网 ...

  4. 什么是BFC? CSS 如何使用伪元素清除浮动?

    .BFC概念: 块级格式化上下文,是一个独立的渲染区域,让处于 BFC 内部的元素与外部的元素相互隔离,使内外元素的定位不会相互影响. 我们先了解一个名词:BFC(block formatting c ...

  5. 直播技术:从性能参数到业务大数据,浅谈直播CDN服务监控

    线上服务的有效监控和数据收集,一直是后端服务离不开的话题.直播作为一种经典的分布式系统,监控以及数据收集更是必不可少的工作.如何对海量的服务集群有效的监控和保活,又如何抓取集群中的碎片数据中来优化服务 ...

  6. Jenkins Email Extension插件模板

    Jenkins Email Extension插件模板 <!DOCTYPE html> <html> <head> <meta charset="U ...

  7. 请使用迭代查找一个list中最小和最大值,并返回一个tuple

    如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们称为迭代(Iteration). 在Python中,迭代是通过for ... in来完成的,而很多语 ...

  8. BZOJ 1085:[SCOI2005]骑士精神(A*算法)

    题目链接 题意 中文题意. 思路 首先找到空白的格子,因为空白的格子可以和其他的骑士换.从空白的点开始搜索,每次和其他点交换.因为最多只有十五步,可以做16次搜索,搜索的时候,记录走过的步数和至少剩余 ...

  9. django基础知识之模板:

    模板介绍 作为Web框架,Django提供了模板,可以很便利的动态生成HTML 模版系统致力于表达外观,而不是程序逻辑 模板的设计实现了业务逻辑(view)与显示内容(template)的分离,一个视 ...

  10. 2019 Java 全栈工程师进阶路线图,一定要收藏

    技术更新日新月异,对于初入职场的同学来说,经常会困惑该往那个方向发展,这一点松哥是深有体会的. 我刚开始学习 Java 那会,最大的问题就是不知道该学什么,以及学习的顺序,我相信这也是很多初学者经常面 ...