搭建单节点Eureka Server
创建 eureka-server 项目,其pom文件如下:
<!-- eureka 服务端依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!--打包插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.yangsx95.demo.netflix.EurekaServerApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
创建一个SpringBoot启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author yangsx
* @version 1.0
* @date 2020/5/30
*/
@SpringBootApplication
@EnableEurekaServer // 启用EurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
配置项目相关参数:
spring:
application:
name: eureka-server
server:
port: 8761
启动项目后访问, http://localhost:8761/即可加载主页EurekaServer主页:
![file](https://i-blog.csdnimg.cn/blog_migrate/1a6f2ba2bb8ce5ec000939cae29b7ff6.png)
搭建高可用Eureka Server
在生产环境中,由于单点环境可用性较差,所以一般不会部署单点服务。幸好Eureka Server本身就支持集群部署,这里演示部署三节点的Eureka Server集群。Eureka Server集群的原理就是每个Server
在application.yml
中添加三个profile,分别配置集群三个节点的信息:
spring:
application:
name: eureka-server-ha
# 分别定义三个配置信息
---
spring:
profiles: peer1
server:
port: 8761
eureka:
instance:
# 主机名,需要在/etc/hosts中配置
hostname: peer1
client:
# Eureka Server本身也集成了Eureka Client,彼此通过Eureka Client同步数据给其它实例又或者从其他实例同步数据
# 是否注册到eureka上,如果需要将当前eureka注册到其他eureka(defaultZone中定义的)上,则设置true
# 如果是单机环境,可以设置为false
register-with-eureka: false
# 是否从其他eureka中同步数据,当前为集群环境,需要同步
fetch-registry: true
serviceUrl:
# 将自己注册到peer2这个Eureka上面去
defaultZone: http://peer2:8762/eureka/,http://peer2:8763/eureka/
---
spring:
profiles: peer2
server:
port: 8762
eureka:
instance:
hostname: peer2
client:
register-with-eureka: false
fetch-registry: true
serviceUrl:
defaultZone: http://peer1:8761/eureka/,http://peer1:8763/eureka/
---
spring:
profiles: peer3
server:
port: 8763
eureka:
instance:
hostname: peer3
client:
register-with-eureka: false
fetch-registry: true
serviceUrl:
defaultZone: http://peer1:8761/eureka/,http://peer1:8761/eureka/