Table of contents
1. Make a jar package (if it is easy to see at a glance, you can directly use the jar at the end)
1. First create a microservice demo2
2. Start the microservice (just right-click on DemoApplication and execute startup)
Note: Errors that may be encountered due to other operations
4. Create a new business class MyController
5. Restart and check if successful
2. Publish microservices and deploy them to docker containers through dockerfile
1. Write a Dockerfile (the file name cannot be changed and D must be capitalized)
2. Build and package it into an image file (switch to the directory to execute)
Foreword: the essence
https://pan.baidu.com/s/1iUPKvr9Y2_vPd44RRzIf8Q 8eub
1. Make a jar package (if it is easy to see at a glance, you can directly use the jar at the end)
1. First create a microservice demo2
2. Start the microservice (just right-click on DemoApplication and execute startup)
Note: Errors that may be encountered due to other operations
java: Unable to access org.springframework.boot.SpringApplication
error class file: /C:/Users/kk/.m2/repository/org/springframework/boot/spring-boot/3.0.4/spring-boot-3.0.4 The .jar!/org/springframework/boot/SpringApplication.class
class file has the wrong version 61.0, should be 52.0
Please delete the file or make sure it is in the correct classpath subdirectory.
Trivia about springboot project:
The springboot3.0 series only supports the JDK17 version (generally the JDK version in pom.xml must also be changed to 17)
The spring2.0 series supports JDK8 version by default (commonly used)
solve:
3. Modify port
4. Create a new business class MyController
package com.example.demo.comtroller;
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;
/**
* @ClassName MyController
* @Description TODO
* @Author wanghaha
* @Date 2023/5/4
**/
@RestController
public class MyController {
@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. Restart and check if successful
http://localhost:6001/order/index http://localhost:6001/order/docker
6. Make jar package
Link: https://pan.baidu.com/s/16POEwpvTBd7ZVThVKv4dKA extraction code: gaz4
( If you don’t want to do the above operations or know everything about it, just use the jar package I prepared)
2. Publish microservices and deploy them to docker containers through dockerfile
1. Write a Dockerfile (the file name cannot be changed and D must be capitalized )
# 基础镜像使用java
FROM java:8
# 作者
MAINTAINER zzyy
# VOLUME 指定临时文件目录为/tmp,在主机/var/lib/docker目录下创建了一个临时文件并链接到容器的/tmp
VOLUME /tmp
# 将jar包添加到容器中并更名为zzyy_docker.jar
ADD demo2-0.0.1-SNAPSHOT.jar zzyy_docker.jar
# 运行jar包
RUN bash -c 'touch /zzyy_docker.jar'
ENTRYPOINT ["java","-jar","/zzyy_docker.jar"]
#暴露6001端口作为微服务
EXPOSE 6001
Upload the microservice jar package and Dockerfile file to the same directory/mydocker
2. Build and package it into an image file (switch to the directory to execute)
docker build -t zzyy_docker:1.6 .
3. Run the container
docker run -d -p 6001:6001 zzyy_docker:1.6
4.Access test
curl 127.0.0.1:6001/order/docker
curl 127.0.0.1:6001/order/index
Then just open the server port and change the local IP to the server IP.