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> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot.version}</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </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 static 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