目錄結構:
整合redis(先把redis啓動起來)
找到對應目錄輸入命令
redis-server redis.windows.conf
controller層
package com.searchmarket.config;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.io.Serializable;
/**
* @program: SearchMarket
* @description: 11
* @Author: 小白白
* @create: 2020/02/17 - 16:35
**/
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfig {
@Bean
public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Serializable> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
RedisConfig
package com.searchmarket.config;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.io.Serializable;
/**
* @program: SearchMarket
* @description: 11
* @Author: 小白白
* @create: 2020/02/17 - 16:35
**/
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfig {
@Bean
public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Serializable> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
redis.yml
#redis
redis:
#redis機器ip
hostname: 127.0.0.1
#redis端口
port: 6379
#redis密碼
password:
#redis超時時間(毫秒),如果不設置,取默認值2000
timeout: 10000
#最大空閒數
maxIdle: 300
#連接池的最大數據庫連接數。設爲0表示無限制,如果是jedis 2.4以後用redis.maxTotal
#maxActive=600
#控制一個pool可分配多少個jedis實例,用來替換上面的redis.maxActive,如果是jedis 2.4以後用該屬性
maxTotal: 1000
#最大建立連接等待時間。如果超過此時間將接到異常。設爲-1表示無限制。
maxWaitMillis: 1000
#連接的最小空閒時間 默認1800000毫秒(30分鐘)
minEvictableIdleTimeMillis: 300000
#每次釋放連接的最大數目,默認3
numTestsPerEvictionRun: 1024
#逐出掃描的時間間隔(毫秒) 如果爲負數,則不運行逐出線程, 默認-1
timeBetweenEvictionRunsMillis: 30000
#是否在從池中取出連接前進行檢驗,如果檢驗失敗,則從池中去除連接並嘗試取出另一個
testOnBorrow: true
#在空閒時檢查有效性, 默認false
testWhileIdle: true
#redis集羣配置
#spring.cluster.nodes=192.168.1.1:7001,192.168.1.1:7002,192.168.1.1:7003,192.168.1.1:7004,192.168.1.1:7005,192.168.1.1:7006
#spring.cluster.max-redirects=3
#哨兵模式
#sentinel.host1=192.168.1.1
#sentinel.port1=26379
#sentinel.host2=192.168.1.2
#sentinel.port2=26379
導入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
整合log4j
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--日誌級別以及優先級排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!-- status log4j2內部日誌級別 -->
<configuration status="INFO">
<!-- 全局參數 -->
<Properties>
<Property name="pattern">%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %c{1}:%L -%m%n</Property>
<Property name="displayName">EurekaServer</Property>
</Properties>
<Appenders>
<Console name="console" target="SYSTEM_OUT" follow="true">
<PatternLayout>
<pattern>${pattern}</pattern>
</PatternLayout>
</Console>
<!-- 文件 每次運行程序會自動清空,由append屬性決定 -->
<File name="error" fileName="${displayName}_error.log" append="false">
<!-- 指定error 級別的日誌 -->
<ThresholdFilter level="ERROR" onMatch="ACCEPT"
onMismatch="DENY" />
<PatternLayout>
<pattern>${pattern}</pattern>
</PatternLayout>
</File>
<!-- 滾動文件 -->
<RollingFile name="rollingFile" fileName="${displayName}.log"
filePattern="${displayName}_%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>${pattern}</pattern>
</PatternLayout>
<!-- 按大小劃分 -->
<SizeBasedTriggeringPolicy size="50 MB" />
</RollingFile>
</Appenders>
<Loggers>
<!-- <Logger name="org.apache.catalina.util.LifecycleBase" level="ERROR"
/> <Logger name="org.apache.coyote.http11.Http11NioProtocol" level="WARN"
/> <Logger name="org.apache.tomcat.util.net.NioSelectorPool" level="WARN"
/> -->
<Logger name="org.springframework" level="WARN" />
<Logger name="com.searchmarket" level="DEBUG" />
<Root level="DEBUG">
<AppenderRef ref="console"></AppenderRef>
<AppenderRef ref="error"></AppenderRef>
<AppenderRef ref="rollingFile"></AppenderRef>
</Root>
</Loggers>
</configuration>
依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>