pom.xml配置文件
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.idea360</groupId> <artifactId>idea360-kafka-spring-boot-demo</artifactId> <version>0.0.1</version> <name>dea360-kafka-spring-boot-demo</name> <description>Demo project for Spring Boot</description>
<properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.3.7.RELEASE</spring-boot.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.example</groupId> <artifactId>idea360-kafka-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
<profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <profileActive>dev</profileActive> </properties> </profile> <profile> <id>test</id> <properties> <profileActive>test</profileActive> </properties> </profile> <profile> <id>prod</id> <properties> <profileActive>prod</profileActive> </properties> </profile> </profiles>
<build> <finalName>${project.artifactId}</finalName> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>bootstrap.yml</include> <include>application.yml</include> <include>application-${profileActive}.yml</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.3.7.RELEASE</version> <configuration> <mainClass>cn.idea360.springdemo.SpringDemoApplication</mainClass> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>3.1.0</version> <configuration> <descriptors> <descriptor>src/main/assembly/assembly.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
</project>
|
assembly.xml打包配置文件
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| <?xml version="1.0" encoding="UTF-8"?> <assembly> <id>${profileActive}-${project.version}</id>
<formats> <format>tar.gz</format> </formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet> <directory>${basedir}/src/bin</directory> <outputDirectory>bin</outputDirectory> <fileMode>0755</fileMode> <includes> <include>**.sh</include> <include>**.bat</include> </includes> </fileSet>
<fileSet> <directory>${basedir}/target/classes</directory> <outputDirectory>config</outputDirectory> <fileMode>0644</fileMode> <includes> <include>application.yml</include> <include>bootstrap.yml</include> <include>application-${profileActive}.yml</include> <include>mapper/**/*.xml</include> <include>static/**</include> <include>templates/**</include> <include>*.xml</include> <include>*.properties</include> </includes> </fileSet>
<fileSet> <directory>${basedir}/target/lib</directory> <outputDirectory>lib</outputDirectory> <fileMode>0755</fileMode> </fileSet>
<fileSet> <directory>${basedir}/target</directory> <outputDirectory>boot</outputDirectory> <fileMode>0755</fileMode> <includes> <include>${project.build.finalName}.jar</include> </includes> </fileSet>
<fileSet> <directory>${basedir}</directory> <includes> <include>NOTICE</include> <include>LICENSE</include> <include>*.md</include> </includes> </fileSet> </fileSets>
</assembly>
|
代码结构
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| . ├── README.md ├── idea360-kafka-spring-boot-demo.iml ├── pom.xml ├── src │ ├── bin │ │ ├── restart.sh │ │ ├── shutdown.sh │ │ └── startup.sh │ ├── main │ │ ├── assembly │ │ │ └── assembly.xml │ │ ├── java │ │ │ └── cn │ │ │ └── idea360 │ │ │ └── springdemo │ │ │ ├── HelloController.java │ │ │ └── SpringDemoApplication.java │ │ └── resources │ │ ├── application-dev.yml │ │ ├── application-prod.yml │ │ ├── application-test.yml │ │ ├── application.yml │ │ ├── static │ │ └── templates │ └── test │ └── java │ └── cn │ └── idea360 │ └── springdemo │ └── SpringDemoApplicationTests.java └── target ├── archive-tmp ├── classes │ ├── application-test.yml │ ├── application.yml │ └── cn │ └── idea360 │ └── springdemo │ ├── HelloController.class │ └── SpringDemoApplication.class ├── generated-sources │ └── annotations ├── generated-test-sources │ └── test-annotations ├── idea360-kafka-spring-boot-demo │ ├── README.md │ ├── bin │ │ ├── restart.sh │ │ ├── shutdown.sh │ │ └── startup.sh │ ├── boot │ │ └── idea360-kafka-spring-boot-demo.jar │ ├── config │ │ ├── application-test.yml │ │ └── application.yml │ └── logs │ ├── back │ ├── idea360-kafka-spring-boot-demo.log │ └── idea360-kafka-spring-boot-demo_startup.log ├── idea360-kafka-spring-boot-demo-test-0.0.1.tar.gz ├── idea360-kafka-spring-boot-demo.jar ├── idea360-kafka-spring-boot-demo.jar.original ├── maven-archiver │ └── pom.properties ├── maven-status │ └── maven-compiler-plugin │ ├── compile │ │ └── default-compile │ │ ├── createdFiles.lst │ │ └── inputFiles.lst │ └── testCompile │ └── default-testCompile │ ├── createdFiles.lst │ └── inputFiles.lst └── test-classes └── cn └── idea360 └── springdemo └── SpringDemoApplicationTests.class
|
application.yml
1 2 3 4 5 6 7 8 9
| idea360: kafka: name: admin password: 123456 spring: application: name: idea360-kafka-spring-boot-demo profiles: active: @profileActive@
|
打包
1
| mvn clean package -Ptest
|
解压
1
| tar zxvf target/idea360-kafka-spring-boot-demo-test-0.0.1.tar.gz -C target
|
解压后的目录结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| ➜ idea360-kafka-spring-boot-demo tree target/idea360-kafka-spring-boot-demo target/idea360-kafka-spring-boot-demo ├── README.md ├── bin │ ├── restart.sh │ ├── shutdown.sh │ └── startup.sh ├── boot │ └── idea360-kafka-spring-boot-demo.jar ├── config │ ├── application-test.yml │ └── application.yml └── logs ├── back ├── idea360-kafka-spring-boot-demo.log └── idea360-kafka-spring-boot-demo_startup.log
|
查看配置文件是否分环境打包
1 2 3 4 5 6 7
| ➜ idea360-kafka-spring-boot-demo ll -al target/classes total 16 drwxr-xr-x 5 cuishiying staff 160B Jan 6 17:12 . drwxr-xr-x 14 cuishiying staff 448B Jan 6 17:12 .. -rw-r--r-- 1 cuishiying staff 20B Jan 6 17:12 application-test.yml -rw-r--r-- 1 cuishiying staff 170B Jan 6 17:12 application.yml drwxr-xr-x 3 cuishiying staff 96B Jan 6 17:12 cn
|
启动
1
| ./target/idea360-kafka-spring-boot-demo/bin/startup.sh
|
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| #! /bin/bash
APPLICATION="idea360-kafka-spring-boot-demo"
APPLICATION_JAR="${APPLICATION}.jar"
BIN_PATH=$(cd `dirname $0`; pwd)
cd `dirname $0`
cd ..
BASE_PATH=`pwd`
CONFIG_DIR=${BASE_PATH}"/config/"
LOG_DIR=${BASE_PATH}"/logs" LOG_FILE="${APPLICATION}.log" LOG_PATH="${LOG_DIR}/${LOG_FILE}"
LOG_BACK_DIR="${LOG_DIR}/back/"
LOG_STARTUP_PATH="${LOG_DIR}/${APPLICATION}_startup.log"
NOW=$(date +"%Y-%m-%m-%H-%M-%S") NOW_PRETTY=$(date +"%Y-%m-%m %H:%M:%S")
STARTUP_LOG="================================================ ${NOW_PRETTY} ================================================\n"
if [[ ! -d "${LOG_DIR}" ]]; then mkdir "${LOG_DIR}" fi
if [[ ! -d "${LOG_BACK_DIR}" ]]; then mkdir "${LOG_BACK_DIR}" fi
if [[ -f "${LOG_PATH}" ]]; then mv ${LOG_PATH} "${LOG_BACK_DIR}/${APPLICATION}_back_${NOW}.log" fi
echo "" > ${LOG_PATH}
echo "${STARTUP_LOG}" >> ${LOG_STARTUP_PATH}
JAVA_OPT="-server -Xms256m -Xmx256m -Xmn512m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=256m" JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow"
STARTUP_LOG="${STARTUP_LOG}application name: ${APPLICATION}\n"
STARTUP_LOG="${STARTUP_LOG}application jar name: ${APPLICATION_JAR}\n"
STARTUP_LOG="${STARTUP_LOG}application bin path: ${BIN_PATH}\n"
STARTUP_LOG="${STARTUP_LOG}application root path: ${BASE_PATH}\n"
STARTUP_LOG="${STARTUP_LOG}application log path: ${LOG_PATH}\n"
STARTUP_LOG="${STARTUP_LOG}application JAVA_OPT : ${JAVA_OPT}\n"
STARTUP_LOG="${STARTUP_LOG}application background startup command: nohup java ${JAVA_OPT} -jar ${BASE_PATH}/boot/${APPLICATION_JAR} --spring.config.location=${CONFIG_DIR} > ${LOG_PATH} 2>&1 &\n"
nohup java ${JAVA_OPT} -jar ${BASE_PATH}/boot/${APPLICATION_JAR} --spring.config.location=${CONFIG_DIR} > ${LOG_PATH} 2>&1 &
PID=$(ps -ef | grep "${APPLICATION_JAR}" | grep -v grep | awk '{ print $2 }') STARTUP_LOG="${STARTUP_LOG}application pid: ${PID}\n"
echo -e ${STARTUP_LOG} >> ${LOG_STARTUP_PATH}
echo -e ${STARTUP_LOG}
tail -f ${LOG_PATH}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #! /bin/bash
APPLICATION="idea360-kafka-spring-boot-demo"
APPLICATION_JAR="${APPLICATION}.jar"
PID=$(ps -ef | grep "${APPLICATION_JAR}" | grep -v grep | awk '{ print $2 }') if [[ -z "$PID" ]] then echo ${APPLICATION} is already stopped else echo kill ${PID} kill -9 ${PID} echo ${APPLICATION} stopped successfully fi
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #! /bin/bash
APPLICATION="idea360-kafka-spring-boot-demo"
echo stop ${APPLICATION} Application... sh shutdown.sh
echo start ${APPLICATION} Application... sh startup.sh
|
日志管理
如果使用logback进行日志管理, 需要将日志配置文件打包进去即可
- pom.xml配置
1 2 3 4 5 6 7 8 9 10 11 12
| <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>application.yml</include> <include>application-${profileActive}.yml</include> <include>logback-spring.xml</include> </includes> </resource> </resources>
|
- 日志配置
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| <?xml version="1.0" encoding="utf-8"?> <configuration> <contextName>idea360-kafka-spring-boot-demo</contextName>
<property name="log.pattern" value="[eachbot-flow-service] [%X{ip}] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] [%-5level] [%logger{80}] [%method,%line] [%msg]%n"/> <property name="log.pattern.color" value="%yellow[eachbot-flow-service] [%X{ip}] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] %highlight[%-5level] %green[%logger{80}] [%method,%line] %highlight[%msg]%n"/>
<property name="log.path" value="logs"/>
<appender name="console-with-color" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>${log.pattern.color}</pattern> </encoder> </appender>
<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>${log.pattern}</pattern> <charset>UTF-8</charset> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/demo.info.%d.%i.log</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>100MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <maxHistory>30</maxHistory> </rollingPolicy> </appender>
<logger name="cn.idea360.springdemo" level="debug"/>
<root level="INFO"> <appender-ref ref="console-with-color"/> <appender-ref ref="file_info"/> </root>
</configuration>
|
- 启动脚本startup.sh修改
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| #! /bin/bash
APPLICATION="idea360-kafka-spring-boot-demo"
APPLICATION_JAR="${APPLICATION}.jar"
BIN_PATH=$(cd `dirname $0`; pwd)
cd `dirname $0`
cd ..
BASE_PATH=`pwd`
CONFIG_DIR=${BASE_PATH}"/config/"
NOW=$(date +"%Y-%m-%m-%H-%M-%S") NOW_PRETTY=$(date +"%Y-%m-%m %H:%M:%S")
JAVA_OPT="-server -Xms256m -Xmx256m -Xmn512m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=256m" JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow"
STARTUP_LOG="${STARTUP_LOG}application name: ${APPLICATION}\n"
STARTUP_LOG="${STARTUP_LOG}application jar name: ${APPLICATION_JAR}\n"
STARTUP_LOG="${STARTUP_LOG}application bin path: ${BIN_PATH}\n"
STARTUP_LOG="${STARTUP_LOG}application root path: ${BASE_PATH}\n"
STARTUP_LOG="${STARTUP_LOG}application JAVA_OPT : ${JAVA_OPT}\n"
STARTUP_LOG="${STARTUP_LOG}application background startup command: exec java ${JAVA_OPT} -jar ${BASE_PATH}/boot/${APPLICATION_JAR} --spring.config.location=${CONFIG_DIR} "$@"\n"
exec java ${JAVA_OPT} -jar ${BASE_PATH}/boot/${APPLICATION_JAR} --spring.config.location=${CONFIG_DIR} "$@"
PID=$(ps -ef | grep "${APPLICATION_JAR}" | grep -v grep | awk '{ print $2 }') STARTUP_LOG="${STARTUP_LOG}application pid: ${PID}\n"
|
最后
本文到此结束,感谢阅读。如果您觉得不错,请关注公众号【当我遇上你】,您的支持是我写作的最大动力。
参考