Unified management of adding operation and maintenance modules to services (Phase 4)

1. Background

With the separation of microservices, management issues are involved as the number of services increases. For example, many common management interfaces. The management interface of Nacos. Management interface for circuit breaking and current limiting. And docker management, etc. Therefore, a microservice dedicated to managing various operations and maintenance is necessary.

2. Clear function

1. Solve the pain points of docker management. For example, start a container. Close the container etc. It is a little troublesome to use the command line directly. For java boy, if you can use java to solve it, you don't need the command line. We choose docker -java to complete the related operation java. Then the front-end management page uses iframe to nest the packages provided by each microservice. For example, the package provided by nacos. Nesting into the background management system to complete
still uses the dockerfile to package the image. At that time, when using sentinel-dashboard, it was started by java command line. Pack it up. Then put it into a docker container and manage it together.

3. Operation steps

3.1 Add dependencies

    	 <dependency>
            <groupId>com.github.docker-java</groupId>
            <artifactId>docker-java</artifactId>
            <version>3.2.5</version>
        </dependency>
        
        <dependency>
            <groupId>com.github.docker-java</groupId>
            <artifactId>docker-java-transport-okhttp</artifactId>
            <version>3.2.5</version>
        </dependency>

3.2 Tools

These two dependencies are required to operate docker. Then add the tool class

package com.rose.util;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.LoadImageCmd;
import com.github.dockerjava.api.command.PullImageCmd;
import com.github.dockerjava.api.model.*;
import com.github.dockerjava.core.DockerClientBuilder;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * @author rose
 * @create 2022/11/5
 */

public class DockerProtocol{
    
    


    public DockerClient connectDocker(){
    
    
        DockerClient dockerClient = DockerClientBuilder.getInstance("tcp://192.168.56.20:2375").build();
        Info info = dockerClient.infoCmd().exec();
        return dockerClient;
    }

    //开始容器
    public void startContainer(DockerClient client,String containerId){
    
    
        client.startContainerCmd(containerId).exec();
    }
    //停止容器
    public void stopContainer(DockerClient client,String containerId){
    
    
        client.stopContainerCmd(containerId).exec();
    }
    //加载镜像
    public LoadImageCmd loadImage(DockerClient client, String filePath){
    
    
        LoadImageCmd loadImageCmd = null;
        try {
    
    
            loadImageCmd = client.loadImageCmd(new FileInputStream(filePath));
        } catch (FileNotFoundException e) {
    
    
            e.printStackTrace();
        }
        return loadImageCmd;
    }

    /**
     * repository 镜像名称:tag名称
     **/
    public PullImageCmd pullImage(DockerClient client, String repository){
    
    
        PullImageCmd pullImageCmd = client.pullImageCmd(repository);
        return pullImageCmd;
    }
    //删除镜像
    public void removeImage(DockerClient client,String imageId){
    
    
        client.removeImageCmd(imageId).exec();
    }



    public static void main(String[] args) {
    
    
        DockerProtocol docker = new DockerProtocol();

        DockerClient dockerClient = docker.connectDocker();
        docker.startContainer(dockerClient,"21e7d7460196");
//        docker.startContainer(dockerClient,"38dc3681aa87");
//        docker.stopContainer(dockerClient,"38dc3681aa87");


    }

}

Some commonly used tool classes are prevented here. There is also for better inheritance with the web. Springboot dependencies need to be introduced.
There is one dependency to be aware of.

  <dependency>
            <groupId>com.rose.yan</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <groupId>javax.validation</groupId>
                    <artifactId>validation-api</artifactId>
                </exclusion>

                <exclusion>
                    <groupId>javax.ws.rs</groupId>
                    <artifactId>jsr311-api</artifactId>
                </exclusion>
            </exclusions>


        </dependency>

My project is this common package, which contains a lot of functions such as service discovery, service registration, circuit breaking and current limiting. But one of the packages is conflicting. The javax.ws.rs needs to be excluded. Otherwise, there will be a call failure problem when calling from the web side.

3.3 Calling method of the controller layer

    /**
     * 启动容器.
     * @return
     */
    @GetMapping("/containerStart")
    public GenericResponse containerStart(@RequestParam("type") String file) {
    
    
        try {
    
    
            DockerProtocol dockerProtocol = new DockerProtocol();
            DockerClient dockerClient = dockerProtocol.connectDocker();
            if(ContainerId.RABBITMQ.equals(file)){
    
    
                dockerProtocol.startContainer(dockerClient, ContainerId.RABBIT_ID);
            }
            if(ContainerId.REDIS.equals(file)){
    
    
                dockerProtocol.startContainer(dockerClient, ContainerId.REDIS_ID);
            }
            return GenericResponse.response(ServiceError.NORMAL);
        }catch(Exception e){
    
    
            return GenericResponse.response(ServiceError.DevopsError);
        }


    }

4. dockerfile build image

It is too troublesome to enable sentinel locally every time. Therefore, it is more convenient to use docker image management.

4.1 The first step is to build the docker image yourself

#java 版本
FROM java:8
##挂载的docker卷
VOLUME /tmp
#前者是要操作的jar包  后者自定义jar包名
ADD *.jar sentinel-dashboard.jar
#定义时区参数
ENV TZ=Asia/Shanghai
#设置时区
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo '$TZ' > /etc/timezone
#配置启动命令,-D表示设置JVM参数
ENTRYPOINT ["java","-jar","-Dserver.port=8858","-Dcsp.sentinel.dashboard.server=172.18.181.35:8858","-Dproject.name=sentinel-dashboard","/sentinel-dashboard.jar"]

4.2 Enter the command under the directory

docker build -t saas/collectcode/sentinel-dashboard .(镜像名称自行定义)

4.3 Start the image

docker run --name collectcode_sentinel --restart=always --privileged=true -p 8858:8858 -d saas/collectcode/sentinel-dashboard

access port

おすすめ

転載: blog.csdn.net/qq_21561833/article/details/127821543