Docker deploys springcloud project (clear and clear)

overview

Recently, I want to build a cloud project. I found a template project on gitee. The backend uses Nacos, Gateway, Security and other technologies, which need to be deployed in Docker containers . Here is a summary. If there are any deficiencies, I hope you guys can help pointed out.

What is Docker

Docker is developed and implemented using the Go language launched by Google. Based on technologies such as cgroup, namespace, and AUFS-like Union FS of the Linux kernel, Docker encapsulates and isolates processes, which belongs to the virtualization technology at the operating system level. Because the isolated process is independent of the host and other isolated processes, it is also called a container .

On the basis of containers, Docker has carried out further encapsulation, from file system, network interconnection to process isolation, etc., which greatly simplifies the creation and maintenance of containers. This makes Docker technology lighter and faster than virtual machine technology.

The three core concepts of Docker : mirror image, container, warehouse

  • Image : Similar to the image of a virtual machine, as the saying goes, it is an installation file.
  • Container : Similar to a lightweight sandbox, a container creates an application running instance from an image, which can be started, started, stopped, and deleted, and these containers are isolated from each other and invisible to each other.
  • Warehouse : Similar to the code warehouse, it is a place where Docker centrally stores image files.

Deploy the Spring Cloud project

Implementation process

insert image description here

Maven Packaging Plugin

  • maven-jar-plugin : The package will not package the dependent library, and you will find that lib is missing after unzipping it (only the jar of the current module is included, and the dependent jar is not included, so it cannot be started directly).
  • spring-boot-maven-plugin : will package the dependent libraries together into the jar package

My current project configuration

<build>
        <!-- 项目名 -->
		<finalName>${project.artifactId}</finalName>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<!--跳过测试-->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<skipTests>true</skipTests>
				</configuration>
			</plugin>
		</plugins>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
			</resource>
		</resources>
	</build>

distribution of jar packages in Linux
insert image description here
insert image description here

System module

insert image description here

Dockerfile

FROM java:8

MAINTAINER lanys

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone

#添加jar包到容器中
ADD maku-cloud-system.jar  maku-cloud-system.jar

#暴露端口
EXPOSE 8081

ENTRYPOINT ["java", "-server", "-Xms512M", "-Xmx512M", "-Djava.security.egd=file:/dev/./urandom", "-Dfile.encoding=UTF-8", "-XX:+HeapDumpOnOutOfMemoryError", "-jar", "maku-cloud-system.jar" ]

generate mirror

Format:

# docker build -t 镜像名称:版本 .
docker build -t makunet_system:1.0 .

Gateway

insert image description here

Dockerfile

# 构建镜像,执行命令:【docker build -t makunet:1.0 .】

# 网关 docker build -t makunet_gateway:1.0 .
# 后台 docker build -t makunet_system:1.0 .
# 启动compost docker-compose up -d
FROM java:8

MAINTAINER lanys

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone

#添加jar包到容器中
ADD maku-cloud-gateway.jar  maku-cloud-gateway.jar

#暴露端口
EXPOSE 8080

ENTRYPOINT ["java", "-server", "-Xms512M", "-Xmx512M", "-Djava.security.egd=file:/dev/./urandom", "-Dfile.encoding=UTF-8", "-XX:+HeapDumpOnOutOfMemoryError", "-jar", "maku-cloud-gateway.jar" ]

generate mirror

docker build -t makunet_gateway:1.0 .

View the generated effect

 docker images

insert image description here

docker-compose.yml

insert image description here

version: '3.9'
services:
  # 服务定义名称
  maku-cloud-gateway:
    # 指定镜像名称
    image: makunet_gateway:1.0
    # 指定运行服务名称,就是上面
    container_name: maku-cloud-gateway
    # 暴露端口
    ports:
      - 8080:8080
    # 挂载解析
    volumes:
      - /work/www/maku-cloud/maku-cloud-gateway.jar:/app/app.jar
  # 服务定义名称
  maku-cloud-system:
    # 指定镜像名称
    image: makunet_system:1.0
    container_name: maku-cloud-system
    volumes:
      - /work/www/maku-cloud/maku-cloud-system.jar:/app/app.jar

Start docker-compose.yml

Check whether the startup is normal (test):

compost docker-compose up

Start the mount:

compost docker-compose up -d

Open ports and implementation effects

insert image description here
insert image description here

expand

Specify yml file configuration

The Spring Cloud yml configuration file generally needs to specify the corresponding test environment (test) in Nacos, and the production environment (prod) needs to set the compose variable in docker-compose.yml .

docker-compose.yml

maku-cloud-gateway:
    image: makunet:1.0
    container_name: maku-cloud-gateway
    ports:
      - 8080:8080
    # 设置环境变量
    env_file:
      // 指定当前目录下的 app.env 文件
      - app.env
    volumes:
      - /work/www/maku-cloud/maku-cloud-gateway.jar:/app/app.jar

app.env

nacos_host=8.134...
nacos_port=88..
nacos_namespace=53b571a0-08c7-4a2d-b094-32dc....
nacos_group=test
activated_properties=test

bootstrap.yml

server:
  port: 8081

spring:
  application:
    name: maku-cloud-system
  profiles:
    active: ${
    
    nacos_group:"dev"}
  cloud:
    nacos:
      discovery:
        server-addr: ${
    
    nacos_host:127.0.0.1}:${
    
    nacos_port:8848}
        # 命名空间,默认:public
        namespace: ${
    
    nacos_namespace:}
        service: ${
    
    spring.application.name}
      config:
        server-addr: ${
    
    spring.cloud.nacos.discovery.server-addr}
        namespace: ${
    
    spring.cloud.nacos.discovery.namespace}
        file-extension: yaml
        # 指定配置
        extension-configs:
          - data-id: datasour...yaml
            refresh: true
          - data-id: comm...yaml
            refresh: true

Guess you like

Origin blog.csdn.net/qq_44697754/article/details/129679025