分类
Java 笔记

Spring Boot集成Cache和Redis

构建基于Spring Boot 2.X应用,使用Cache,需要引入:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

以前开发Spring用EhCache来做缓存。在做集群或分布式时,还是Redis比较好用。引入Spring Data Redis如下:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

注意这里会有jedis依赖。

在applicatin.yml配置Redis和Cache:

spring:
  cache:
    redis:
      cache-names:
      cache-null-values: false
      key-prefix: spring:cache
      time-to-live: 0
      use-key-prefix: true
    type: # Cache type. By default, auto-detected according to the environment.
  redis:
    database: 5
    url: # Connection URL. Overrides host, port, and password. User is ignored. Example: redis://user:password@example.com:6379
    host: 127.0.0.1
    port: 6379
    password:
    timeout: 3000
    jedis:
      pool:
        max-active: 200
        max-idle: 10
        max-wait: -1
        min-idle: 0

更多配置属性可参考Spring Boot官方说明:

新建RedisConfig类,增加如下方法:

/**
 * 定义缓存数据 key 生成策略的bean
 * 包名+类名+方法名+所有参数
 * @return
 */
@Bean
public KeyGenerator wiselyKeyGenerator(){
	return new KeyGenerator() {
		@Override
		public Object generate(Object target, Method method, Object... params) {
			StringBuilder buff = new StringBuilder();
			buff.append(target.getClass().getName());
			buff.append(method.getName());
			for (Object obj : params) {
				buff.append(obj.toString());
			}
			return buff.toString();
		}
	};
}
分类
Java

Spring Boot打包分离依赖JAR和配置文件

开发Spring Boot应用,用Spring Boot的Maven插件打包部署,会得到一个很大的JAR文件。该JAR文件包含了所有依赖JAR和配置文件。有时升级只需更新某个模块或修改某项配置,所以需要把它们分离出来。

在pom.xml添加如下插件,配置。

<plugins>
	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-compiler-plugin</artifactId>
		<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>
		<configuration>
			<includes>
				<include>
					<groupId>null</groupId>
					<artifactId>null</artifactId>
				</include>
			</includes>
			<layout>JAR</layout>
			<addResources>true</addResources>
		</configuration>
		<executions>
			<execution>
				<goals>
					<goal>repackage</goal>
				</goals>
				<configuration>
					<!--配置jar包特殊标识 配置后,保留原文件,生成新文件 *-run.jar -->
					<!--配置jar包特殊标识 不配置,原文件命名为 *.jar.original,生成新文件 *.jar -->
					<!--<classifier>run</classifier>-->
				</configuration>
			</execution>
		</executions>
	</plugin>

	<!--打包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>cn.payadd.admin.AdminApplication</mainClass>
				</manifest>
				<manifestEntries>
					<!--MANIFEST.MF 中 Class-Path 加入资源文件目录-->
					<Class-Path>./resources/</Class-Path>
				</manifestEntries>
			</archive>
			<outputDirectory>${project.build.directory}</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}/lib/
					</outputDirectory>
				</configuration>
			</execution>
		</executions>
	</plugin>

	<!--拷贝资源文件 copy-resources-->
	<plugin>
		<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>
						</resource>
					</resources>
					<outputDirectory>${project.build.directory}/resources</outputDirectory>
				</configuration>
			</execution>
		</executions>
	</plugin>
</plugins>

执行maven,编译结果如图:

lib目录是依赖jar,resources是配置文件目录。

参考:
https://my.oschina.net/u/2329318/blog/1933393

分类
Java 笔记

Spring Boog引入ZooKeeper启动报错

在Spring Boot中引入ZooKeeper的jar包,启动时报错,如图:

是slf4j日志包冲出导致,因为项目在spring-boot-starter-logging中已引入。

解决办法,排除ZooKeeper中slf4j的依赖:

<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>${zookeeper.version}</version>
    <exclusions>
        <exclusion>
            <artifactId>slf4j-log4j12</artifactId>
            <groupId>org.slf4j</groupId>
        </exclusion>
    </exclusions>
</dependency>

参考:https://blog.csdn.net/change987654321/article/details/82800279

分类
Java

Spring Boot修改最大上传文件限制

SpringBoot做文件上传时出现了The field file exceeds its maximum permitted size of 1048576 bytes.错误,显示文件的大小超出了允许的范围。查看了官方文档,原来Spring Boot工程嵌入的tomcat限制了请求的文件大小,这一点在Spring Boot的官方文档中有说明,原文如下:

文档说明表示,每个文件的配置最大为1Mb,单次请求的文件的总数不能大于10Mb。要更改这个默认值需要在配置文件(如application.properties)中加入两个配置。

需要设置以下两个参数

multipart.maxFileSize
multipart.maxRequestSize