1、建立eureka server微服务,打开启动类,添加@EnableEurekaServer
bash
package cn.iysworld.eurekaservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceApplication.class, args);
}
}
2、为了后续更方便镜像包的构建,以及应用部署,在pom文件中添加jenkins、harbor等相关配置。同时为了更方便的部署eureka高可用,在pom文件中引入nacos做为配置中心。
bash
<?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>cn.iysworld</groupId>
<artifactId>ysworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ys-eureka-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ys-eureka-service</name>
<description>ys-eureka-service</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>1.8</java.version>
<!--换成你的harbor仓库地址-->
<docker.repostory>10.211.55.57:80</docker.repostory>
<!--换成你的仓库项目名称-->
<docker.registry.name>ys</docker.registry.name>
<docker.image.tag>1.0.0</docker.image.tag>
<docker.maven.plugin.version>1.4.10</docker.maven.plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
<build>
<finalName>ys-eureka-service</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>cn.iysworld.yseurekaservice.YsEurekaServiceApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${docker.maven.plugin.version}</version>
<executions>
<execution>
<id>default</id>
<goals>
<!--如果package时不想用docker打包,就注释掉这个goal-->
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<contextDirectory>${project.basedir}</contextDirectory>
<!-- harbor 仓库用户名及密码-->
<useMavenSettingsForAuth>true</useMavenSettingsForAuth>
<repository>${docker.repostory}/${docker.registry.name}/${project.artifactId}</repository>
<tag>${docker.image.tag}</tag>
<buildArgs>
<!-- 要修改成你的target目录-->
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
</plugins>
<resources>
<!-- 指定 src/main/resources下所有文件及文件夹为资源文件 -->
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.directory}/classes</targetPath>
<includes>
<include>**/*</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
这是父工程的pom文件
bash
<?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>
<groupId>cn.iysworld</groupId>
<artifactId>ysworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ysworld</name>
<description>ysworld</description>
<packaging>pom</packaging>
<modules>
<module>ys-eureka-service</module>
</modules>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.version>
<spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
<spring-cloud.version>2021.0.5</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
3、建立Dokerfile文件,后续方便使用jenkins打包,直接与src目录同级别
bash
# 使用一个基础镜像
FROM openjdk:8-jdk-alpine
# 指定镜像制作作者
MAINTAINER ys
# 安装必要的工具(curl、ping、telnet)
RUN apk update && apk add --no-cache curl busybox-extras
# 设置工作目录
VOLUME /app
# 将本地的文件拷贝到容器
ADD target/*jar app.jar
# 设置环境变量 主要为了记录内存溢出时的信息
ENV JAVA_OPTS="-Xmx64m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/app/logs/heapdump.hprof"
# 启动容器后自动执行的命令
CMD ["sh", "-c", "java $JAVA_OPTS -jar app.jar"]
4、配置文件
bash
# Eureka 服务器端口号
server.port=8761
# Eureka 服务器应用名称
spring.application.name=${SPRING_APPLICATION_NAME:ys-eureka-service}
spring.profiles.active=${SPRING_PROFILES_ACTIVE:dev}
# 配置中心
spring.config.import=nacos:${spring.application.name}
spring.cloud.nacos.config.server-addr=${SPRING_CLOUD_NACOS_CONFIG_SERVER_ADDR:10.211.55.56:8848}
spring.cloud.nacos.config.username=${SPRING_CLOUD_NACOS_CONFIG_USERNAME:nacos}
spring.cloud.nacos.config.password=${SPRING_CLOUD_NACOS_CONFIG_PASSWORD:nacos}
spring.cloud.nacos.config.group=DEFAULT_GROUP
spring.cloud.nacos.config.namespace=${SPRING_CLOUD_NACOS_CONFIG_NAMESPACE:ys-dev}
spring.cloud.nacos.config.file-extension=properties
nacos配置中心的配置:三个实例
ys-eureka-service
bash
# Eureka 服务器应用名称
spring.application.name=${SPRING_APPLICATION_NAME:ys-eureka-service}
# Eureka 服务器端口号
server.port=8761
# 禁用 Eureka 服务器中的客户端行为(不注册自身,不获取注册信息)
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.instance.hostname=${spring.application.name}
eureka.client.service-url.defaultZone=http://ys-eureka-service-two:8761/eureka/,http://ys-eureka-service-three:8761/eureka/
# Eureka 仪表板和服务器配置
# 启用自我保护模式,防止过多服务同时下线
eureka.server.enable-self-preservation=true
# 服务剔除检查间隔时间,默认60秒
eureka.server.eviction-interval-timer-in-ms=60000
# Eureka 实例租约配置
# 租约续约频率(秒),客户端每10秒发送一次心跳
eureka.instance.lease-renewal-interval-in-seconds=10
# 租约到期时间(秒),90秒后服务器认为实例不可用
eureka.instance.lease-expiration-duration-in-seconds=90
# 显示 Eureka 仪表板 UI 在根路径下
# 公开所有管理端点
management.endpoints.web.exposure.include=*
# 始终显示健康检查详情
management.endpoint.health.show-details=always
logging.file.path=/app/logs
logging.file.name=/app/logs/${spring.application.name}.log
ys-eureka-service-two
bash
# Eureka 服务器应用名称
spring.application.name=${SPRING_APPLICATION_NAME:ys-eureka-service-two}
# Eureka 服务器端口号
server.port=8761
# 禁用 Eureka 服务器中的客户端行为(不注册自身,不获取注册信息)
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.instance.hostname=${spring.application.name}
eureka.client.service-url.defaultZone=http://ys-eureka-service:8761/eureka/,http://ys-eureka-service-three:8761/eureka/
# Eureka 仪表板和服务器配置
# 启用自我保护模式,防止过多服务同时下线
eureka.server.enable-self-preservation=true
# 服务剔除检查间隔时间,默认60秒
eureka.server.eviction-interval-timer-in-ms=60000
# Eureka 实例租约配置
# 租约续约频率(秒),客户端每10秒发送一次心跳
eureka.instance.lease-renewal-interval-in-seconds=10
# 租约到期时间(秒),90秒后服务器认为实例不可用
eureka.instance.lease-expiration-duration-in-seconds=90
# 显示 Eureka 仪表板 UI 在根路径下
# 公开所有管理端点
management.endpoints.web.exposure.include=*
# 始终显示健康检查详情
management.endpoint.health.show-details=always
logging.file.path=/app/logs
logging.file.name=/app/logs/${spring.application.name}.log
3、ys-eureka-service-three
bash
# Eureka 服务器应用名称
spring.application.name=${SPRING_APPLICATION_NAME:ys-eureka-service-three}
# Eureka 服务器端口号
server.port=8761
# 禁用 Eureka 服务器中的客户端行为(不注册自身,不获取注册信息)
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.instance.hostname=${spring.application.name}
eureka.client.service-url.defaultZone=http://ys-eureka-service:8761/eureka/,http://ys-eureka-service-two:8761/eureka/
# Eureka 仪表板和服务器配置
# 启用自我保护模式,防止过多服务同时下线
eureka.server.enable-self-preservation=true
# 服务剔除检查间隔时间,默认60秒
eureka.server.eviction-interval-timer-in-ms=60000
# Eureka 实例租约配置
# 租约续约频率(秒),客户端每10秒发送一次心跳
eureka.instance.lease-renewal-interval-in-seconds=10
# 租约到期时间(秒),90秒后服务器认为实例不可用
eureka.instance.lease-expiration-duration-in-seconds=90
# 显示 Eureka 仪表板 UI 在根路径下
# 公开所有管理端点
management.endpoints.web.exposure.include=*
# 始终显示健康检查详情
management.endpoint.health.show-details=always
logging.file.path=/app/logs
logging.file.name=/app/logs/${spring.application.name}.log