openeuler/bisheng-jdk容器镜像实践

简 介

  • 本文选用的操作系统是openEuler, jdk是华为的bisheng-jdk:8,注册中心是netflix的eureka
  • 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容器镜像大家感兴趣也可以了解和实践。

  1. 如果在openEuler基础镜像或openEuler其他应用镜像使用中存在问题,可以提交自己的问题和建议
  2. openEuler容器镜像使用介绍
  3. openEuler基础镜像使用实践
  • 11
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值