首页 > 技术文章 > 今日份学习:部署?

pipemm 2020-02-14 18:04 原文

笔记

Springboot 修改配置文件

Externalized Configuration

Maven Lifecycle

生命周期

使用 Maven exec plugin

pom.xml加入:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.6.0</version>
    <configuration>
        <executable>java</executable>    //要运行什么程序
        <arguments>
            <argument>-classpath</argument>
            <!-- automatically creates the classpath using all project dependencies,
                 also adding the project build directory -->
            <classpath/>
            <argument>com.example.Main</argument>    //运行这个程序的时候用什么参数
        </arguments>
    </configuration>
</plugin>

运行:

# mvn exec:exec

参考于:MojoHaus

emmm....感觉不太方便。

jar包

jar包:编译后的代码和资源打包

# mvn package

mvn xxx -DskipTests 忽略测试

# java -jar xxx.jar/xxx.zip

本质其实还是 #java -classpath com.example.Main

Docker

emmm...好像差不多

实战一个小练习

创建第一个:

使用了 Maven exec plugin:

mvn exec:exec
  1. nginx
docker run --restart=always -v /host/path/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx

run之前需要先把配置文件(nginx.conf)弄好,才能使用nginx load balancing

http {
    upstream myapp1 {
        server abc.com:8080;
        server abc.com:8081;
        server abc.com:8082;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
        }
    }
}

但是这个时候出问题咯:

>docker logs b41720c891ee
2020/02/15 10:06:48 [emerg] 1#1: no "events" section in configuration
nginx: [emerg] no "events" section in configuration
2020/02/15 10:06:49 [emerg] 1#1: no "events" section in configuration
nginx: [emerg] no "events" section in configuration
2020/02/15 10:06:50 [emerg] 1#1: no "events" section in configuration

这个时候只需要在 http {} 前面加一个

event {}

就可以了

>docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS                               NAMES
b41720c891ee        nginx               "nginx -g 'daemon off"   6 minutes ago       Restarting (1) 2 minutes ago   80/tcp                              backstabbing_poincare

这时候已经可以了,docker logs也没再更新了

然后点开abc.com....还没好

但是这个时候好像没有任何东西在监听80端口。。

windows下查看80端口监听情况:netstat -ano | findstr 80

原来,run的时候没有加端口映射 -p ..T^T

新的启动脚本:

docker run --restart=always -v e:/nginx.conf:/etc/nginx/nginx.conf:ro -p 80:80 -d nginx

然后再访问abc.com

502 Bad Gateway
nginx/1.17.8

docker logs :

2020/02/15 10:22:43 [error] 5#5: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "abc.com"
172.17.0.1 - - [15/Feb/2020:10:22:43 +0000] "GET / HTTP/1.1" 502 559 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"

进入docker容器排查问题:

>docker exec -it 226f0d1c8d16 bash

了解了docker容器内访问不到本地的host,导致出错。
所以更换abc.com为局域网的ip(不然只能host映射了。。。)

重启一下容器

>docker restart 226f0d1c8d16

这个时候可以使用了。(nginx监听端口,接收流量,转发到后端服务器。)

创建第二个

使用了jar包的方式

mvn package
java -Dserver.port=8081 -jar my-jar.jar

知识点:spring change port

创建第三个

使用了docker的方式:
Dockerfile:

FROM java:openjdk-8u111-alpine

RUN mkdir /app

WORKDIR /app

COPY target/my-jar.jar /app

EXPOSE 8080

CMD ["java","-jar","my-jar.jar"]

然后就是创建镜像,启动

>docker build .

>docker run -p 8083:8083 镜像ID

但是容器内访问不到其他容器内的东西(redis和mysql是通过docker使用的)
所以还得需要配置外部化:spring Externalized Configuration

spring官方文档的链接中的23.3方法:

vim E:/application.properties
docker run -v E:/application.properties:/app/config/application.properties -p 8083:8080 Build后的镜像ID

推荐阅读