com.yanyun.log.configuration.LogAutoConfiguration Maven / Gradle / Ivy
package com.yanyun.log.configuration;
import com.yanyun.log.aop.DealAopSendMsg;
import com.yanyun.log.configuration.properties.KubeMqProperties;
import com.yanyun.log.service.MQSendEventService;
import com.yanyun.log.websocket.LogSocketConfig;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 日志组件的自动配置类
*/
@Configuration
@EnableConfigurationProperties({KubeMqProperties.class})
@EnableScheduling
@EnableWebSocketMessageBroker
public class LogAutoConfiguration {
/**
* 远程调用的工具Bean,如果提供了则不会进行提供
*
* @return
*/
@Bean
@ConditionalOnMissingBean
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Value("${gather.ip.url}")
private String ip2RegionAddr;
/**
* 注入mq发送消息的Bean对象
*
* @return
*/
@Bean
@ConditionalOnMissingBean
public MQSendEventService mqSendEventService(KubeMqProperties kubeMqProperties) {
return new MQSendEventService(kubeMqProperties);
}
/**
* 注入拦截实现Bean,该Bean中定义了拦截的策略与采集的数据
*
* @param mqSendEventService
* @return
*/
@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(value = {ApiOperation.class, SecurityContextHolder.class})
public DealAopSendMsg dealAopSendMsg(MQSendEventService mqSendEventService,
@Qualifier("senderThreadPool") ExecutorService senderThreadPool,
RestTemplate restTemplate) {
return new DealAopSendMsg(mqSendEventService, senderThreadPool, restTemplate, ip2RegionAddr);
}
/**
* 自定义线程池,用来处理发送行为数据到MQ
* 使用Cache类型进行复用线程
*
* @return
*/
@Bean
public ExecutorService senderThreadPool() {
ExecutorService sender = new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue(), new NamedThreadFactory("SENDER"),
//丢弃策略为丢弃最老的线程任务
new ThreadPoolExecutor.DiscardOldestPolicy());
return sender;
}
/**
* 线程池监控
*/
@Bean
public LogSocketConfig logSocketConfig(@Qualifier("senderThreadPool") ExecutorService senderThreadPool) {
return new LogSocketConfig(senderThreadPool);
}
/**
* 自定义具名线程Factory
*/
private static class NamedThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
NamedThreadFactory(String customPrefix) {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = customPrefix + "-thread-";
}
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
}