程序员蜗牛
1 year ago
8 changed files with 219 additions and 0 deletions
@ -0,0 +1,95 @@ |
|||||
|
<?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> |
||||
|
<parent> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-parent</artifactId> |
||||
|
<version>2.7.12</version> |
||||
|
<relativePath/> <!-- lookup parent from repository --> |
||||
|
</parent> |
||||
|
<groupId>com.itheima</groupId> |
||||
|
<artifactId>jvm-optimize</artifactId> |
||||
|
<version>0.0.1-SNAPSHOT</version> |
||||
|
<name>jvm-optimize</name> |
||||
|
<description>jvm-optimize</description> |
||||
|
<properties> |
||||
|
<java.version>8</java.version> |
||||
|
</properties> |
||||
|
<dependencies> |
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-web</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.projectlombok</groupId> |
||||
|
<artifactId>lombok</artifactId> |
||||
|
<optional>true</optional> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-test</artifactId> |
||||
|
<scope>test</scope> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>org.apache.commons</groupId> |
||||
|
<artifactId>commons-lang3</artifactId> |
||||
|
<version>3.12.0</version> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-actuator</artifactId> |
||||
|
|
||||
|
<exclusions><!-- 去掉springboot默认配置 --> |
||||
|
<exclusion> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-logging</artifactId> |
||||
|
</exclusion> |
||||
|
</exclusions> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.openjdk.jol</groupId> |
||||
|
<artifactId>jol-core</artifactId> |
||||
|
<version>0.9</version> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.springframework.data</groupId> |
||||
|
<artifactId>spring-data-commons</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>cn.hutool</groupId> |
||||
|
<artifactId>hutool-all</artifactId> |
||||
|
<version>5.8.16</version> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>com.github.ben-manes.caffeine</groupId> |
||||
|
<artifactId>caffeine</artifactId> |
||||
|
<version>2.9.3</version> |
||||
|
</dependency> |
||||
|
|
||||
|
</dependencies> |
||||
|
|
||||
|
<build> |
||||
|
<plugins> |
||||
|
<plugin> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-maven-plugin</artifactId> |
||||
|
<configuration> |
||||
|
<excludes> |
||||
|
<exclude> |
||||
|
<groupId>org.projectlombok</groupId> |
||||
|
<artifactId>lombok</artifactId> |
||||
|
</exclude> |
||||
|
</excludes> |
||||
|
</configuration> |
||||
|
</plugin> |
||||
|
|
||||
|
</plugins> |
||||
|
</build> |
||||
|
|
||||
|
</project> |
@ -0,0 +1,13 @@ |
|||||
|
package com.woniu.jvmoptimize; |
||||
|
|
||||
|
import org.springframework.boot.SpringApplication; |
||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication; |
||||
|
|
||||
|
@SpringBootApplication |
||||
|
public class JvmOptimizeApplication { |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
SpringApplication.run(JvmOptimizeApplication.class, args); |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,17 @@ |
|||||
|
package com.woniu.jvmoptimize.config; |
||||
|
|
||||
|
import org.springframework.context.annotation.Configuration; |
||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
||||
|
|
||||
|
@Configuration |
||||
|
public class LoginConfig implements WebMvcConfigurer { |
||||
|
|
||||
|
@Override |
||||
|
public void addInterceptors(InterceptorRegistry registry) { |
||||
|
//注册拦截器
|
||||
|
registry.addInterceptor(new UserInterceptor()) |
||||
|
.addPathPatterns("/dealMemory/**"); |
||||
|
|
||||
|
} |
||||
|
} |
@ -0,0 +1,32 @@ |
|||||
|
package com.woniu.jvmoptimize.config; |
||||
|
|
||||
|
import com.woniu.jvmoptimize.entity.UserData; |
||||
|
import org.springframework.web.servlet.HandlerInterceptor; |
||||
|
import org.springframework.web.servlet.ModelAndView; |
||||
|
|
||||
|
import javax.servlet.http.HttpServletRequest; |
||||
|
import javax.servlet.http.HttpServletResponse; |
||||
|
|
||||
|
/** |
||||
|
* 拦截器的实现,模拟放入数据到threadlocal中 |
||||
|
*/ |
||||
|
public class UserInterceptor implements HandlerInterceptor { |
||||
|
|
||||
|
public static ThreadLocal<UserData> userData = new ThreadLocal<>(); |
||||
|
|
||||
|
@Override |
||||
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { |
||||
|
userData.set(new UserData()); |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { |
||||
|
// userData.remove();
|
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { |
||||
|
userData.remove(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,26 @@ |
|||||
|
package com.woniu.jvmoptimize.controller; |
||||
|
|
||||
|
import org.springframework.http.ResponseEntity; |
||||
|
import org.springframework.web.bind.annotation.GetMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
|
||||
|
/** |
||||
|
* -Xmx1g -Xms1g |
||||
|
* 系统不处理业务的时候 也占用大量的内存 该如何排查并解决? |
||||
|
*/ |
||||
|
@RestController |
||||
|
@RequestMapping("/dealMemory") |
||||
|
public class DemoMemoryController { |
||||
|
|
||||
|
@GetMapping |
||||
|
public ResponseEntity test() { |
||||
|
error(); |
||||
|
return ResponseEntity.ok().build(); |
||||
|
} |
||||
|
|
||||
|
private void error() { |
||||
|
throw new RuntimeException("出错了"); |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,5 @@ |
|||||
|
package com.woniu.jvmoptimize.entity; |
||||
|
|
||||
|
public class UserData { |
||||
|
byte[] data = new byte[1024 * 1024 * 10]; //模拟保存10m的用户数据
|
||||
|
} |
@ -0,0 +1,18 @@ |
|||||
|
|
||||
|
#设置应用名 |
||||
|
spring: |
||||
|
application: |
||||
|
name: jvm-service |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
server: |
||||
|
port: 8881 |
||||
|
tomcat: |
||||
|
threads: |
||||
|
min-spare: 100 |
||||
|
max: 500 |
||||
|
|
||||
|
|
||||
|
|
@ -0,0 +1,13 @@ |
|||||
|
package com.woniu.jvmoptimize; |
||||
|
|
||||
|
import org.junit.jupiter.api.Test; |
||||
|
import org.springframework.boot.test.context.SpringBootTest; |
||||
|
|
||||
|
@SpringBootTest |
||||
|
class JvmOptimizeApplicationTests { |
||||
|
|
||||
|
@Test |
||||
|
void contextLoads() { |
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue