文章13 | 阅读 3661 | 点赞0
SpringCloud支持Eureka、Zookeeper、Consul作为注册中心,这里我们选择Eureka(其他两种集成和Eureka基本类似)
我们介绍上次单机版的继续我们还是一样分别新建两个maven module工程分别为microservicecloud-eureka-7002 和 microservicecloud-eureka-7003
拷贝microservicecloud-eureka-7001的代码和yml配置以及pom依赖文件到其他两个工程
修改启动类为EurekaServer7002_App.java 和EurekaServer7003_App.java
下面再次展示7001启动类代码
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer // EurekaServer服务器端启动类,接受其它微服务注册进来
public class EurekaServer7001_App {
public static void main(String[] args) {
SpringApplication.run(EurekaServer7001_App.class, args);
}
}
7001的pom依赖文件
<?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>microservicecloud</artifactId>
<groupId>com.atguigu.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservicecloud-eureka-7001</artifactId>
<dependencies>
<!--eureka-server服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--fork : 如果没有该项配置,可能这个devtools不会起作用,即应用不会restart-->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
还记得上次讲的单机版的注册中心的配置吗?这里我再次把单机版的注册中心yml文件配置代码贴出来
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)
不知道有没有发现单机版的注册中心eureka.instance.hostname:localhost
现在要高可用集群的注册中心分别还有两个7002和7003
1,如果还配置相同的服务名称实例eureka.instance.hostname:localhost是不是有点区别哪个和哪个?
下面我们该如何解决才能更加的方便看起来更加的区别明显
域名解析
在C:\Windows\System32\drivers\etc\hosts中添加以下内容
127.0.0.1 eureka7001.com eureka7002.com eureka7003.com
通过域名解析就可以把127.0.0.1映射三个不同的域名,
2,既然高可用就要像注册注册中心注册其他两个
这里我们将三台Eureka伪集群通过defaultCone进行相互注册,保证高可用,我们需要注意三台伪集群各server.port、eureka.instance.hostname、eureka.instance.instance-id需要唯一,并且eureka.client.service-url.defaultZone需要三台机器需要互相注册另外两台机器才可保证高可用,图示如下:
application.yml
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
#单机defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
application.yml
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
#单机defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
application.yml
server:
port: 7003
eureka:
instance:
hostname: eureka7003.com #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
#单机defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
关于pom文件和启动类上面说过了我就不贴代码了
好了关于高可用的eureka的注册中心搭建完成
关于8001的生产服务提供者之前是注册单机的eureka,只将这个微服务注册到一个eureka,
eureka:
client: #客户端注册进eureka服务列表内
service-url:
defaultZone: http://localhost:7001/eureka
instance:
instance-id: microservicecloud-dept8001-hystrix #自定义微服务名称信息
prefer-ip-address: true #访问路径可以显示IP地址
eureka:
client: #客户端注册进eureka服务列表内
service-url:
# defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: microservicecloud-dept8001-hystrix #自定义微服务名称信息
prefer-ip-address: true #访问路径可以显示IP地址
关于生产服务提供者的创建请参考之前讲的:https://blog.csdn.net/ywl470812087/article/details/102535582
只需要在生产服务提供者修改yml文件即可
下面分别启动eureka集群和生产服务提供者,启动成功
下面我们就来访问下:http://eureka7001.com:7001/
同理访问:http://eureka7002.com:7002/ http://eureka7003.com:7003/
由上图可知我们的微服务注册成功,高可用Eureka集群搭建完成。
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/ywl470812087/article/details/102594937
内容来源于网络,如有侵权,请联系作者删除!