Commit 048836e6 authored by ag's avatar ag

1. 修复IDGenerator 的DataCenterId 默认不存在无法启动的问题

2. 默认增加MQ自动停止.
parent 1f42d4a5
......@@ -2,12 +2,22 @@ package cn.quantgroup.tech.shutdown;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.config.RabbitListenerConfigUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.Lifecycle;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.PreDestroy;
/**
* 这里是一些基本的停止工具. to be continue
*
* @author ag
*/
@Slf4j
@Component
public class BaseDestroyHandler {
......@@ -15,14 +25,14 @@ public class BaseDestroyHandler {
@Autowired
private ApplicationContext applicationContext;
//todo test
// @PreDestroy
@PreDestroy
@ConditionalOnBean(RedisTemplate.class)
private void stopRedisSub() {
log.info("redis stopped");
log.info("我什么都没做, redis stopped");
}
//todo test
// @PreDestroy
@PreDestroy
@ConditionalOnBean(RabbitTemplate.class)
private void stopRabbitMQ() {
try {
RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry = applicationContext.getBean(
......@@ -30,6 +40,7 @@ public class BaseDestroyHandler {
RabbitListenerEndpointRegistry.class);
rabbitListenerEndpointRegistry.getListenerContainers().forEach(Lifecycle::stop);
} catch (Exception e) {
log.error("貌似停止 MQ 遇到了问题...");
}
log.info("MQ listener stopped");
}
......
......@@ -13,7 +13,6 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
@Setter
@ConfigurationProperties(prefix = "shutdown.graceful")
public class GracefulShutdownProperties {
/**
* The timeout before force shutdown. TimeUnit.Second
*/
......
......@@ -7,6 +7,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
......@@ -33,6 +34,7 @@ import java.util.concurrent.locks.ReentrantLock;
@Slf4j
@Component
@ConditionalOnBean(RedisTemplate.class)
@ConditionalOnProperty(name = "data.center.id")
public class IDGenerator {
private static final String REDIS_WORK_ID_KEY = "GLOBAL:WORK:ID:";
......@@ -88,13 +90,12 @@ public class IDGenerator {
private StringRedisTemplate redis;
@Value("${data.center.id}")
public void setDataCenterId(int dataCenterId) {
public void setDataCenterId(Integer dataCenterId) {
DATA_CENTER_ID = dataCenterId;
}
@PostConstruct
public void init() {
log.info("test Init id generator");
int workerId = (int) (redis.opsForValue().increment(REDIS_WORK_ID_KEY + DATA_CENTER_ID, 1) % MAX_WORK_ID);
WORKER_ID_STR = String.format("%04d", workerId);
DATA_CENTER_STR = String.format("%03d", DATA_CENTER_ID);
......
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