logo头像

分享技术,品味人生

瑞吉实战21-Spring之helloworld

瑞吉实战21–SpringBoot(HelloWorld)

实验内容

  • SpringBoot HelloWorld程序搭建

  • 增加会话处理,方便负载均衡测试

一、开发环境配置

开发环境主要包括:

  • jdk
  • maven
  • git
  • idea

具体操作可以参看【开发环境配置】章节

二、nginx常用命令使用

三、SpringBoot集成

  • 新建spring initializr工程,选择jdk8, sping/web

  • 添加application平级下的controller.TestController.java
package com.iyyxx.springboothelloworld.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @className: TestController
 * @description: TODO 类描述
 * @author: eric 4575252@gmail.com
 * @date: 2022/9/21/0021 9:19:36
 **/
@RestController
public class TestController {

    @GetMapping("/hello")
    public String test(){
        return "hello world";
    }
}
  • 右上角bug启动,web访问
    image-20220921092217394

3.1、集群测试(增加IP、计数器)

增加ip、计数器,方便nginx负载均衡测试时观察!

    @GetMapping("/hello")
    public String hello(HttpServletRequest request){
        Integer count = (Integer)request.getSession().getAttribute("count");
        if(count!=null){
            count+=1;
        }else{
            count = 1;
        }
        request.getSession().setAttribute("count", count);
        String IP = null;
        try {
            IP = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
        return "hello world "+IP+" "+ count;
    }
}

3.2、Idea集成Docker

重头戏!IDEA 与Docker远程服务器的集成

这边走了不少弯路,用的是idea2021的版本,死活不行,用了2022.1的最新版就OK了~

步骤一: Docker服务器开放2375端口

ssh到服务器上进行配置、重启服务并验证

vim /lib/systemd/system/docker.service#修改ExecStart这行
ExecStart=/usr/bin/dockerd  -H tcp://0.0.0.0:2375  -H unix:///var/run/docker.sock


systemctl daemon-reload
systemctl restart docker

curl localhost:2375/info  //如果可以正常响应json信息代表开启成功

步骤二: Idea配置Docker

这步一定要看到底部提示连接成功!

步骤三: 修改工程pom配置

添加dockerFile,约定规范,文件存放在java源码平级目录

FROM java:8  
COPY demo.jar /usr/local/  
WORKDIR /usr/local/  
CMD java -jar demo.jar

修改pom文件,添加properties,添加plugin

<properties>  
    <docker.image.prefix>lzh</docker.image.prefix>  
</properties>


  
<build>  
    <finalName>demo</finalName>  
    <plugins>        <plugin>            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-maven-plugin</artifactId>  
        </plugin>  
  
  
        <plugin><!--制作docker镜像的maven插件-->  
            <groupId>com.spotify</groupId>  
            <artifactId>docker-maven-plugin</artifactId>  
            <version>1.2.2</version>  
            <executions>                <execution>                    <id>build-image</id>  
                    <phase>package</phase>  
                    <goals>                        <goal>build</goal>  
                    </goals>                </execution>            </executions>            <configuration>                <imageName>${docker.image.prefix}/${project.artifactId}</imageName><!--镜像名-->  
                <imageTags>  
                    <imageTag>latest</imageTag>  
                </imageTags>                <dockerDirectory>src/main/docker</dockerDirectory><!--Dockerfile所在的目录-->  
                <dockerHost>http://192.168.20.151:2375</dockerHost><!--docker所在的宿主机地址-->  
                <resources>  
                    <resource><!--这里配置的就是打包后jar所在的位置-->  
                        <targetPath>/</targetPath>  
                        <directory>${project.build.directory}</directory>  
                        <include>${project.build.finalName}.jar</include>  
                    </resource>                </resources>            </configuration>        </plugin>  
  
    </plugins></build>

这边执行maven面板的clean、package操作,一切顺利,看到了docker分层编译!

步骤四: 联调联试
在idea下方service中,找到docker,如果没有启动按一下,找到我们编译好上传的image,右键创建容器,这样就自动生成了运行配置

对启动配置设定镜像名称、容器名称,并增加运行参数,还可以预览,下方还可以设置启动前进行maven重新编译和打包,这样就完成了自动化!

修改代码后,重新执行,可以看到页面发生了及时变化

评论系统未开启,无法评论!