no20-springboot项目打包部署监控
分类: springboot 专栏: springboot学习 标签: springboot监控
2023-04-19 22:47:09 839浏览
打jar
前提
1 项目的parent为spring-boot-starter-parent
2 spring-boot-maven-plugin插件
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.7.RELEASE</version> </parent> <build> <finalName>ch9</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
打包命令
mvn package -Dmaven.test.skip=true
运行
java -jar xxx.jar
#&表示让项目在后台运行
java -jar xxx.jar &
#nohup表示当窗口(xshell)关闭时服务不挂起,继续在后台运行
nohup java -jar xxx.jar & nohup java -jar -Dserver.port=8088 xxx.jar &
重命名jar
<build> <finalName>ch9</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
打war
修改pom文件
<packaging>war</packaging> <!-- 将内嵌容器的依赖标记为provided --> <!-- compile 默认的scope,表示 dependency 都可以在生命周期中使用。而且, 这些dependencies 会传递到依赖的项目中。适用于所有阶段,会随着项目一起发布 provided 跟compile相似,但是表明了dependency 由JDK或者容器提供, 例如Servlet AP和一些Java EE APIs。这个scope 只能作用在编译和测试时,同时没有传递性。--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
修改启动类
@SpringBootApplication public class Application extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(Application.class,args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(Application.class); } }
热部署
启动热部署
项目启动时即有reload的过程也有restart的过程
小结
设置自动构建项目
1 开启IDEA自动编译一
2 开启IDEA自动编译二 (2021.2版之前)
同时按下shift+ctrl+alt+/
2021.2版做如下配置即可
3 激活方式
查看DevToolsProperties类
关闭热部署
线上环境运行时是不可能使用热部署功能的,所以需要强制关闭此功能
actuator监控
依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
访问监控信息
http://localhost:8080/actuator/health
http://localhost:8080/actuator/info
增加应用基本信息
info.version=V1.0 info.author=jfit
#健康指标详细点 management.endpoint.health.show-details=always management.endpoints.web.exposure.include=configprops,beans #暴露所有的端点 management.endpoints.web.exposure.include=* #允许通过post请求关闭应用 management.endpoint.shutdown.enabled=true
注意:yml配置文件中 * 号要加单引号或双引号
多个微服务监控
- server服务端
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.3.0</version> </dependency> </dependencies>
@SpringBootApplication @EnableAdminServer public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
- 应用客户端
在所有被监控的模块中加入两个依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.3.0</version> </dependency>
配置文件(指向admin-server)
server.port=8888 spring.boot.admin.client.url=http://localhost:9999 #健康指标详细点 management.endpoint.health.show-details=always #暴露所有的端点 management.endpoints.web.exposure.include=* #允许通过post请求关闭应用 management.endpoint.shutdown.enabled=true info.author=jfit info.vx=jf3qcom
最核心的就是spring.boot.admin.client.url
效果图
链路追踪
安装zipkin
docker pull openzipkin/zipkin:2.12.9 docker run -d -p 9411:9411 --name zipkin imageid
启动后可以直接访问http://192.168.56.16:9411/
依赖
<dependency> <groupId>io.opentracing.contrib</groupId> <artifactId>opentracing-spring-zipkin-web-starter</artifactId> <version>0.1.4</version> </dependency>
配置文件
server.port=81 spring.application.name=app1 opentracing.zipkin.http-sender.base-url=http://192.168.56.16:9411/
微服务调用
@SpringBootApplication public class ZipkinApp { public static void main(String[] args) { SpringApplication.run(ZipkinApp.class, args); } @Bean public RestTemplate restTemplate(ClientHttpRequestFactory factory){ RestTemplate restTemplate= new RestTemplate(factory); // 支持中文编码 restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(Charset.forName("UTF-8"))); return restTemplate; } @Bean public ClientHttpRequestFactory simpleClientHttpRequestFactory(){ SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setReadTimeout(5000);//单位为ms factory.setConnectTimeout(5000);//单位为ms return factory; } }
测试controller
@RestController public class TracingController { @Autowired private RestTemplate restTemplate; @Value("${server.port}") private int port; @RequestMapping("/tracing") public String tracing() throws InterruptedException { Thread.sleep(100); return "tracing"; } @RequestMapping("/open") public String open() throws InterruptedException { ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:" + port + "/tracing", String.class); Thread.sleep(200); return "open " + response.getBody(); } }
效果
好博客就要一起分享哦!分享海报