spring cloud实战 1-高可用注册中心

Posted lijianming180

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring cloud实战 1-高可用注册中心相关的知识,希望对你有一定的参考价值。

创建父maven项目

技术图片

提交代码至GitHub

技术图片

创建eureka-server-1

项目搭建两种方式:

  1. 父pom中继承spring-boot-starter-parent,子pom中直接结成父pom。该方式比较方便,但子项目都是spring boot项目了。
  2. 父项目不需要继承spring-boot-starter-parent,子pom中通过使用scope = import依赖关系。
1
2
3
4
5
6
7
8
9
10
11
12
<dependencyManagement>  
<dependencies>
<dependency>

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

参考文档 http://tengj.top/2017/02/26/springboot1/

EurekaServer1Application中声明@EnableEurekaServer

大专栏  spring cloud实战 1-高可用注册中心ng>在默认情况下,Eureka会将自己也作为客户端尝试注册,所以在单机模式下,我们需要禁止该行为

创建eureka-server-2

  1. 修改hosts文件,C:WINDOWSsystem32driversetchosts,新增
    127.0.0.1 eureka1 eureka2
  2. 修改application.yml文件,如下为eureka2中的部分配置,eureka1修改同理。
1
2
3
4
5
6
7
8
9
10
11
12
13
spring:  
profiles:
active: eureka2
eureka:
instance:
#主机名
hostname: eureka2
client:
register-with-eureka: false
fetch-registry: false
service-url:
#将自己注册到eureka1
defaultZone: http://eureka1:8761/
  1. 将服务注册到高可用eureka中心,修改如下即可。
1
2
3
4
eureka:  
client:
serviceUrl:
defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka

eureka-server

高可用注册中心在本项目中用不到,所以另外创建一个eureka-server,之后的项目均注册到此
复制eureka-server-1修改一下即可。

以上是关于spring cloud实战 1-高可用注册中心的主要内容,如果未能解决你的问题,请参考以下文章

Spring cloud之Eureka注册中心高可用

Spring Cloud Eureka 4 (高可用服务注册中心)

基于Spring Cloud的微服务构建学习-3 服务治理-Spring Cloud Eureka之高可用注册中心

实战_Spring_Cloud

Spring cloud Eureka 服务治理(高可用服务中心)

Spring Cloud构建微服务架构高可用服务注册中心