Commit 3893149c authored by 张辰's avatar 张辰

加上redis

parent 3556e454
......@@ -64,6 +64,22 @@
<artifactId>fastjson</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.kafka/spring-kafka -->
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
......
......@@ -4,11 +4,13 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement
@EnableCaching
@EnableScheduling
@ComponentScan({"com.tanpu.common", "com.tanpu.community"})
public class CommunityApplication {
......
......@@ -2,62 +2,95 @@ package com.tanpu.community.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisPoolConfig;
import java.lang.reflect.Method;
import java.time.Duration;
@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport {
public class RedisConfig {
// @Value("${spring.redis.host}")
// private String host;
// @Value("${spring.redis.port}")
// private int port;
// @Value("${spring.redis.password}")
// private String password;
// @Value("${spring.redis.timeout}")
// private int timeout;
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.max-active}")
private int maxActive;
@Value("${spring.redis.max-wait}")
private long maxWaitMillis;
@Value("${spring.redis.max-idle}")
private int maxIdle;
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
// maximum connection
jedisPoolConfig.setMaxTotal(maxActive);
// Maximum wait time when no connection is available in the pool
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
// Maximum number of idle connections
jedisPoolConfig.setMinIdle(maxIdle);
// Other properties can be added by yourself
return jedisPoolConfig;
}
// see https://www.baeldung.com/spring-boot-redis-cache
@Bean
public RedisCacheConfiguration cacheConfiguration() {
return RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofDays(30))
.disableCachingNullValues()
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
JedisClientConfiguration jedisClientConfiguration = JedisClientConfiguration.builder().usePooling()
.poolConfig(jedisPoolConfig).and().readTimeout(Duration.ofMillis(timeout)).build();
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(host);
redisStandaloneConfiguration.setPort(port);
redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory(jedisPoolConfig()));
return template;
}
/**
* cache
*/
@Bean
public RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer() {
return (builder) -> builder
return (builder) -> RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory(jedisPoolConfig()))
.withCacheConfiguration("tempCache",
RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(60)))
RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(10)))
.withCacheConfiguration("longCache",
RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(365)));
RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(7)));
}
@Bean("communityKeyGenerator")
public KeyGenerator keyGenerator() {
return new CommunityKeyGenerator();
return new RedisConfig.CommunityKeyGenerator();
}
public static class CommunityKeyGenerator implements KeyGenerator {
public Object generate(Object target, Method method, Object... params) {
// todo prefix 加到common
return "new_community_" + target.getClass().getSimpleName() + "_"
+ method.getName() + "_"
+ StringUtils.arrayToDelimitedString(params, "_");
......
package com.tanpu.community.controller;
import com.tanpu.community.manager.KafkaManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@Slf4j
@RequestMapping(value = "/api/test")
public class TestController {
@Autowired
private KafkaManager kafkaManager;
@PostMapping(value = "/sendKafka")
@ResponseBody
public String sendKafka(@RequestBody String message) {
return "success";
}
}
package com.tanpu.community.manager;
import com.tanpu.community.service.RedisService;
import com.tanpu.community.service.VisitSummaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Slf4j
@Service
@Configuration
public class ConJobManager {
@Autowired
private VisitSummaryService visitSummaryService;
@Autowired
private RedisService redisService;
/**
* 定时统计 话题 访问数据,并刷到redis
*/
@Scheduled(cron = "*/10 * * * * ?")
public void topicVisitorStats() {
String topicId = "123";
Integer detailVisitTimes = visitSummaryService.queryTopicDetailVisit(topicId);
redisService.set("topicVisitorStats", detailVisitTimes);
}
}
package com.tanpu.community.manager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class KafkaManager {
@Autowired
private KafkaTemplate<String, String> kafkaTemplate;
// public void sendMessage(String message) {
// System.out.println("#### send " + message);
// this.kafkaTemplate.send("users", message);
// }
// todo topic
@KafkaListener(topics = "newCommunityVisitor")
public void consumeVisitorHis(String message) {
System.out.println("#### receive " + message);
}
}
package com.tanpu.community.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Slf4j
@Service
public class RedisService {
// todo prefix
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public boolean existsKey(String key) {
return redisTemplate.hasKey(key);
}
/**
* set
*/
public void set(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
public void set(String key, Object value, long time, TimeUnit timeUnit) {
redisTemplate.opsForValue().set(key, value, time, timeUnit);
}
/**
* get
*/
public String getString(String key) {
Object v = redisTemplate.opsForValue().get(key);
return v == null ? null : (String) v;
}
public Integer getInteger(String key) {
Object v = redisTemplate.opsForValue().get(key);
return v == null ? null : (Integer) v;
}
}
package com.tanpu.community.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanpu.community.dao.entity.community.VisitSummaryEntity;
import com.tanpu.community.dao.mapper.community.VisitSummaryMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Slf4j
@Service
public class VisitSummaryService {
@Resource
private VisitSummaryMapper visitSummaryMapper;
// 查询话题 详细页面 浏览量
public Integer queryTopicDetailVisit(String topicId) {
return visitSummaryMapper.selectCount(new LambdaQueryWrapper<VisitSummaryEntity>()
.eq(VisitSummaryEntity::getRefId, topicId));
}
}
......@@ -29,12 +29,34 @@ spring.redis:
port: 56379
password: qimeng123
timeout: 2000
max-active: 5
max-wait: 5
max-idle: 5
jedis:
pool:
max-active: 3
max-idle: 3
min-idle: 3
spring.kafka:
bootstrap-servers: 118.190.63.109:9092
consumer:
group-id: tp_group_new_community
auto-offset-reset: latest
fetch-min-size: 64
fetch-max-wait: 500
max-poll-records: 500
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
producer:
acks: 1
batch-size: 10000
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
spring:
sleuth:
enabled: false
......
......@@ -80,6 +80,26 @@
<version>1.2.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.kafka/spring-kafka -->
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.6.7</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment