springcloud-Eureka集群(三)
eureka集群之间的相互注册

子模块 springcloud-eureka-7001
- pom.xml
<!--导入Eureka Server依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--热部署工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
- application.yml
##单节点eureka
server:
port: 7001 #指定运行端口
#eureka配置
eureka:
instance:
hostname: localhost #指定主机地址 表示eureka服务端的实例名称
client:
#指定是否要注册到注册中心(默认true) false表示eureka服务不注册自己
register-with-eureka: false
#指定是否要从注册中心获取服务(默认true)false就表明自己是注册中心,不需要注册
fetch-registry: false
service-url: #eureka集群需要引入其他两个集群的地址
defaultZone: http://localhost:7002/eureka/,http://localhost:7003/eureka/
- 主启动类
package com.dong.eureka;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
新建子模块 springcloud-eureka-7002
- pom.xml
<!--导入Eureka Server依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--热部署工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
- application.yml
##单节点eureka
server:
port: 7002 #指定运行端口
#eureka配置
eureka:
instance:
hostname: localhost #指定主机地址 表示eureka服务端的实例名称
client:
#指定是否要注册到注册中心(默认true) false表示eureka服务不注册自己
register-with-eureka: false
#指定是否要从注册中心获取服务(默认true)false就表明自己是注册中心,不需要注册
fetch-registry: false
service-url: #eureka集群需要引入其他两个集群的地址
defaultZone: http://localhost:7001/eureka/,http://localhost:7003/eureka/
- 主启动类
package com.dong.eureka2;
@SpringBootApplication
@EnableEurekaServer
public class EurekaTwoApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaTwoApplication.class,args);
}
}
新建子模块 springcloud-eureka-7003
- pom.xml
<!--导入Eureka Server依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--热部署工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
- application.yml
##单节点eureka
server:
port: 7003 #指定运行端口
#eureka配置
eureka:
instance:
hostname: localhost #指定主机地址 表示eureka服务端的实例名称
client:
#指定是否要注册到注册中心(默认true) false表示eureka服务不注册自己
register-with-eureka: false
#指定是否要从注册中心获取服务(默认true)false就表明自己是注册中心,不需要注册
fetch-registry: false
service-url: #eureka集群需要引入其他两个集群的地址
defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/
- 主启动类
package com.dong.eureka3;
@SpringBootApplication
@EnableEurekaServer
public class EurekaThreeApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaThreeApplication.class,args);
}
}
子模块 springcloud-provider-8001
- pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--我们需要拿到实体类,所以要配置api module-->
<dependency>
<groupId>com.dong</groupId>
<artifactId>springcloud-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<!--日志门面-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--jetty:尝试着用这个当应用服务器,与Tomcat没什么区别-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<!--热部署工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
- application.yml
# Eureka配置:配置服务注册中心地址
eureka:
client:
service-url:
#defaultZone: http://localhost:7001/eureka/
# 然而现在服务发布要发布到3个注册中心上面去
defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/
instance:
instance-id: springcloud-provider #修改Eureka上的默认描述信息
- 主启动类
package com.dong.provider;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
测试
- 依次启动三个eureka集群。
- 然后依此访问下面三个地址。
http://localhost:7001/
http://localhost:7002/
http://localhost:7003/

从上面我们看到DS Replicas并没有显示另外两个集群,这是因为我们配置eureka时instance:hostname: 我们都是命名的localhost。只是端口不一样。所以Eureka识别不出来。
为了效果逼真,我们可以在系统中重命名127.0.0.1。然后将3个集群的instance:hostname:的值分别修改为重命名后的值(此处我就不演示了)。就能看到集群效果。(这个效果是模仿eureka部署到3台服务器上,其实都是127.0.0.1)

修改后重新启动运行,我们随机访问http://eureka7003.com:7003/ ,1和2两个eureka就显示出来了。同理:访问1,就会显示2和3,访问2就会显示1和3

启动provider模块,在分别访问
http://eureka7001.com:7001/
http://eureka7002.com:7002/
http://eureka7003.com:7003/

发现provider被分别注册到三个eureka中了,这样即使一个eureka崩了,还有两个可以继续使用

浙公网安备 33010602011771号