本文实战 Eureka Server 集群。
一、Eureka Server 工程
工程代码很简单,工程代码主要包含:
1、pom.xml 文件
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.8.RELEASE
com.shanhy
shanhy-eureka
0.0.1-SNAPSHOT
shanhy-eureka
Eureka project for Spring Boot
1.8
Greenwich.SR3
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
${project.name}
org.springframework.boot
spring-boot-maven-plugin
2、启动主Java类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class ShanhyEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(ShanhyEurekaApplication.class, args);
}
}
3、配置文件 application.yml
spring:
application:
name: shanhy-eureka
---
spring:
profiles: eureka-server1
server:
port: 8761
eureka:
server:
enable-self-preservation: false # 开发环境关掉保护机制,快速发现已经挂掉的服务,生产环境需要设置为true提升服务稳定性
instance:
hostname: eureka-server1
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
client:
serviceUrl:
defaultZone: http://eureka-server2:8762/eureka/,http://eureka-server3:8763/eureka/
register-with-eureka: true
---
spring:
profiles: eureka-server2
server:
port: 8762
eureka:
server:
enable-self-preservation: false # 开发环境关掉保护机制,快速发现已经挂掉的服务,生产环境需要设置为true提升服务稳定性
instance:
hostname: eureka-server2
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
client:
serviceUrl:
defaultZone: http://eureka-server1:8761/eureka/,http://eureka-server3:8763/eureka/
register-with-eureka: true
---
spring:
profiles: eureka-server3
server:
port: 8763
eureka:
server:
enable-self-preservation: false # 开发环境关掉保护机制,快速发现已经挂掉的服务,生产环境需要设置为true提升服务稳定性
instance:
hostname: eureka-server3
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
client:
serviceUrl:
defaultZone: http://eureka-server1:8761/eureka/,http://eureka-server2:8762/eureka/
register-with-eureka: true
4、打包和启动
先使用 mvn package
打包
然后配置本地 host 用于测试
127.0.0.1 eureka-server1
127.0.0.1 eureka-server2
127.0.0.1 eureka-server3
最后指定不同 profile 启动3个集群节点
java -jar shanhy-eureka.jar --spring.profiles.active=eureka-server1
java -jar shanhy-eureka.jar --spring.profiles.active=eureka-server2
java -jar shanhy-eureka.jar --spring.profiles.active=eureka-server3
在没有全部都启动完成之前,控制台会报错的,意思是还有集群节点服务链接不上。
完全启动后,浏览器分别访问 http://localhost:8761、http://localhost:8762、http://localhost:8763 可以查看集群状态。
5、docker-compose 编排示例
前提是你的工程已经使用 dockerfile 构建完成(这个本文不演示)
假设我们构建的镜像名为 xzxiaoshan/eureka-server:latest
然后创建 docker-compose.yml 内容如下:
version: "3"
services:
eureka-server1:
image: xzxiaoshan/eureka-server:latest
container_name: eureka-server1
hostname: eureka-server1
networks:
- eureka-net
ports:
- "8761:8761"
environment:
- spring.profiles.active=eureka-server1
eureka-server2:
image: xzxiaoshan/eureka-server:latest
hostname: eureka-server2
container_name: eureka-server2
networks:
- eureka-net
ports:
- "8762:8762"
environment:
- spring.profiles.active=eureka-server2
eureka-server3:
image: xzxiaoshan/eureka-server:latest
hostname: eureka-server3
container_name: eureka-server3
networks:
- eureka-net
ports:
- "8763:8763"
environment:
- spring.profiles.active=eureka-server3
networks:
eureka-net:
driver: bridge
这个 docker-compose 文件意在示范使用docker 启动eureka服务,实际上这是一个假集群,因为在实际生产中,集群肯定落实在不同的物理机上。不同的物理机之间的docker访问,你可能需要参考一下文章 《Docker 跨主机网络通讯 flannel+etcd》 。
如果你用了 flannel+etcd 方案,建议所有服务都使用 docker run 来启动,因为docker-compose 会强制给你管理网络,会出现一些问题。
3个eureka 服务实例的启动脚本示例:
docker run -itd --name eureka-server1 -p 8761:8761 -e spring.profiles.active=eureka-server1 -e eureka.client.serviceUrl.defaultZone=http://192.168.1.164:8762/eureka/,http://192.168.1.164:8763/eureka/ 192.168.1.163:81/gukesoft/shanhy-eureka:latest
docker run -itd --name eureka-server2 -p 8762:8762 -e spring.profiles.active=eureka-server2 -e eureka.client.serviceUrl.defaultZone=http://192.168.1.164:8761/eureka/,http://192.168.1.164:8763/eureka/ 192.168.1.163:81/gukesoft/shanhy-eureka:latest
docker run -itd --name eureka-server3 -p 8763:8763 -e spring.profiles.active=eureka-server3 -e eureka.client.serviceUrl.defaultZone=http://192.168.1.164:8761/eureka/,http://192.168.1.164:8762/eureka/ 192.168.1.163:81/gukesoft/shanhy-eureka:latest
PS:如果你上了K8S和ServiceMesh,Eureka 可能你就不需要了。
(END)