8.Docker微服务实战

文章目录

1、创建微服务

  1. 创建空项目并在其中创建一个Module

​ 这里使用start.aliyun.com代替start.spring.io,这样可以使用jdk8。

  1. 选择需要的配置或者直接在pom文件中手动添加

    xml 复制代码
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.hpu</groupId>
        <artifactId>docker</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>docker</name>
        <description>docker</description>
        <properties>
            <java.version>1.8</java.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <spring-boot.version>2.6.13</spring-boot.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web-services</artifactId>
            </dependency>
    <!--        <dependency>-->
    <!--            <groupId>org.mybatis.spring.boot</groupId>-->
    <!--            <artifactId>mybatis-spring-boot-starter</artifactId>-->
    <!--            <version>2.2.2</version>-->
    <!--        </dependency>-->
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>${spring-boot.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${spring-boot.version}</version>
                    <executions>
                        <execution>
                            <id>repackage</id>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    
    </project>
  2. 修改 resource下application.properties配置文件

    properties 复制代码
    server.port=6001
  3. DockerApplication启动类

    java 复制代码
    package com.hpu.docker;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
    
    @SpringBootApplication
    @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
    public class DockerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DockerApplication.class, args);
        }
    
    }
  4. OrderController业务类

    java 复制代码
    package com.hpu.docker.demos.web;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.UUID;
    
    /**
     * @auther zyn
     * @create 2024-05-19 17:43
     */
    @RestController
    public class OrderController
    {
        @Value("${server.port}")
        private String port;
    
        @RequestMapping("/order/docker")
        public String helloDocker()
        {
            return "hello docker"+"\t"+port+"\t"+ UUID.randomUUID().toString();
        }
    
        @RequestMapping(value ="/order/index",method = RequestMethod.GET)
        public String index()
        {
            return "服务端口号: "+"\t"+port+"\t"+UUID.randomUUID().toString();
        }
    
    }
  5. 执行主启动本地调试

  6. 打jar包上传至节点

    bash 复制代码
    scp /Users/***/zyn/JavaProject/Docker/docker/target/docker-0.0.1-SNAPSHOT.jar root@192.168.49.101:/root/service

2、通过Dockerfile发布微服务

  1. 编写Dockerfile

    dockerfile 复制代码
    # 基础镜像使用java
    FROM java:8
    # 根据操作系统架构不同,x86选择java:8,aarch64选择openjdk:8-jdk,否则报错The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
    #FROM openjdk:8-jdk
    # 作者
    MAINTAINER zyn
    # VOLUME 指定临时文件目录为/tmp,在主机/var/lib/docker目录下创建了一个临时文件并链接到容器的/tmp
    VOLUME /tmp
    # 将jar包添加到容器中并更名为zyn_docker.jar
    ADD docker-0.0.1-SNAPSHOT.jar zyn_docker.jar
    # 运行jar包
    #RUN bash -c 'touch /zyn_docker.jar'
    ENTRYPOINT ["java","-jar","/zyn_docker.jar"]
    #暴露8080端口作为微服务
    EXPOSE 6001
  2. 构建镜像

    bash 复制代码
    [root@zyn01 service]#docker build -t zynservice:1.0 .
    [root@zyn01 service]# docker images
    REPOSITORY     TAG       IMAGE ID       CREATED          SIZE
    zynservice     1.0       990e31e212d2   28 minutes ago   544MB
  3. 运行容器

    docker run -d -p 6001:6001 zynservice:1.0
    
  4. 访问测试

    bash 复制代码
     curl 192.168.49.101:6001/order/index
    服务端口号: 	6001	8706d157-a12b-416c-9325-4e544bcc5e0a
     curl 192.168.49.101:6001/order/docker
     hello docker	6001	d20bba4e-0787-4021-917e-cb39a26b413d
相关推荐
颜淡慕潇2 小时前
【K8S系列】深入解析 Kubernetes 中的 Deployment
后端·云原生·容器·kubernetes
zwm_yy4 小时前
docker-mysql
mysql·docker·容器
yours_Gabriel4 小时前
【SpringBoot问题】IDEA中用Service窗口展示所有服务及端口的办法
java·微服务·springboot
wclass-zhengge5 小时前
02微服务系统与设计(D1_走出微服务误区:避免从单体到分布式单体)
分布式·微服务·架构
FinelyYang6 小时前
docker+容器+redis+minio+java jar,实现开机自启动
运维·docker·容器
黄俊懿9 小时前
【深入理解SpringCloud微服务】Sentinel功能详解
后端·spring·spring cloud·微服务·中间件·架构·sentinel
运维&陈同学9 小时前
【zookeeper04】消息队列与微服务之zookeeper客户端访问
linux·后端·微服务·zookeeper·云原生·消息队列·云计算
hong1616889 小时前
Docker 启动和停止的精准掌舵:操控指南
docker·容器·eureka
zwm_yy9 小时前
docker创建vue镜像
docker·vue