Browse Source

springboot1.x支持多dataId

weihf 5 years ago
parent
commit
6711a9ac93

+ 0 - 92
ipu-rest-scaffold/src/main/java/com/ai/ipu/server/config/NacosListenerConfiguration.java

1
package com.ai.ipu.server.config;
2

3
import static com.alibaba.nacos.api.common.Constants.DEFAULT_GROUP;
4

5
import javax.annotation.PostConstruct;
6

7
import org.slf4j.Logger;
8
import org.slf4j.LoggerFactory;
9
import org.springframework.context.ApplicationListener;
10
import org.springframework.context.annotation.Bean;
11
import org.springframework.context.annotation.Configuration;
12

13
import com.alibaba.nacos.api.annotation.NacosInjected;
14
import com.alibaba.nacos.api.config.ConfigService;
15
import com.alibaba.nacos.api.config.listener.AbstractListener;
16
import com.alibaba.nacos.api.config.listener.Listener;
17
import com.alibaba.nacos.api.exception.NacosException;
18
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
19
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySources;
20
import com.alibaba.nacos.spring.context.event.config.NacosConfigListenerRegisteredEvent;
21
import com.alibaba.nacos.spring.context.event.config.NacosConfigPublishedEvent;
22
import com.alibaba.nacos.spring.context.event.config.NacosConfigReceivedEvent;
23
import com.alibaba.nacos.spring.context.event.config.NacosConfigRemovedEvent;
24

25
@Configuration
26
//@NacosPropertySource(dataId = "mysql", autoRefreshed = true)
27
public class NacosListenerConfiguration {
28
	private static final Logger logger = LoggerFactory.getLogger(NacosListenerConfiguration.class);
29
	@NacosInjected
30
    private ConfigService configService;
31
	
32
	@PostConstruct
33
    public void init() throws NacosException {
34

35
        Listener listener = new AbstractListener() {
36
            @Override
37
            public void receiveConfigInfo(String configInfo) {
38
            }
39
        };
40

41
        //添加事件监听
42
        configService.addListener("mysqlConfig", DEFAULT_GROUP, listener);
43
        
44
        //取消事件监听
45
        //configService.removeListener("com.ai.ipu.nacos", DEFAULT_GROUP, listener);
46
    }
47

48

49
    @Bean
50
    public ApplicationListener<NacosConfigReceivedEvent> nacosConfigReceivedEventListener() {
51
        return new ApplicationListener<NacosConfigReceivedEvent>() {
52
            @Override
53
            public void onApplicationEvent(NacosConfigReceivedEvent event) {
54
                logger.info("Listening on NacosConfigReceivedEvent -  dataId : {} , groupId : {} , " + "content : {} , "
55
                        + "source : {}", event.getDataId(), event.getGroupId(), event.getContent(), event.getSource());
56
            }
57
        };
58
    }
59

60
    @Bean
61
    public ApplicationListener<NacosConfigRemovedEvent> nacosConfigRemovedEventListener() {
62
        return new ApplicationListener<NacosConfigRemovedEvent>() {
63
            @Override
64
            public void onApplicationEvent(NacosConfigRemovedEvent event) {
65
                logger.info("Listening on NacosConfigRemovedEvent -  dataId : {} , groupId : {} , " + "removed : {} , "
66
                        + "source : {}", event.getDataId(), event.getGroupId(), event.isRemoved(), event.getSource());
67
            }
68
        };
69
    }
70

71
    @Bean
72
    public ApplicationListener<NacosConfigListenerRegisteredEvent> nacosConfigListenerRegisteredEventListener() {
73
        return new ApplicationListener<NacosConfigListenerRegisteredEvent>() {
74
            @Override
75
            public void onApplicationEvent(NacosConfigListenerRegisteredEvent event) {
76
                logger.info("Listening on NacosConfigListenerRegisteredEvent -  dataId : {} , groupId : {} , " + "registered : {} , "
77
                        + "source : {}", event.getDataId(), event.getGroupId(), event.isRegistered(), event.getSource());
78
            }
79
        };
80
    }
81

82
    @Bean
83
    public ApplicationListener<NacosConfigPublishedEvent> nacosConfigPublishedEvent() {
84
        return new ApplicationListener<NacosConfigPublishedEvent>() {
85
            @Override
86
            public void onApplicationEvent(NacosConfigPublishedEvent event) {
87
                logger.info("Listening on NacosConfigPublishedEvent -  dataId : {} , groupId : {} , " + "published : {} , "
88
                        + "source : {}", event.getDataId(), event.getGroupId(), event.isPublished(), event.getSource());
89
            }
90
        };
91
    }
92
}

+ 6 - 1
ipu-rest-scaffold/src/main/java/com/ai/ipu/server/control/NacosConfigController.java

13
import com.alibaba.nacos.api.config.ConfigService;
13
import com.alibaba.nacos.api.config.ConfigService;
14
import com.alibaba.nacos.api.config.annotation.NacosValue;
14
import com.alibaba.nacos.api.config.annotation.NacosValue;
15
import com.alibaba.nacos.api.exception.NacosException;
15
import com.alibaba.nacos.api.exception.NacosException;
16
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
17
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySources;
16

18

17
@Controller
19
@Controller
18
@RequestMapping("/nacos/config")
20
@RequestMapping("/nacos/config")
21
@NacosPropertySources({
22
@NacosPropertySource(dataId = "mysql", autoRefreshed = true),
23
@NacosPropertySource(dataId = "myService", autoRefreshed = true)})
19
public class NacosConfigController {
24
public class NacosConfigController {
20
	private static final Logger logger = LoggerFactory.getLogger(NacosConfigController.class);
25
	private static final Logger logger = LoggerFactory.getLogger(NacosConfigController.class);
21
	
26
	
41
	 public boolean setSql(JMap params) throws Exception {
46
	 public boolean setSql(JMap params) throws Exception {
42
		 try {
47
		 try {
43
			    //将select * from display.tab_page_info发布到配置中心,dataId=com.ai.ipu.nacos,变量名=sql
48
			    //将select * from display.tab_page_info发布到配置中心,dataId=com.ai.ipu.nacos,变量名=sql
44
	            configService.publishConfig("mysqlConfig", DEFAULT_GROUP, "sql001 = select * from display.tab_page_info\nuserName = david");
49
	            configService.publishConfig("mysql", DEFAULT_GROUP, "sql001 = select * from display.tab_page_info\nuserName = david");
45
	            return true;
50
	            return true;
46
	        } catch (NacosException e) {
51
	        } catch (NacosException e) {
47
//	            e.printStackTrace();
52
//	            e.printStackTrace();