SpringCloud(三)Eureka服务注册中心搭载集群
一、Eureka 注册中心集群原理
1.Eureka 没有区分中心节点和从节点,集群中每个节点地位相等,节点之间通过相互注册和相互守望,保证集群可用性。假设注册中心有三个Eureka服务节点 A、B、C,想要搭建 Eureka Server 集群,只需要将 A 节点的信息注册到 B 和 C 节点上,将 B节点的信息注册到 A 和 C 上,将 C 节点的信息注册到 A 和 B 上,这样就完成了服务端集群搭建。
2.正规的集群搭建应该将Eureka服务端部署在不同的ip机器上,由于本地只有一台机器,所以只能通过区分端口号模拟搭建Eureka服务端集群,端口号分别为7001 7002 7003 我们这边需要准备三个eureka注册中心,来搭载eureka集群,由于我们已经有一个了,所有这里我们只需要再新建两个子模块:(shop-eureka-7002 shop-eureka-7003)
二、开始搭建Eureka 注册中心
1.新建两个子模块
2.在新建的两个eureka注册中心导入相同的依赖
<?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">
<parent>
<artifactId>SpringCloud-Demo</artifactId>
<groupId>com.nine.winfred</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shop-eureka-7002</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--eureka 服务端注册-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
3.新建主启动类
package com.nine.winfred;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
4.修改shop-eureka 服务端的application.yml文件
server:
port: 7001
spring:
application:
name: server-eureka
#单机版
#eureka:
# instance:
# hostname: localhost
# client:
# register-with-eureka: false #false表示不向注册中心注册自己
# fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
# service-url:
# defaultZone: http://localhost:7001/eureka/
#集群版
eureka:
instance:
hostname: eureka7001
client:
register-with-eureka: false #false表示不向注册中心注册自己
service-url:
defaultZone: http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/
修改shop-eureka-7002 服务端的application.yml 文件
server:
port: 7002
spring:
application:
name: server-eureka
#集群版
eureka:
instance:
hostname: eureka7002
client:
register-with-eureka: false #false表示不向注册中心注册自己
service-url:
defaultZone: http://eureka7001:7001/eureka/,http://eureka7003:7003/eureka/
修改shop-eureka-7003 服务端的application.yml 文件
server:
port: 7003
spring:
application:
name: server-eureka
#集群版
eureka:
instance:
hostname: eureka7003
client:
register-with-eureka: false #false表示不向注册中心注册自己
service-url:
defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/
5.修改host文件 host文件地址 C:\Windows\System32\drivers\etc\host,将以下文件信息添加到host文件末尾
127.0.0.1 eureka7001
127.0.0.1 eureka7002
127.0.0.1 eureka7003
6.接下来回到项目中,依次启动:eureka7001 eureka7002 eureka7003
(启动第一个跟第二个eureka的时候会报错)这是因为,其他的eureka没有启动,所以他找不到地方注册,暂时不管他,等三个eureka全部启动完成后就可以了。
7.启动完毕
8.打开浏览器访问 http://localhost:7001/
打开浏览器访问 http://localhost:7002/
打开浏览器访问 http://localhost:7003/
三、C:\WINDOWS\system32\drivers\etc\host 文件无法修改
1.找到该文件右击属性