Eureka集群搭建
高可用集群配置
当注册中心扛不住高并发的时候,这时候 要用集群来扛
普通操作
我们再新建两个module
microservice-eureka-server-2002 microservice-eureka-server-2003
1、pom.xml 把依赖加下
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
2、2002 2003的主启动类EurekaServerApplication_2002,EurekaServerApplication_2003复制修改下
package com.su.microserviceeurekaserver2003;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class MicroserviceEurekaServer2003Application {
public static void main(String[] args) {
SpringApplication.run(MicroserviceEurekaServer2003Application.class, args);
}
}
3、正常来说一台机器是不允许做集群的,所有这里先要处理下进入C:\Windows\System32\drivers\etc 打开hosts,加配置
4、修改三个项目的application.yml
2001
server:
port: 2001
context-path: /
eureka:
instance:
# 单机 hostname: localhost #eureka注册中心实例名称
hostname: eureka2001.su.com # 集群
client:
register-with-eureka: false #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
service-url:
defaultZone: http://eureka2002.su.com:2002/eureka/,http://eureka2003.su.com:2003/eureka/ # 集群
#单机defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka注册中心交互的地址,查询服务和注册服务用到
2002
server:
port: 2002
context-path: /
eureka:
instance:
# 单机 hostname: localhost #eureka注册中心实例名称
hostname: eureka2002.su.com # 集群
client:
register-with-eureka: false #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
fetch-registry: false #false 由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
service-url:
defaultZone: http://eureka2001.su.com:2001/eureka/,http://eureka2003.su.com:2003/eureka/ # 集群
#单机defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka注册中心交互的地址,查询服务和注册服务用到
2003
server:
port: 2003
context-path: /
eureka:
instance:
# 单机 hostname: localhost #eureka注册中心实例名称
hostname: eureka2003.su.com # 集群
client:
register-with-eureka: false #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
fetch-registry: false #false 由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
service-url:
defaultZone: http://eureka2001.su.com:2001/eureka/,http://eureka2002.su.com:2002/eureka/ # 集群
#单机defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka注册中心交互的地址,查询服务和注册服务用到
最后我们测试下:
启动三个注册中心,以及服务提供者项目;
然后浏览器地址栏输入:
http://eureka2001.javaxl.com:2001/
http://eureka2002.javaxl.com:2002/
http://eureka2003.javaxl.com:2003/
骚操作(三合一)
从上面普通操作可以看出来。三个注册中心只是application.yml文件不一样,其他的都是一样的。
只写一个项目
启动类
package com.su.microserviceeurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class MicroserviceEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceEurekaServerApplication.class, args);
}
}
application.yml
---
server:
port: 2001
context-path: /
eureka:
instance:
hostname: eureka2001.su.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka2002.su.com:2002/eureka/,http://eureka2003.su.com:2003/eureka/
spring:
profiles: eureka2001
---
server:
port: 2002
context-path: /
eureka:
instance:
hostname: eureka2002.su.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka2001.su.com:2001/eureka/,http://eureka2003.su.com:2003/eureka/
spring:
profiles: eureka2002
---
server:
port: 2003
context-path: /
eureka:
instance:
hostname: eureka2003.su.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka2001.su.com:2001/eureka/,http://eureka2002.su.com:2002/eureka/
spring:
profiles: eureka2003
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.su</groupId>
<artifactId>t237microservice</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microservice-eureka-server</artifactId>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
效果和上面一样