微服务项目多种部署方式
·
【k8s】的方式部署:
打包镜像并推送至自己的远程镜像厂库(阿里云、harbor)等等都可以,然后安装 k8s 从镜像仓库拉去镜像并运行部署
打包镜像并推送至远程仓库:
配置pom.xml:
<properties>
<!-- 项目版本号 -->
<nacos.username>nacos</nacos.username>
<nacos.password>nacos</nacos.password>
<revision>3.8.2</revision>
<spring-boot.version>3.3.5</spring-boot.version>
<spring-cloud.version>2023.0.3</spring-cloud.version>
<spring-cloud-alibaba.version>2023.0.1.3</spring-cloud-alibaba.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<spring-boot-admin.version>3.3.5</spring-boot-admin.version>
<spring.authorization.version>1.3.3</spring.authorization.version>
<screw.version>0.0.3</screw.version>
<captcha.version>2.2.4</captcha.version>
<aws.version>1.12.675</aws.version>
<velocity.version>2.4</velocity.version>
<velocity.tool.version>3.1</velocity.tool.version>
<configuration.version>1.10</configuration.version>
<jasypt.version>3.0.5</jasypt.version>
<jaxb.version>4.0.5</jaxb.version>
<knife4j.version>3.0.5</knife4j.version>
<swagger.fox.version>3.0.0</swagger.fox.version>
<xxl-job.version>2.4.0</xxl-job.version>
<images.version>1.0.0</images.version>
<docker.plugin.version>0.45.1</docker.plugin.version>
<docker.host>http://127.0.0.1:2375</docker.host>
<docker.registry.chengdu.aliyuncs.com</docker.registry>
<docker.namespace>mentalimages</docker.namespace>
<docker.username>admin</docker.username>
<docker.password>123</docker.password>
<git.commit.plugin>9.0.1</git.commit.plugin>
<spring.checkstyle.plugin>0.0.43</spring.checkstyle.plugin>
<flatten-maven-plugin.version>1.6.0</flatten-maven-plugin.version>
<maven-compiler-plugin.version>3.13.0</maven-compiler-plugin.version>
</properties>
<build>
<finalName>${project.name}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<finalName>${project.build.finalName}</finalName>
<layers>
<enabled>true</enabled>
</layers>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!--maven docker 打包插件 -->
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>${docker.plugin.version}</version>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- Docker Remote Api-->
<dockerHost>${docker.host}</dockerHost>
<!-- Docker 镜像私服-->
<registry>${docker.registry}</registry>
<!-- 认证信息-->
<authConfig>
<username>${docker.username}</username>
<password>${docker.password}</password>
</authConfig>
<images>
<image>
<!-- 镜像名称: 172.17.0.111/library/mental-gateway:2.6.3-->
<name>${docker.registry}/${docker.namespace}/${project.name}:${images.version}</name>
<build>
<dockerFile>${project.basedir}/Dockerfile</dockerFile>
</build>
</image>
</images>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<!-- 统一 revision 版本 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>${flatten-maven-plugin.version}</version>
<configuration>
<flattenMode>resolveCiFriendliesOnly</flattenMode>
<updatePomFile>true</updatePomFile>
</configuration>
<executions>
<execution>
<id>flatten</id>
<phase>process-resources</phase>
<goals>
<goal>flatten</goal>
</goals>
</execution>
<execution>
<id>flatten.clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<target>${maven.compiler.target}</target>
<source>${maven.compiler.source}</source>
<encoding>UTF-8</encoding>
<parameters>true</parameters>
</configuration>
</plugin>
<!--打包jar 与git commit 关联插件-->
<plugin>
<groupId>io.github.git-commit-id</groupId>
<artifactId>git-commit-id-maven-plugin</artifactId>
<version>${git.commit.plugin}</version>
<executions>
<execution>
<id>get-the-git-infos</id>
<phase>initialize</phase>
</execution>
</executions>
<configuration>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<!--因为项目定制了jackson的日期时间序列化/反序列化格式,因此这里要进行配置,不然通过management.info.git.mode=full进行完整git信息监控时会存在问题-->
<dateFormat>yyyy-MM-dd HH:mm:ss</dateFormat>
<includeOnlyProperties>
<includeOnlyProperty>^git.build.(time|version)$</includeOnlyProperty>
<includeOnlyProperty>^git.commit.(id|message|time).*$</includeOnlyProperty>
</includeOnlyProperties>
</configuration>
</plugin>
<!--
代码格式插件,默认使用spring 规则,可运行命令进行项目格式化:./mvnw spring-javaformat:apply 或 mvn spring-javaformat:apply,可在IDEA中安装插件以下插件进行自动格式化:
https://repo1.maven.org/maven2/io/spring/javaformat/spring-javaformat-intellij-idea-plugin
-->
<plugin>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-maven-plugin</artifactId>
<version>${spring.checkstyle.plugin}</version>
<executions>
<execution>
<phase>validate</phase>
<inherited>true</inherited>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>dev</id>
<properties>
<!-- 环境标识,需要与配置文件的名称相对应 -->
<profiles.active>dev</profiles.active>
</properties>
<activation>
<!-- 默认环境 -->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<!-- 环境标识,需要与配置文件的名称相对应 -->
<profiles.active>test</profiles.active>
</properties>
<modules>
<module>mental-register</module>
<module>mental-gateway</module>
<module>mental-auth</module>
<module>mental-upms</module>
<module>mental-common</module>
<module>mental-visual</module>
</modules>
</profile>
<profile>
<id>prod</id>
<properties>
<!-- 环境标识,需要与配置文件的名称相对应 -->
<profiles.active>prod</profiles.active>
</properties>
<modules>
<module>mental-register</module>
<module>mental-gateway</module>
<module>mental-auth</module>
<module>mental-upms</module>
<module>mental-common</module>
<module>mental-visual</module>
</modules>
</profile>
</profiles>
编写DockerFile:
放入对应模块下
FROM openjdk:17
WORKDIR /mental-auth
ARG JAR_FILE=target/mental-auth.jar
COPY ${JAR_FILE} app.jar
EXPOSE 3000
ENV TZ=Asia/Shanghai JAVA_OPTS="-Xms128m -Xmx256m -Djava.security.egd=file:/dev/./urandom"
CMD sleep 60; java $JAVA_OPTS -jar app.jar
安装k8s:
这里使用kubersphere 的 在 Linux 上以 All-in-One 模式安装 KubeSphere 方式:
【docker】的方式部署:
编写DockerFile:
在每个模块 编写 DockerFile
FROM openjdk:17
WORKDIR /mental-gateway
ARG JAR_FILE=target/mental-gateway.jar
COPY ${JAR_FILE} app.jar
EXPOSE 9999
ENV TZ=Asia/Shanghai JAVA_OPTS="-Xms128m -Xmx256m -Dfile.encoding=utf-8"
CMD sleep 60; java $JAVA_OPTS -jar app.jar
编写docker-compose.yml:
在项目根路径编写docker-compose.yml
version: '3'
services:
mental-mysql:
build:
context: ./db
environment:
MYSQL_ROOT_HOST: "%"
MYSQL_ROOT_PASSWORD: XXXXXX #数据库密码
restart: always
container_name: mental-mysql
image: mental-mysql
ports:
- 3306:3306
volumes:
- /var/mysql/data:/var/lib/mysql
networks:
- mental_network_default
mental-redis:
image: registry.cn-hangzhou.aliyuncs.com/dockerhub_mirror/redis
ports:
- 6379:6379
restart: always
container_name: mental-redis
hostname: mental-redis
networks:
- mental_network_default
mental-register:
build:
context: mental-register
restart: always
ports:
- 8848:8848
- 9848:9848
container_name: mental-register
hostname: mental-register
image: mental-register
networks:
- mental_network_default
mental-gateway:
build:
context: mental-gateway
restart: always
ports:
- 9999:9999
container_name: mental-gateway
hostname: mental-gateway
image: mental-gateway
networks:
- mental_network_default
mental-auth:
build:
context: mental-auth
restart: always
container_name: mental-auth
hostname: mental-auth
image: mental-auth
networks:
- mental_network_default
mental-upms:
build:
context: mental-upms/mental-upms-biz
restart: always
container_name: mental-upms
hostname: mental-upms
image: mental-upms
networks:
- mental_network_default
mental-screen:
build:
context: mental-screen/mental-screen-biz
restart: always
container_name: mental-screen
hostname: mental-screen
image: mental-screen
networks:
- mental_network_default
mental-monitor:
build:
context: mental-visual/mental-monitor
restart: always
ports:
- 5001:5001
container_name: mental-monitor
hostname: mental-monitor
image: mental-monitor
networks:
- mental_network_default
# mental-codegen:
# build:
# context: ./mental-visual/mental-codegen
# restart: always
# container_name: mental-codegen
# hostname: mental-codegen
# image: mental-codegen
# networks:
# - mental_network_default
# mental-quartz:
# build:
# context: ./mental-visual/mental-quartz
# restart: always
# image: mental-quartz
# container_name: mental-quartz
# networks:
# - mental_network_default
networks: #配置自定义桥接网络,可以通过容器名实现容器之间的通信
mental_network_default:
name: mental_network_default
driver: bridge
将工程打包成jar包:
将项目打成压缩包传到服务器上,解压,在项目更目录执行
# 构建镜像
docker compose build
# 启动容器 (-d 后台启动,建议第一次不要加,方便看错误)
docker compose up -d
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)