简 介
- 本文选用的操作系统是openEuler, jdk是华为的bisheng-jdk:8,注册中心是netflix的eureka
- openEuler社区目前已发布多款热门应用镜像,开发者可以基于应用镜像进行二次开发,满足实际业务场景
- openEuler镜像仓库:
- openEuler源码仓库:
开源向导
- JDK: BiSheng JDK 8
- eureka: github.com
- 操作系统: openEuler
两套eureka代码
eureka服务端
EnableEurekaServer注解会标记当前应用为Eureka的服务端,应用启动后会作为服务的注册中心对外提供注册功能。
启动类
package eureka.server.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* eureka服务端启动类
* @author Guangjie1
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
yml配置
spring:
application:
name: eureka-server
server:
port: 8001
eureka:
instance:
hostname: localhost
client:
fetch-registry: false #是否注册自身 默认true-注册
register-with-eureka: false #是否检查服务清单 默认true-检查
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
pom依赖
eureka服务端和客户端的pom依赖大部分一致,有区别的只有服务注册发现组件。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/>
</parent>
<dependencyManagement>
<!-- Spring Cloud版本 -->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入服务注册发现组件 eureka依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
Eureka客户端
EnableDiscoveryClient注解会标记当前应用为一个服务提供者,启动时会注册到yml配置的eureka服务端注册中心,对外提供服务。
启动类
package eureka.client.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* Eureka客户端启动类
* @author Guangjie1
*/
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
yml配置
spring:
application:
name: eureka-client
server:
port: 8002
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://x.x.x.x:8001/eureka/ #服务注册中心的Url,x.x.x.x替换为Eureka服务端IP
pom依赖
eureka服务端和客户端的pom依赖大部分一致,有区别的只有服务注册发现组件。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/>
</parent>
<dependencyManagement>
<!-- Spring Cloud版本 -->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入服务注册发现组件 eureka依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
构建eureka镜像
为简化构建流程,本地maven打包好事先准备的eureka-client和eureka-server工程,用dockerfile构建一个简单的eureka-client和eureka-server镜像。本次通过Docker Buildx多平台构建docker镜像。
docker buildx build --platform linux/amd64,linux/arm64 -t guangjie1/bisheng-jdk:eureka-server-1
镜像dockerfile
- 构建eureka-client镜像的dockerfile
ARG BASE=openeuler/bisheng-jdk:1.8.0-oe2203sp3
FROM ${BASE}
WORKDIR /app
COPY EurekaSimpleClient-1.0-SNAPSHOT.jar /app/eureka-client.jar
EXPOSE 8002
ENTRYPOINT [ "java", "-jar", "/app/eureka-client.jar" ]
- 构建eureka-server镜像的dockerfile
ARG BASE=openeuler/bisheng-jdk:1.8.0-oe2203sp3
FROM ${BASE}
WORKDIR /app
COPY EurekaSimpleServer-1.0-SNAPSHOT.jar /app/eureka-server.jar
EXPOSE 8001
ENTRYPOINT [ "java", "-jar", "/app/eureka-server.jar" ]
eureka镜像部署
需要先启动eureka服务端镜像作为注册中心为客户端应用启动时提供注册服务,eureka客户端服务启动时会向eureka服务端注册,注册完成后可以在eureka注册中心页面看到注册的客户端应用。
服务端部署
# 拉取服务端镜像
docker pull guangjie1/bisheng-jdk:eureka-server-1
# 运行服务端容器
docker run -dit --name eureka-server -p 8001:8001 guangjie1/bisheng-jdk:eureka-server-1
服务端容器启动之后,访问http://{eureka-server-ip}:8001
客户端部署
# 拉取客户端镜像
docker pull guangjie1/bisheng-jdk:eureka-client-1
# 运行客户端容器
docker run -dit --name eureka-server -p 8002:8002 guangjie1/bisheng-jdk:eureka-client-1
客户端容器启动之后,再次访问http://{eureka-server-ip}:8001,即可看到注册的应用
Q & A
本文主要介绍了openeuler/bisheng-jdk的使用实践,其他openEuler容器镜像大家感兴趣也可以了解和实践。
- 如果在openEuler基础镜像或openEuler其他应用镜像使用中存在问题,可以提交自己的问题和建议
- openEuler容器镜像使用介绍
- openEuler基础镜像使用实践