liujie
2026-01-24 13392606c16362d23478e84e0d43e869813d3095
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.estate.expert.admin.config;
 
import com.estate.expert.common.AppRuntime;
import com.estate.expert.common.log.RequestLogAspect;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.task.TaskExecutorBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
 
import java.util.AbstractMap;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.stream.Collectors;
 
import static org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME;
 
/**
 * 全局配置
 * @author code
 */
@Slf4j
public class GlobalConfig {
    @Value("${spring.application.name}")
    private String appName;
    @Value("${spring.application.id}")
    private String appId;
 
    @Bean
    ScheduledThreadPoolExecutor scheduledThreadPoolExecutor() {
        return new ScheduledThreadPoolExecutor(1);
    }
 
    /**
     * XXX Spring默认配置当有Executor的bean后不再装载TaskExecutor, 这里因为手动注册了
     * {@link #scheduledThreadPoolExecutor}会导致spring不再自动注册TaskExecutor, 因此需要去掉ConditionalOnMissingBean的限制.
     * 参考{@link TaskExecutionAutoConfiguration#applicationTaskExecutor}
     */
    @Bean(name = {APPLICATION_TASK_EXECUTOR_BEAN_NAME,
            AsyncAnnotationBeanPostProcessor.DEFAULT_TASK_EXECUTOR_BEAN_NAME})
    public ThreadPoolTaskExecutor applicationTaskExecutor(TaskExecutorBuilder builder) {
        return builder.build();
    }
 
    @Bean
    public RequestLogAspect requestLogAspect() {
        return new RequestLogAspect();
    }
 
    @Bean
    AppRuntime appRuntime(ApplicationContext applicationContext) {
        return AppRuntime.builder().appId(appId).appName(appName).applicationContext(applicationContext).build();
    }
 
 
    @Data
    @Component
    @EnableConfigurationProperties
    @ConfigurationProperties(prefix = "web.frequency-alert")
    public static class RuleConfig {
        private List<String> rules;
 
        public Map<String, String> getAlertRuleMap() {
            if (rules == null) {
                return Collections.emptyMap();
            }
            return rules.stream()
                    .map(e -> {
                        String[] split = e.split(":");
                        return new AbstractMap.SimpleEntry<>(split[0], split[1]);
                    }).collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));
        }
    }
}