Browse Source

@IPU_REQ_2022@添加nacos使用示例,添加nacos配置发生变化后,定时任务让新配置在第二天0点0分1秒生效的示例。

weihf 3 years ago
parent
commit
3bb5a88004

+ 69 - 3
ipu-rest-demo/pom.xml

72
				<type>pom</type>
72
				<type>pom</type>
73
				<scope>import</scope>
73
				<scope>import</scope>
74
			</dependency>
74
			</dependency>
75
			<dependency>
76
				<groupId>org.springframework.cloud</groupId>
77
				<artifactId>spring-cloud-dependencies</artifactId>
78
				<!-- <version>Greenwich.RELEASE</version>  -->
79
				<version>Hoxton.SR8</version>
80
				<type>pom</type>
81
				<scope>import</scope>
82
			</dependency>
75
83
84
			<dependency>
85
				<groupId>com.alibaba.cloud</groupId>
86
				<artifactId>spring-cloud-alibaba-dependencies</artifactId>
87
				<version>2.2.1.RELEASE</version>
88
				<type>pom</type>
89
				<scope>import</scope>
90
			</dependency>
76
		</dependencies>
91
		</dependencies>
77
	</dependencyManagement>
92
	</dependencyManagement>
78
93
96
			<groupId>com.ai.ipu.server</groupId>
111
			<groupId>com.ai.ipu.server</groupId>
97
			<artifactId>ipu-restful</artifactId>
112
			<artifactId>ipu-restful</artifactId>
98
        </dependency>
113
        </dependency>
99
        
114
    	<dependency>
115
            <groupId>org.springframework.boot</groupId>
116
            <artifactId>spring-boot-starter-log4j2</artifactId>
117
            <exclusions>
118
				<exclusion>
119
					<groupId>org.apache.logging.log4j</groupId>
120
					<artifactId>log4j-core</artifactId>
121
				</exclusion>
122
				<exclusion>
123
					<groupId>org.apache.logging.log4j</groupId>
124
					<artifactId>log4j-api</artifactId>
125
				</exclusion>
126
				<exclusion>
127
					<groupId>org.apache.logging.log4j</groupId>
128
					<artifactId>log4j-slf4j-impl</artifactId>
129
				</exclusion>
130
			</exclusions>
131
        </dependency>
132
    	<dependency>
133
			<groupId>org.apache.logging.log4j</groupId>
134
			<artifactId>log4j-slf4j-impl</artifactId>
135
		</dependency>
136
		<!--log4j2核心包-->
137
		<dependency>
138
			<groupId>org.apache.logging.log4j</groupId>
139
			<artifactId>log4j-api</artifactId>
140
		</dependency>
141
		<dependency>
142
			<groupId>org.apache.logging.log4j</groupId>
143
			<artifactId>log4j-core</artifactId>
144
		</dependency>        
100
        <!-- 使用ipu-nosql、ipu-cache 3.2-SNAPSHOT是因为配合ipu-spring-nacos自动同步配置所需 -->
145
        <!-- 使用ipu-nosql、ipu-cache 3.2-SNAPSHOT是因为配合ipu-spring-nacos自动同步配置所需 -->
101
		<dependency>
146
		<dependency>
102
            <groupId>com.ai.ipu</groupId>
147
            <groupId>com.ai.ipu</groupId>
108
		</dependency>
153
		</dependency>
109
154
110
		<dependency>
155
		<dependency>
156
			<groupId>com.ai.ipu</groupId>
157
			<artifactId>ipu-spring-nacos</artifactId>
158
		</dependency>
159
		<dependency>
111
		    <groupId>com.google.guava</groupId>
160
		    <groupId>com.google.guava</groupId>
112
		    <artifactId>guava</artifactId>
161
		    <artifactId>guava</artifactId>
113
		</dependency>
162
		</dependency>
114
163
115
    	<!--  -->
164
		<!-- nacos -->
165
		<dependency>
166
			<groupId>com.alibaba.cloud</groupId>
167
			<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
168
		</dependency>
169
		<dependency>
170
			<groupId>com.alibaba.cloud</groupId>
171
			<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
172
		</dependency>
173
		<!-- nacos定时任务 -->
174
		<dependency>  
175
        	<groupId>org.quartz-scheduler</groupId>  
176
        	<artifactId>quartz</artifactId>   
177
    	</dependency>  
178
    	<dependency>  
179
        	<groupId>org.quartz-scheduler</groupId>  
180
        	<artifactId>quartz-jobs</artifactId>  
181
    	</dependency>
116
    	<dependency>
182
    	<dependency>
117
			<groupId>junit</groupId>
183
			<groupId>junit</groupId>
118
			<artifactId>junit</artifactId>
184
			<artifactId>junit</artifactId>
119
			<scope>test</scope>
185
			<scope>test</scope>
120
		</dependency>	
186
		</dependency>
121
	</dependencies>
187
	</dependencies>
122
188
123
	<build>
189
	<build>

+ 1 - 1
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/IpuRestDemoStart.java

13
 * @desc 启动的时候需要指定参数--server.port=8080
13
 * @desc 启动的时候需要指定参数--server.port=8080
14
 */
14
 */
15
public class IpuRestDemoStart {
15
public class IpuRestDemoStart {
16
    public final static String DUBBO_CONSUMER_CONFIG = "dubbo-consumer-nacos.xml";
16
//    public final static String DUBBO_CONSUMER_CONFIG = "dubbo-consumer-nacos.xml";
17
    public final static String EXCEPTION_MESSAGES_CONFIG = "exception_messages";
17
    public final static String EXCEPTION_MESSAGES_CONFIG = "exception_messages";
18
    
18
    
19
    public static void main(String[] args) {
19
    public static void main(String[] args) {

+ 63 - 0
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/config/IpuSpringNacosConfig.java

1
package com.ai.ipu.server.demo.config;
2
3
4
import org.springframework.beans.factory.annotation.Value;
5
import org.springframework.context.annotation.Bean;
6
import org.springframework.context.annotation.PropertySource;
7
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
8
import org.springframework.stereotype.Component;
9
10
import lombok.Data;
11
12
@PropertySource(value = { "classpath:ipu-spring-nacos.properties" })
13
@Component
14
@Data
15
public class IpuSpringNacosConfig {
16
17
//	@Value("#{'${list}'.split(',')}")
18
//	private List<String> ipuMiddlewareList;
19
20
	@Value("${num}")
21
	private long ipuMiddlewareNum;
22
	
23
	@Value("${ipu.cache.dataid}")
24
	private String ipuCacheDataid;
25
	
26
	@Value("${ipu.nosql.dataid}")
27
	private String ipuNosqlDataid;
28
29
	@Value("${ipu.database.dataid}")
30
	private String ipuDatabaseDataid;
31
	
32
	@Value("${ipu.dfs.dataid}")
33
	private String ipuDfsDataid;
34
35
	@Value("${ipu.cache.group}")
36
	private String ipuCacheGroup;
37
	
38
	@Value("${ipu.nosql.group}")
39
	private String ipuNosqlGroup;
40
	
41
	@Value("${ipu.database.group}")
42
	private String ipuDatabaseGroup;
43
	
44
	@Value("${ipu.dfs.group}")
45
	private String ipuDfsGroup;
46
	
47
	@Value("${ipu.cache.timeoutms}")
48
	private long ipuCacheTimeoutms;
49
50
	@Value("${ipu.nosql.timeoutms}")
51
	private long ipuNosqlTimeoutms;
52
53
	@Value("${ipu.database.timeoutms}")
54
	private long ipuDatabaseTimeoutms;
55
56
	@Value("${ipu.dfs.timeoutms}")
57
	private long ipuDfsTimeoutms;	
58
59
	@Bean
60
	public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
61
		return new PropertySourcesPlaceholderConfigurer();
62
	}
63
}

+ 106 - 0
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/config/NacosConfig.java

1
package com.ai.ipu.server.demo.config;
2
3
4
import java.util.Properties;
5
import java.util.concurrent.Executor;
6
7
import javax.annotation.PostConstruct;
8
9
import org.springframework.beans.factory.annotation.Value;
10
import org.springframework.context.annotation.Bean;
11
import org.springframework.stereotype.Component;
12
13
import com.ai.ipu.basic.string.StringUtil;
14
import com.alibaba.nacos.api.NacosFactory;
15
import com.alibaba.nacos.api.PropertyKeyConst;
16
import com.alibaba.nacos.api.config.ConfigService;
17
import com.alibaba.nacos.api.config.listener.Listener;
18
import com.alibaba.nacos.api.exception.NacosException;
19
20
21
import lombok.extern.slf4j.Slf4j;
22
23
//springcloud也支持在程序中手工注册到配置中心,此时就可以监听到配置中心推送的配置文件。
24
//springcloud已经不再支持@NacosConfigListener,配置了也不生效。
25
@Component
26
@Slf4j
27
public class NacosConfig {
28
	private String service002Url;
29
	private String service002Param;
30
	
31
	@Bean(name="getService002Url")
32
	public String getService002Url() {
33
		return service002Url;
34
	}
35
	
36
	@Bean(name="getService002Param")
37
	public String getService002Param() {
38
		return service002Param;
39
	}
40
	
41
	
42
	@Value("${spring.cloud.nacos.config.server-addr}")
43
    private String nacosConfigServer;
44
	@Value("${spring.cloud.nacos.namespace}")
45
    private String nacosNamespace;
46
	
47
	@Value("${spring.cloud.nacos.dataid}")
48
    private String dataId;
49
	@Value("${spring.cloud.nacos.group}")
50
    private String group;
51
	@Value("${spring.cloud.nacos.timeout}")
52
    private long timeout;
53
	
54
	@PostConstruct
55
	public void initConfigServer() {
56
		try {
57
	        Properties properties = new Properties();
58
	        if (!StringUtil.isEmpty(nacosNamespace))
59
	        	properties.put(PropertyKeyConst.NAMESPACE, nacosNamespace);
60
	        properties.put(PropertyKeyConst.SERVER_ADDR, nacosConfigServer);
61
	        ConfigService configService = NacosFactory.createConfigService(properties);
62
	        String content = configService.getConfig(dataId, group, timeout);
63
	        //从配置中心获取到指定配置,需要自己解析后给service002Url、service002Param赋值
64
	        log.debug(content);
65
	        dealConfigServerConfig(content);
66
	        configService.addListener(dataId, group, new Listener() {
67
	            @Override
68
	            public void receiveConfigInfo(String configInfo) {
69
	            	//从配置中心获取到推送来的更新配置信息,需要自己解析给service002Url、service002Param赋值
70
	            	log.debug(configInfo);
71
	            	dealConfigServerConfig(configInfo);
72
	            }
73
	
74
	            @Override
75
	            public Executor getExecutor() {
76
	                return null;
77
	            }
78
	        });
79
	    } catch (NacosException e) {
80
	        log.error(e.getMessage());
81
	    }
82
	}
83
	
84
	private void dealConfigServerConfig(String configInfo) {
85
		if (StringUtil.isEmpty(configInfo))
86
			return;
87
		String[] configs = configInfo.split("\n\r|\n|\r|\r\n");
88
		int i=0;
89
		for (;i<configs.length;i++) {
90
			if (!StringUtil.isEmpty(configs[i]))
91
			{
92
				if (configs[i].indexOf("service002Url")>=0)
93
				{
94
					String[] urls = configs[i].split("=");
95
					service002Url = urls[1];
96
				}
97
				if (configs[i].indexOf("service002Param")>=0)
98
				{
99
					String[] params = configs[i].split("=");
100
					service002Param = params[1];
101
				}
102
			}
103
		}
104
	}
105
	
106
}

+ 364 - 0
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/control/nacos/IpuSpringNacosController.java

1
package com.ai.ipu.server.demo.control.nacos;
2
3
import java.util.HashMap;
4
import java.util.Iterator;
5
import java.util.List;
6
import java.util.Map;
7
import java.util.Map.Entry;
8
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.stereotype.Controller;
11
import org.springframework.web.bind.annotation.RequestMapping;
12
import org.springframework.web.bind.annotation.ResponseBody;
13
14
import com.ai.ipu.basic.log.ILogger;
15
import com.ai.ipu.basic.log.IpuLoggerFactory;
16
import com.ai.ipu.cache.CacheFactory;
17
import com.ai.ipu.cache.ICache;
18
import com.ai.ipu.cache.config.IpuCacheConfig;
19
import com.ai.ipu.common.xml.Dom4jHelper;
20
import com.ai.ipu.data.JMap;
21
import com.ai.ipu.data.impl.JsonMap;
22
import com.ai.ipu.database.dao.IpuDaoManager;
23
import com.ai.ipu.database.dao.impl.SqlDao;
24
import com.ai.ipu.database.datasource.DataSourceEntity;
25
import com.ai.ipu.database.uitl.MybatisUtil;
26
import com.ai.ipu.dfs.s3.IFs;
27
import com.ai.ipu.dfs.s3.IpuDfsFactory;
28
import com.ai.ipu.dfs.s3.conf.IpuDfsConfig;
29
import com.ai.ipu.dfs.s3.entity.BucketEntity;
30
import com.ai.ipu.dfs.s3.entity.DfsEntity;
31
import com.ai.ipu.nacos.config.IpuNacosConfig;
32
import com.ai.ipu.nacos.service.IpuNacosServiceImpl;
33
import com.ai.ipu.nosql.INoSql;
34
import com.ai.ipu.nosql.config.IpuNoSQLConfig;
35
import com.ai.ipu.nosql.mongodb.MongoCacheFactory;
36
import com.ai.ipu.server.demo.config.IpuSpringNacosConfig;
37
import com.ai.ipu.server.demo.listener.IpuCacheListener;
38
import com.github.pagehelper.util.StringUtil;
39
/**
40
 * 基于ipu-spring-nacos组件对ipu-cache/ipu-nacos/ipu-s3/ipu-database动态刷新配置范例。
41
 * 需要在启动程序时,通过jvm参数设置nacos配置中心ip和端口,如果不配置,缺省为127.0.0.1:8848。
42
 * jvm格式:-Dipu.nacos.server-addr=47.105.160.21:8810
43
 * @author weihf@asiainfo.com
44
 * @team IPU
45
 * @date 2021年3月16日
46
 */
47
@Controller
48
@RequestMapping("/nacos/")
49
public class IpuSpringNacosController {
50
	private static final ILogger LOGGER = IpuLoggerFactory.createLogger(IpuSpringNacosController.class);
51
	
52
	@Autowired
53
	private IpuNacosServiceImpl service;
54
	@Autowired
55
	private IpuSpringNacosConfig nacosConfig;
56
	@Autowired
57
	private IpuNacosConfig config;
58
	@ResponseBody
59
    @RequestMapping("/syncIpuConfigUsingIpuCache")
60
    public JMap syncIpuConfigUsingIpuCache(JMap param) throws Exception {
61
		JMap result = new JsonMap();
62
		final String CACHE_KEY = "com:ai:ipu:rest-scaffold:test";
63
		final String DEFAULT_CONN_NAME = "ssn";
64
		String connName = param.getString("connName", DEFAULT_CONN_NAME);
65
		ICache cache = null;
66
		try {
67
			//根据本地的ipu-cache.xml配置操作redis
68
			cache = CacheFactory.getCache(connName);
69
			cache.put(CACHE_KEY, "123");
70
			LOGGER.debug(CACHE_KEY + " is " + cache.get(CACHE_KEY));
71
			
72
			//由于下面要连到其他redis,因此需要将连接关闭/释放
73
			cache.close();
74
			
75
			//从nacos配置中心获取ipu-cache.xml的配置,并加载到ipu-cache的系统配置中
76
			boolean status = service.pullFromServerAndSyncIpuConfig(nacosConfig.getIpuCacheDataid(), nacosConfig.getIpuCacheGroup(), nacosConfig.getIpuCacheTimeoutms(), IpuCacheConfig.getInstance());
77
78
			if (status)
79
			{
80
				//此时虽然还是ssn,但是已经连到配置中心里的redis服务器了
81
				cache = CacheFactory.getCache(connName);
82
				LOGGER.debug(CACHE_KEY + " is " + cache.get(CACHE_KEY));
83
				result.put("result", "从nacos server:" + config.getConfigServerAddr() + "同步" + nacosConfig.getIpuCacheDataid() + "成功");
84
			}
85
			else
86
				result.put("result", "从nacos server:" + config.getConfigServerAddr() + "同步" + nacosConfig.getIpuCacheDataid() + "失败");
87
	
88
		} catch (Exception e)
89
		{
90
			String errorMsg = "从nacos server:" + config.getConfigServerAddr() + "同步" + nacosConfig.getIpuCacheDataid() + "错误:" + e.toString();
91
			LOGGER.error(errorMsg);
92
			result.put("result", errorMsg);
93
		}finally {
94
			cache.close();
95
		}
96
		return result;
97
	}
98
	
99
	@ResponseBody
100
    @RequestMapping("/syncIpuConfigUsingIpuNosql")
101
    public JMap syncIpuConfigUsingIpuNosql(JMap param) throws Exception {
102
		JMap result = new JsonMap();
103
		final String DEFAULT_CONN_NAME = "data";
104
		final String DATABASE_NAME = "test";
105
		final String TABLE_NAME = "mycol1";
106
		String connName = param.getString("connName", DEFAULT_CONN_NAME);
107
		INoSql noSql = null;
108
		try {
109
			//根据本地的ipu-nosql.xml配置操作mongodb
110
			noSql = MongoCacheFactory.getMongoDao(connName, DATABASE_NAME, TABLE_NAME);
111
			noSql.executeInsert(
112
					"insert into mycol1 (name, nickName, remarks, age) values ('test','this is a test','1',20)");
113
			System.out.println("mongodb result: "
114
					+ noSql.executeSelect("select * from mycol1 where name <> 'mike' order by age asc"));
115
			
116
			noSql.commitTransaction();
117
			
118
			//从nacos配置中心获取ipu-nosql.xml的配置,并加载到ipu-nosql的系统配置中
119
			boolean status = service.pullFromServerAndSyncIpuConfig(nacosConfig.getIpuNosqlDataid(), nacosConfig.getIpuNosqlGroup(), nacosConfig.getIpuNosqlTimeoutms(), IpuNoSQLConfig.getInstance());
120
121
			if (status)
122
			{
123
				//此时虽然还是data,但是已经连到配置中心里的mongodb服务器了
124
				noSql = MongoCacheFactory.getMongoDao(connName, DATABASE_NAME, TABLE_NAME);
125
				//ipu-nosql支持3种mongodb操作:mongodb原生操作、标准sql操作、表对象操作
126
				//上面示例是标准sql操作,下面是表对象操作示例
127
				//表对象操作的条件目前仅支持等于。
128
				Map<String, Object> insertData = new HashMap<String, Object>();
129
				insertData.put("name", "test");
130
				insertData.put("nickName", "test");
131
				insertData.put("remarks", "2");
132
				insertData.put("age", 18);
133
				noSql.insert(insertData);
134
				noSql.commitTransaction();
135
				
136
				//查询刚插入的数据
137
				Map<String, Object> selectParam = new HashMap<String, Object>();
138
				selectParam.put("name", "test");
139
				
140
				LOGGER.debug("result is: " + noSql.selectByCond(selectParam));
141
				result.put("result", "从nacos server:" + config.getConfigServerAddr() + "同步" + nacosConfig.getIpuNosqlDataid() + "成功");
142
			}
143
			else
144
				result.put("result", "从nacos server:" + config.getConfigServerAddr() + "同步" + nacosConfig.getIpuNosqlDataid() + "失败");
145
	
146
		} catch (Exception e)
147
		{
148
			if (noSql != null)
149
				noSql.abortTransaction();
150
			String errorMsg = "从nacos server:" + config.getConfigServerAddr() + "同步" + nacosConfig.getIpuNosqlDataid() + "错误:" + e.toString();
151
			LOGGER.error(errorMsg);
152
			result.put("result", errorMsg);
153
		}finally {
154
			
155
		}
156
		return result;
157
	}
158
	
159
	@ResponseBody
160
    @RequestMapping("/syncIpuConfigUsingIpuS3")
161
    public JMap syncIpuConfigUsingIpuS3(JMap param) throws Exception {
162
		JMap result = new JsonMap();
163
		final String DFS_NAME = "default";
164
		final String DEFAULT_BUCKET_NAME = "test";
165
		String bucketName = param.getString("bucketName", DEFAULT_BUCKET_NAME);
166
		
167
		DfsEntity dfsEntity;
168
		IFs client;
169
		try {
170
			//根据本地的ipu-nosql.xml配置操作mongodb
171
			dfsEntity = IpuDfsFactory.getDfsEntity(DFS_NAME);
172
			BucketEntity bucket = new BucketEntity();
173
			bucket.setDfsName(dfsEntity.getDfsName());
174
			bucket.setBucketName(bucketName);
175
			bucket.setRegionName(dfsEntity.getRegionName());
176
			client = IpuDfsFactory.getFsClient(dfsEntity.getDfsName());
177
			
178
			//创建目录,用户需要在minio里有创建目录的权限
179
			client.createBucket(bucket);
180
			
181
			bucket = new BucketEntity();
182
			bucket.setDfsName(dfsEntity.getDfsName());
183
			bucket.setRegionName(dfsEntity.getRegionName());
184
			LOGGER.debug("bucket list: "
185
					+ client.getAllBuckets(bucket));
186
					
187
			//从nacos配置中心获取ipu-s3.xml的配置,并加载到ipu-s3的系统配置中
188
			boolean status = service.pullFromServerAndSyncIpuConfig(nacosConfig.getIpuDfsDataid(), nacosConfig.getIpuDatabaseGroup(), nacosConfig.getIpuDfsTimeoutms(), IpuDfsConfig.getInstance());
189
190
			if (status)
191
			{
192
				//注意,由于系统配置发生了变化,虽然下面的代码和上面一致,但是实际内容不同。
193
				//请不要注释下面的代码,并直接使用上面已经定义好的对象!
194
				dfsEntity = IpuDfsFactory.getDfsEntity(DFS_NAME);
195
				
196
				client = IpuDfsFactory.getFsClient(dfsEntity.getDfsName());
197
				
198
				bucket = new BucketEntity();
199
				bucket.setDfsName(dfsEntity.getDfsName());
200
				bucket.setRegionName(dfsEntity.getRegionName());
201
				LOGGER.debug("bucket list: "
202
						+ client.getAllBuckets(bucket));
203
				
204
				bucket = new BucketEntity();
205
				bucket.setDfsName(dfsEntity.getDfsName());
206
				bucket.setBucketName(bucketName);
207
				bucket.setRegionName(dfsEntity.getRegionName());
208
				
209
				//创建目录,用户需要在minio里有创建目录的权限
210
				client.createBucket(bucket);
211
				
212
				bucket = new BucketEntity();
213
				bucket.setDfsName(dfsEntity.getDfsName());
214
				bucket.setRegionName(dfsEntity.getRegionName());
215
				LOGGER.debug("bucket list: "
216
						+ client.getAllBuckets(bucket));
217
				
218
				result.put("result", "从nacos server:" + config.getConfigServerAddr() + "同步" + nacosConfig.getIpuDfsDataid() + "成功");
219
			}
220
			else
221
				result.put("result", "从nacos server:" + config.getConfigServerAddr() + "同步" + nacosConfig.getIpuDfsDataid() + "失败");
222
	
223
		} catch (Exception e)
224
		{
225
			String errorMsg = "从nacos server:" + config.getConfigServerAddr() + "同步" + nacosConfig.getIpuDfsDataid() + "错误:" + e.toString();
226
			LOGGER.error(errorMsg);
227
			result.put("result", errorMsg);
228
		}finally {
229
			
230
		}
231
		return result;
232
	}
233
	
234
	@ResponseBody
235
    @RequestMapping("/syncIpuConfigUsingIpuDatabase")
236
    public JMap syncIpuConfigUsingIpuDatabase(JMap param) throws Exception {
237
		JMap result = new JsonMap();
238
		final String CONN_NAME = "test";
239
		final String DEFAULT_TABLE_NAME = "ipu_db_demo";
240
		
241
		String tableName = param.getString("tableName", DEFAULT_TABLE_NAME);
242
		
243
		try {
244
			SqlDao dao = IpuDaoManager.takeDao(SqlDao.class, CONN_NAME);
245
			String sql = "select * from " + tableName;
246
			List<Map<String, Object>> dbResult = dao.executeSelect(sql);
247
			LOGGER.debug("查询结果:" + dbResult);
248
					
249
			//从nacos配置中心获取ipu-database.xml的配置
250
			String ipuDatabaseXml = service.pullFromServer(nacosConfig.getIpuDatabaseDataid(), nacosConfig.getIpuDatabaseGroup(), nacosConfig.getIpuDatabaseTimeoutms());
251
252
			//加载到ipu-database的系统配置中
253
			if (!StringUtil.isEmpty(ipuDatabaseXml))
254
			{
255
				Map map = ipuDatabaseXmlToMap(ipuDatabaseXml, "utf-8");	
256
				LOGGER.debug("map:" + map);
257
				Iterator it = map.entrySet().iterator();
258
				while (it.hasNext()) {
259
					Entry entry = (Entry) it.next();
260
					Map<String, Object> attrs = (Map<String, Object>) entry.getValue();
261
					Map<String, String> properties = (Map<String, String>) attrs.get("property");
262
					DataSourceEntity dataSource = new DataSourceEntity();
263
					dataSource.setName((String) entry.getKey());
264
					dataSource.setUrl(properties.get("jdbcUrl"));
265
					dataSource.setUsername(properties.get("username"));
266
					dataSource.setPassword(properties.get("password"));
267
					MybatisUtil.registerDataSource(dataSource.getName(), dataSource);
268
269
				}
270
				//手工增加版本,需要修改ipu-database代码,才能实现自动为connName加版本,此时这里只需要输入test即可。
271
				dao = IpuDaoManager.takeDao(SqlDao.class, CONN_NAME + "1.0");
272
				sql = "select * from " + tableName;
273
				dbResult = dao.executeSelect(sql);
274
				LOGGER.debug("查询结果:" + dbResult);	
275
				
276
				result.put("result", "从nacos server:" + config.getConfigServerAddr() + "同步" + nacosConfig.getIpuDatabaseDataid() + "成功");
277
			}
278
			else
279
				result.put("result", "从nacos server:" + config.getConfigServerAddr() + "同步" + nacosConfig.getIpuDatabaseDataid() + "失败");
280
	
281
		} catch (Exception e)
282
		{
283
			String errorMsg = "从nacos server:" + config.getConfigServerAddr() + "同步" + nacosConfig.getIpuDatabaseDataid() + "错误:" + e.toString();
284
			LOGGER.error(errorMsg);
285
			result.put("result", errorMsg);
286
		}finally {
287
			
288
		}
289
		return result;
290
	}
291
	
292
    /**
293
     * @description 将ipu-database的xml字符串转换成map
294
     * @param xml
295
     * @return Map
296
     * @throws Exception 
297
     */
298
    private Map ipuDatabaseXmlToMap(String xml, String charsetName) throws Exception {
299
        Dom4jHelper dh = new Dom4jHelper(xml, charsetName);
300
        Map<String, ?> originData = dh.getAll();
301
        List<Map<String, Object>> configs = (List<Map<String, Object>>) originData.get("environments");
302
        Map dbsMap = new HashMap<String, Object>();
303
        Map<String, Object> dbMap;
304
        Map<String, String> configAttr;
305
        /*循环缓存实例*/
306
        for (Map<String, Object> config : configs) {
307
            dbMap = new HashMap<String, Object>();
308
            configAttr = new HashMap<String, String>();
309
            
310
            Map<String, String> dbAttrMap = (Map<String, String>) config.get("environment" + Dom4jHelper.SUFFIX_ATTR);
311
            String datdbaseName = dbAttrMap.get("id");
312
            List<Map<String, Object>> dbList = (List<Map<String, Object>>)config.get("environment");
313
            String type = "";
314
            String driver = "";
315
            for(Map<String, Object> map : dbList){
316
            	if(map.get("transactionManager_attr")!=null){
317
                    /*解析缓存实例的服务列表*/
318
                    Map<String, Object> serverMap = (Map<String, Object>)map.get("transactionManager_attr");
319
                    type = (String)serverMap.get("type");
320
                }
321
            	if(map.get("dataSource_attr")!=null){
322
                    /*解析缓存实例的服务列表*/
323
                    Map<String, Object> serverMap = (Map<String, Object>)map.get("dataSource_attr");
324
                    driver = (String)serverMap.get("type");
325
                }
326
            	if(map.get("dataSource")!=null){
327
                    /*解析缓存实例的服务列表*/
328
            		List<Map<String, Object>> serverList = (List<Map<String, Object>>)map.get("dataSource");
329
//                    List<Map<String, Object>> serverList = (List<Map<String, Object>>)serverMap.get("property_attr");
330
                    for(Map<String, Object> configValue : serverList){
331
                    	Map<String, Object> configMap = (Map<String, Object>)configValue.get("property_attr");
332
                    	configAttr.put((String)configMap.get("name"), (String)configMap.get("value"));
333
                    }
334
                                        
335
                }
336
                
337
            }            
338
            dbMap.put("databaseName", datdbaseName+"1.0");
339
            dbMap.put("type", type);
340
            dbMap.put("property", configAttr);
341
            dbMap.put("dataSource_driver", driver);
342
            dbsMap.put(datdbaseName+"1.0", dbMap);
343
        }
344
        return dbsMap;
345
    }
346
    
347
	@ResponseBody
348
    @RequestMapping("/scheduleIpuConfigUsingIpuCache")
349
    public JMap scheduleIpuConfigUsingIpuCache(JMap param) throws Exception {
350
		JMap result = new JsonMap();	
351
		try {		
352
			//监听配置中心里ipu-cache.xml的配置
353
			service.addListener(nacosConfig.getIpuCacheDataid(), nacosConfig.getIpuCacheGroup(), new IpuCacheListener());
354
355
			result.put("result", "监听配置中心:" + config.getConfigServerAddr() + "的配置" + nacosConfig.getIpuCacheDataid() + "成功");		
356
		} catch (Exception e)
357
		{
358
			String errorMsg = "监听配置中心:" + config.getConfigServerAddr() + "i的配置" + nacosConfig.getIpuCacheDataid() + "错误:" + e.toString();
359
			LOGGER.error(errorMsg);
360
			result.put("result", errorMsg);
361
		}
362
		return result;
363
	}
364
}

+ 41 - 0
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/listener/IpuCacheListener.java

1
package com.ai.ipu.server.demo.listener;
2
3
4
import com.ai.ipu.basic.log.ILogger;
5
import com.ai.ipu.basic.log.IpuLoggerFactory;
6
import com.ai.ipu.common.date.TimeUtil;
7
import com.ai.ipu.restful.spring.SpringBeanUtil;
8
import com.ai.ipu.server.demo.config.IpuSpringNacosConfig;
9
import com.ai.ipu.server.demo.schedule.ScheduledTask;
10
import com.alibaba.nacos.api.config.listener.AbstractListener;
11
12
public class IpuCacheListener extends AbstractListener {
13
	private static final ILogger log = IpuLoggerFactory.createLogger(IpuCacheListener.class);
14
	
15
	@Override
16
	public void receiveConfigInfo(String configInfo) {
17
		
18
		try {
19
			log.debug(">>>>>>>>>>>>>>>>>>>new config from nacos:" + configInfo);
20
			IpuSpringNacosConfig config = SpringBeanUtil.getBean(IpuSpringNacosConfig.class);
21
			ScheduledTask scheduledTask = SpringBeanUtil.getBean(ScheduledTask.class);
22
			//新配置第二天0点0分1秒生效
23
			StringBuffer cron = new StringBuffer();
24
			cron.append("1").append(" 0").append(" 0 ");
25
			String day = TimeUtil.getSysDate(1, "dd");
26
			String month = TimeUtil.getSysDate(1, "MM");
27
			cron.append(day.startsWith("0")?day.substring(1):day).append(" ");
28
			cron.append(month.startsWith("0")?month.substring(1):month).append(" ");
29
			cron.append("? ");
30
			cron.append(TimeUtil.getSysDate(1, "yyyy"));
31
//			scheduledTask.addCronJob("0 07 15 6 5 ? 2022", config.getIpuCacheDataid(), configInfo);
32
			log.debug("cron:" + cron.toString());
33
			scheduledTask.addCronJob(cron.toString(), config.getIpuCacheDataid(), configInfo);
34
35
		} catch (Exception e) {
36
			log.error("receiveConfigInfo error:", e);
37
		}
38
	}
39
40
}
41

+ 44 - 0
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/schedule/ScheduleIpuCacheJob.java

1
package com.ai.ipu.server.demo.schedule;
2
3
import org.quartz.Job;
4
import org.quartz.JobExecutionContext;
5
import org.quartz.JobExecutionException;
6
7
import java.util.Map;
8
import com.ai.ipu.basic.log.ILogger;
9
import com.ai.ipu.basic.log.IpuLoggerFactory;
10
import com.ai.ipu.restful.spring.SpringBeanUtil;
11
import com.ai.ipu.server.demo.config.IpuSpringNacosConfig;
12
import com.ai.ipu.server.demo.util.XmlUtil;
13
14
public class ScheduleIpuCacheJob implements Job {
15
	private static final ILogger logger = IpuLoggerFactory.createLogger(ScheduleIpuCacheJob.class);
16
17
	public void execute(JobExecutionContext context) throws JobExecutionException {
18
		try {
19
			Map xml = context.getMergedJobDataMap();
20
			new Thread(new Runnable() {
21
				@Override
22
				public void run() {
23
					try {
24
						IpuSpringNacosConfig config = SpringBeanUtil.getBean(IpuSpringNacosConfig.class);
25
						Map map = XmlUtil.ipuCacheXmlToMap((String) xml.get(config.getIpuCacheDataid()), "utf-8");
26
						logger.debug("map:" + map);
27
						logger.debug("为ipu-cache添加配置成功");
28
					} catch (Exception e) {
29
						failed(e);
30
					}
31
				}
32
			}).start();
33
34
		} catch (Exception e) {
35
			logger.error("为ipu-cache添加配置异常:" + e.toString(), e);
36
		}
37
38
	}
39
40
	private void failed(Exception e) {
41
		logger.error("error " + e.toString(), e);
42
	}
43
44
}

+ 77 - 0
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/schedule/ScheduledTask.java

1
package com.ai.ipu.server.demo.schedule;
2
3
import java.util.Map;
4
import java.util.concurrent.ConcurrentHashMap;
5
6
import org.quartz.CronScheduleBuilder;  
7
import org.quartz.JobBuilder;
8
import org.quartz.JobDataMap;
9
import org.quartz.JobDetail;  
10
import org.quartz.Scheduler;
11
import org.quartz.SchedulerException;  
12
import org.quartz.Trigger;  
13
import org.quartz.TriggerBuilder;  
14
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
16
import org.springframework.stereotype.Component;
17
18
import com.ai.ipu.basic.log.ILogger;
19
import com.ai.ipu.basic.log.IpuLoggerFactory;
20
21
@Component
22
public class ScheduledTask{  
23
	private static final ILogger logger = IpuLoggerFactory.createLogger(ScheduledTask.class);
24
	private Map<String, Scheduler> task = new ConcurrentHashMap<String, Scheduler>();
25
	
26
	@Autowired
27
	SchedulerFactoryBean schedulerFactoryBean;
28
	
29
	private void clearCronJob(Scheduler sched) throws SchedulerException {
30
		sched.shutdown();
31
		sched.clear();
32
	}
33
	public void addCronJob(String cron, String dataId,String xml) 
34
    {	  
35
        Scheduler sched;
36
		try {
37
			if (task.containsKey(dataId))
38
			{
39
				sched = task.get(dataId);
40
				clearCronJob(sched);
41
			}
42
			else
43
			{	
44
				synchronized (this) { 
45
					if (task.containsKey(dataId))
46
					{
47
						sched = task.get(dataId);
48
						clearCronJob(sched);
49
					}
50
					else
51
					{
52
						sched = schedulerFactoryBean.getScheduler();
53
						task.put(dataId, sched);
54
					}
55
				}
56
			}
57
			// Define job  
58
			JobDataMap newJobDataMap = new JobDataMap();
59
			newJobDataMap.put(dataId, xml);
60
	        JobDetail job = JobBuilder.newJob(ScheduleIpuCacheJob.class)  
61
	                .withIdentity("update ipu-cache configaration", "group1")
62
	                .setJobData(newJobDataMap)
63
	                .build();  
64
	  
65
	        // Define trigger strategy  
66
	        Trigger trigger = TriggerBuilder.newTrigger()  
67
	                .withIdentity("trigger1", "group1")  
68
	                .withSchedule(CronScheduleBuilder.cronSchedule(cron))  
69
	                .build();  
70
	        sched.scheduleJob(job, trigger); 
71
	        
72
	        sched.start();
73
		} catch (SchedulerException e) {
74
			logger.error(e.toString(), e);
75
		}          
76
    }
77
}  

+ 1 - 0
ipu-rest-demo/src/main/resources/dev/application.properties

52

52

53
#spring-data-es配置
53
#spring-data-es配置
54
spring.elasticsearch.rest.uris=http://47.105.160.21:10020
54
spring.elasticsearch.rest.uris=http://47.105.160.21:10020
55
nacos.config.server-addr=47.105.160.21:8849