随笔 - 52  文章 - 0 评论 - 7 阅读 - 55212
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

摘要: Linux系统 部署 运维 系列导航 阅读全文
posted @ 2023-09-22 10:59 xiaoyaozhe 阅读(424) 评论(0) 推荐(1) 编辑

 

 

Linux系统-部署-运维系列导航

 


 

maven多环境配置,根据激活环境,只打包对应的配置文件

1.项目主pom文件增加多环境配置

<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <!-- 环境标识,需要与配置文件的名称相对应 -->
                <profiles.active>dev</profiles.active>
                <logging.level>debug</logging.level>
                <endpoints.include>'*'</endpoints.include>
            </properties>
            <activation>
                <!-- 默认环境 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
                <logging.level>debug</logging.level>
                <endpoints.include>'*'</endpoints.include>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
                <logging.level>warn</logging.level>
                <endpoints.include>health, info, logfile</endpoints.include>
            </properties>
        </profile>
    </profiles>

 

2.需要打包的模块pom文件激活环境的配置文件

<build>
    <!-- 资源配置 -->
    <resources>
        <!--排除配置文件-->
        <resource>
            <directory>src/main/resources</directory>
            <!-- 先排除所有配置文件 -->
            <excludes>
                <!--使用通配符-->
                <exclude>application*.yml</exclude>
            </excludes>
        </resource>
        <!-- 根据激活条件引入打包所的配置和文件 -->
        <resource>
            <directory>src/main/resources</directory>
            <!-- 开启过滤替换功能-->
            <filtering>true</filtering>
            <!-- 项目打包完成的包中只包含当前环境文件 -->
            <includes>
                <include>application.yml</include>
                 <!--根据maven选择环境导入配置文件-->
                <include>application-${profiles.active}.yml</include>
            </includes>
        </resource>
    </resources>
</build>

 

3.需要打包的模块配置文件(以yml为例)

# Spring配置
spring:
  # 资源信息
  profiles:
    active: @profiles.active@

 

4.maven打包时通过 -P 指定环境变量,如dev、test、prod

mvn clean package -P prod -pl admin

 


maven打包,依赖包统一到 lib 目录,配置文件统一到 config 目录

减小jar包大小,减少迭代升级传输流量

<build>
    <finalName>${project.artifactId}</finalName>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <plugins>
        <!--打包jar -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <!--不打包资源文件 -->
                <excludes>
                    <exclude>*.**</exclude>
                    <exclude>*/*.xml</exclude>
                </excludes>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <!--MANIFEST.MF 中 Class-Path 加入前缀 -->
                        <classpathPrefix>lib/</classpathPrefix>
                        <!--jar包不包含唯一版本标识 -->
                        <useUniqueVersions>false</useUniqueVersions>
                        <!--指定入口类 -->
                        <mainClass>com.romai.RomaiApplication</mainClass>
                    </manifest>
                    <manifestEntries>
                        <!--MANIFEST.MF 中 Class-Path 加入资源文件目录 -->
                        <Class-Path>./config/</Class-Path>
                    </manifestEntries>
                </archive>
                <outputDirectory>${project.build.directory}/deploy</outputDirectory>
            </configuration>
        </plugin>

        <!--拷贝依赖 copy-dependencies -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/deploy/lib/</outputDirectory>
                        <!--Release检查(默认值为false)。如果为true,则将强制覆盖-->
                        <overWriteReleases>false</overWriteReleases>
                        <!--快照检查(默认值=false)。如果为true,则将强制覆盖-->
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <!--如果此值为true,则仅当源比目标更新时(或者目标中不存在),才会复制插件-->
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!--拷贝资源文件 copy-resources -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                                <!-- 这里也要加上,会在复制resources时生效的,不加的话,打出来的包有问题 -->
                                <filtering>true</filtering>
                                <excludes>
                                    <exclude>META-INF/</exclude>
                                    <exclude>application-*.*</exclude>
                                </excludes>
                            </resource>

                            <resource>
                                <directory>src/main/resources</directory>
                                <includes>
                                    <include>application-${profiles.active}.*</include>
                                </includes>
                            </resource>
                        </resources>
                        <outputDirectory>${project.build.directory}/deploy/config</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

 


 

mvn控制台指令,自动打包依赖或被依赖模块

mvn clean install -Dmaven.test.skip=true -pl admin -am

-pl 本次打包的项目列表,如果不指定,则处理当前目录pom文件的所有模块
-am 同时打包 本此项目依赖  的项目
-amd 同时打包 依赖本次项目 的项目

 


spring boot 默认 jackson 序列化忽略空值

spring:
  jackson:
    default-property-inclusion: non_null

 


 

IP工具类,获取客户端真实IP地址

package cn.kt.ipcount.utils;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
 * 获取http请求的真实客户端IP
 */
public class IPUtil {
    /**
     * 获取用户真实IP地址,不使用request.getRemoteAddr();的原因是有可能用户使用了代理软件方式避免真实IP地址。
     * 如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值
     * 真正的用户端的真实IP取X-Forwarded-For中第一个非unknown的有效IP字符串
     *
     * @param request
     * @return
     */
    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if ("127.0.0.1".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip)) {
                //根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                ip = inet.getHostAddress();
            }
        }
        return ip;
    }
}

 


 

静态注入

@Component
public class DemoClass {
/** 方法一:setter方案 */
/** * ISomeService */ private static ISomeService someService; /** * 静态对象注入 * * @param someService */ @Resource public void setSomeService(ISomeService someService) { RomaiWsServer.someService = someService; }
  /** 方法二:PostConstruct方案,需要额外一个实例对象支持 */
/** * ISomeService2 */ @Resource private ISomeService2 someService2; /** * ISomeService2 */ private static ISomeService2 someService2Static; @PostConstruct public void init() { // 注入静态服务 someService2Static = someService2; } }

 


 

Java虚拟机时区设置,效果为Java日志时间

localtime作用于容器操作系统,timezone/TZ 作用于java虚拟机

java -jar -Duser.timezone=Asia/Shanghai

docker java
1.dockerfile RUN echo 'Asia/shanghai'>/etc/timezone
2.dockerfile ENTRYPOINT -Duser.timezone=Asia/Shanghai
3.docker run -e TZ=Asia/Shanghai
4.docker compose environment:- TZ=Asia/Shanghai

 

posted @ 2024-07-02 21:08 xiaoyaozhe 阅读(10) 评论(0) 推荐(0) 编辑
摘要: Linux系统-部署-运维系列导航 MySQL常用安装方式有3种:rpm安装、yum安装、二进制文件安装。 本文介绍yum安装方式。 组件安装操作步骤参考 组件安装部署手册模板,根据不同组件的安装目标,部分操作可以省略。 本文将按照该参考步骤执行。 一、获取组件可执行程序库,包括主程序,此为组件的基 阅读全文
posted @ 2023-09-28 14:01 xiaoyaozhe 阅读(786) 评论(0) 推荐(0) 编辑
摘要: Linux系统-部署-运维系列导航 MySQL常用安装方式有3种:rpm安装、yum安装、二进制文件安装。 本文介绍rpm安装方式。 组件安装操作步骤参考 组件安装部署手册模板,根据不同组件的安装目标,部分操作可以省略。 本文将按照该参考步骤执行。 一、获取组件可执行程序库,包括主程序,此为组件的基 阅读全文
posted @ 2023-09-28 14:00 xiaoyaozhe 阅读(1244) 评论(0) 推荐(0) 编辑
摘要: Linux系统-部署-运维系列导航 文档说明 nginx使用过程中,配置最多的,最难以理解的,也是最容易出问题的,就是location块级指令,本文旨在将location相关配置规范以及使用经验,搜集汇总,便于需要时查看。 特别说明:本文详细内容大部分为网络搜集整理,旨在提供一条学习路线,让我们有条 阅读全文
posted @ 2023-09-27 16:51 xiaoyaozhe 阅读(103) 评论(0) 推荐(0) 编辑
摘要: Linux系统-部署-运维系列导航 Nginx介绍 官方网站为:http://nginx.org/ 。它是一款免费开源的高性能 HTTP 代理服务器及反向代理服务器(Reverse Proxy)产品,同时它还可以提供 IMAP/POP3 邮件代理服务等功能。它高并发性能很好,官方测试能够支撑 5 万 阅读全文
posted @ 2023-09-22 15:59 xiaoyaozhe 阅读(1386) 评论(0) 推荐(0) 编辑
摘要: Linux系统-部署-运维系列导航 Nginx介绍 官方网站为:http://nginx.org/ 。它是一款免费开源的高性能 HTTP 代理服务器及反向代理服务器(Reverse Proxy)产品,同时它还可以提供 IMAP/POP3 邮件代理服务等功能。它高并发性能很好,官方测试能够支撑 5 万 阅读全文
posted @ 2023-09-22 15:58 xiaoyaozhe 阅读(5798) 评论(0) 推荐(0) 编辑
摘要: Linux系统-部署-运维系列导航 -- 连表 SELECT t.* from test_table t inner join (select t1.`name`,max(t1.id) id from test_table t1 group by t1.`name`) t2 on t.id = t2 阅读全文
posted @ 2023-09-06 09:39 xiaoyaozhe 阅读(94) 评论(0) 推荐(0) 编辑
摘要: Linux-Windows系统-部署-运维系列导航 守护程序的指标 开机能启动 正常运行时不守护 手动关闭进程,守护启动 只有一个进程 本文以windows批处理程序(.bat)来演示守护程序,也可以使用其他方式,如Python、VB等脚本语言,当然也可以使用C/C++、C#、Java等高级语言编写 阅读全文
posted @ 2023-09-06 09:22 xiaoyaozhe 阅读(180) 评论(0) 推荐(0) 编辑
摘要: Linux系统-部署-运维系列导航 RabbitMQ介绍 RabbitMQ 是使用Erlang语言开发的基于AMQP标准的开源实现,用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面表现不错 RabbitMQ的特点 1、保证可靠性(Reliability):使用持久化、传输确认、发布确 阅读全文
posted @ 2023-09-05 17:18 xiaoyaozhe 阅读(1614) 评论(0) 推荐(0) 编辑
点击右上角即可分享
微信分享提示