Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in
Toggle navigation
T
tanpu-community
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
探普后端
tanpu-community
Commits
3893149c
Commit
3893149c
authored
Jun 11, 2021
by
张辰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
加上redis
parent
3556e454
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
314 additions
and
0 deletions
+314
-0
pom.xml
community-service/pom.xml
+16
-0
CommunityApplication.java
...c/main/java/com/tanpu/community/CommunityApplication.java
+2
-0
RedisConfig.java
...src/main/java/com/tanpu/community/config/RedisConfig.java
+99
-0
TestController.java
...n/java/com/tanpu/community/controller/TestController.java
+21
-0
ConJobManager.java
.../main/java/com/tanpu/community/manager/ConJobManager.java
+33
-0
KafkaManager.java
...c/main/java/com/tanpu/community/manager/KafkaManager.java
+29
-0
RedisService.java
...c/main/java/com/tanpu/community/service/RedisService.java
+48
-0
VisitSummaryService.java
...java/com/tanpu/community/service/VisitSummaryService.java
+24
-0
application-dev.yml
community-service/src/main/resources/application-dev.yml
+22
-0
pom.xml
pom.xml
+20
-0
No files found.
community-service/pom.xml
View file @
3893149c
...
...
@@ -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>
...
...
community-service/src/main/java/com/tanpu/community/CommunityApplication.java
View file @
3893149c
...
...
@@ -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
{
...
...
community-service/src/main/java/com/tanpu/community/config/
Cache
Config.java
→
community-service/src/main/java/com/tanpu/community/config/
Redis
Config.java
View file @
3893149c
...
...
@@ -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
.
of
Minutes
(
6
0
)))
RedisCacheConfiguration
.
defaultCacheConfig
().
entryTtl
(
Duration
.
of
Seconds
(
1
0
)))
.
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
,
"_"
);
...
...
community-service/src/main/java/com/tanpu/community/controller/TestController.java
0 → 100644
View file @
3893149c
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"
;
}
}
community-service/src/main/java/com/tanpu/community/manager/ConJobManager.java
0 → 100644
View file @
3893149c
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
);
}
}
community-service/src/main/java/com/tanpu/community/manager/KafkaManager.java
0 → 100644
View file @
3893149c
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
);
}
}
community-service/src/main/java/com/tanpu/community/service/RedisService.java
0 → 100644
View file @
3893149c
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
;
}
}
community-service/src/main/java/com/tanpu/community/service/VisitSummaryService.java
0 → 100644
View file @
3893149c
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
));
}
}
community-service/src/main/resources/application-dev.yml
View file @
3893149c
...
...
@@ -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
...
...
pom.xml
View file @
3893149c
...
...
@@ -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>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment