Przeglądaj źródła

@IPU_FIXBUG_2021@master分支只保留springboot1.X实例

weihf 3 lat temu
rodzic
commit
a1bed29dc4

+ 10 - 50
ipu-rest-demo/pom.xml

@ -4,7 +4,7 @@
4 4
	<parent>
5 5
		<groupId>com.ai.ipu.server</groupId>
6 6
		<artifactId>ipu-server-parent</artifactId>
7
		<version>3.3-SNAPSHOT</version>
7
		<version>3.2-SNAPSHOT</version>
8 8
	</parent>
9 9
10 10
	<artifactId>ipu-rest-demo</artifactId>
@ -35,8 +35,8 @@
35 35
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
36 36
		<ipu>3.2-SNAPSHOT</ipu>
37 37
		<!-- ipu-dependencies3.3-SNAPSHOT起,不再支持springboot1.x,升级为springboot2.1.3-RELEASE -->
38
		<ipu-rest>3.3-SNAPSHOT</ipu-rest>
39
		<org.springframework.boot>2.3.1.RELEASE</org.springframework.boot>
38
		<ipu-rest>3.2-SNAPSHOT</ipu-rest>
39
		<org.springframework.boot>1.5.9.RELEASE</org.springframework.boot>
40 40
		<!-- 执行完build镜像后,是否自动push到仓库,如果是,必须配置docker-registry-server -->
41 41
		<spring-boot-docker-push>false</spring-boot-docker-push>
42 42
        <!-- 拉取镜像的策略,可选的值:ALWAYS, NEVER, IF_NOT_PRESENT -->
@ -54,7 +54,6 @@
54 54
55 55
		<msgframe>1.9.2</msgframe>
56 56
		<jms>1.1</jms>
57
		<google.plugin>19.0</google.plugin>
58 57
	</properties>
59 58
60 59
	<dependencyManagement>
@ -73,33 +72,11 @@
73 72
				<type>pom</type>
74 73
				<scope>import</scope>
75 74
			</dependency>
76
			<dependency>
77
				<groupId>org.springframework.cloud</groupId>
78
				<artifactId>spring-cloud-dependencies</artifactId>
79
				<!-- <version>Greenwich.RELEASE</version>  -->
80
				<version>Hoxton.SR8</version>
81
				<type>pom</type>
82
				<scope>import</scope>
83
			</dependency>
84 75
85
			<dependency>
86
				<groupId>com.alibaba.cloud</groupId>
87
				<artifactId>spring-cloud-alibaba-dependencies</artifactId>
88
				<version>2.2.1.RELEASE</version>
89
				<type>pom</type>
90
				<scope>import</scope>
91
			</dependency>
92 76
		</dependencies>
93 77
	</dependencyManagement>
94 78
95 79
	<dependencies>
96
		<!-- dubbo相关依赖 -->
97
		<dependency>
98
			<groupId>com.ai.ipu.test</groupId>
99
			<artifactId>ipu-dubbo-api</artifactId>
100
			<version>0.0.1-SNAPSHOT</version>
101
		</dependency>
102
103 80
		<!-- msgframe相关依赖 -->
104 81
		<dependency>
105 82
			<groupId>com.ai.aif.msgframe</groupId>
@ -118,7 +95,6 @@
118 95
        <dependency>
119 96
			<groupId>com.ai.ipu.server</groupId>
120 97
			<artifactId>ipu-restful</artifactId>
121
			<version>3.3-SNAPSHOT</version>
122 98
        </dependency>
123 99
        
124 100
        <!-- 使用ipu-nosql、ipu-cache 3.2-SNAPSHOT是因为配合ipu-spring-nacos自动同步配置所需 -->
@ -130,34 +106,18 @@
130 106
		    <groupId>com.ai.ipu</groupId>
131 107
		    <artifactId>ipu-s3</artifactId>
132 108
		</dependency>
133
		<dependency>
134
			<groupId>com.ai.ipu</groupId>
135
			<artifactId>ipu-spring-nacos</artifactId>
136
		</dependency>
109
137 110
		<dependency>
138 111
		    <groupId>com.google.guava</groupId>
139 112
		    <artifactId>guava</artifactId>
140
		    <version>${google.plugin}</version>
141 113
		</dependency>
142 114
143
		<!-- nacos -->
144
		<dependency>
145
			<groupId>com.alibaba.cloud</groupId>
146
			<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
147
		</dependency>
148
		<dependency>
149
			<groupId>com.alibaba.cloud</groupId>
150
			<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
151
		</dependency>
152
		<!-- nacos定时任务 -->
153
		<dependency>  
154
        	<groupId>org.quartz-scheduler</groupId>  
155
        	<artifactId>quartz</artifactId>   
156
    	</dependency>  
157
    	<dependency>  
158
        	<groupId>org.quartz-scheduler</groupId>  
159
        	<artifactId>quartz-jobs</artifactId>  
160
    	</dependency>	
115
    	<!--  -->
116
    	<dependency>
117
			<groupId>junit</groupId>
118
			<artifactId>junit</artifactId>
119
			<scope>test</scope>
120
		</dependency>	
161 121
	</dependencies>
162 122
163 123
	<build>

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

@ -1,63 +0,0 @@
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
}

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

@ -1,106 +0,0 @@
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
}

+ 0 - 17
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/config/RestConsumerConfiguration.java

@ -1,17 +0,0 @@
1
package com.ai.ipu.server.demo.config;
2
3
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
4
import org.springframework.context.annotation.Bean;
5
import org.springframework.context.annotation.Configuration;
6
import org.springframework.web.client.RestTemplate;
7
8
9
@Configuration
10
public class RestConsumerConfiguration {
11
12
	@LoadBalanced
13
    @Bean
14
    public RestTemplate ipuRestTemplate() {
15
        return new RestTemplate();
16
    }
17
}

+ 0 - 86
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/control/dubbo/DubboConsumerController.java

@ -1,86 +0,0 @@
1
package com.ai.ipu.server.demo.control.dubbo;
2

3

4
import org.springframework.stereotype.Controller;
5
import org.springframework.web.bind.annotation.RequestMapping;
6
import org.springframework.web.bind.annotation.ResponseBody;
7

8
import com.ai.ipu.basic.log.ILogger;
9
import com.ai.ipu.basic.log.IpuLoggerFactory;
10
import com.ai.ipu.basic.reflect.ReflectUtil;
11
import com.ai.ipu.basic.util.IpuUtility;
12
import com.ai.ipu.restful.spring.SpringManager;
13
import com.ai.ipu.test.springboot.service.IBizService;
14
import com.ai.ipu.test.springboot.service.IpuService;
15
import com.ailk.common.data.IData;
16
import com.alibaba.dubbo.config.annotation.Reference;
17

18
/**
19
 * @author huangbo@asiainfo.com
20
 * @team IPU
21
 * @date 2018年10月24日上午00:13:37
22
 * @desc Dubbo客户端的使用范例
23
 */
24
@Controller
25
@RequestMapping("/dubbo")
26
public class DubboConsumerController {
27
	private static final transient ILogger logger = IpuLoggerFactory.createLogger(DubboConsumerController.class);
28
	
29
	@Reference
30
	IBizService bizService;
31
	
32
	@Reference
33
    IpuService ipuService;
34
    
35
	@ResponseBody
36
    @RequestMapping("/index")
37
    public String index(String data) {
38
//		IpuService ipuService = (IpuService)SpringManager.takeSpringContext().getBean("ipuService"); // 获取远程服务代理
39
        logger.info("I am ConsumerController" + System.getProperty("user.home"));
40
        if(data==null||"".equals(data)){
41
            data="你啥都没有输入!";
42
        }
43

44
        logger.info("你输入的是"+data);
45

46
        ipuService.genaratePlumelog(data);
47
        logger.info("调用dubbo结束...........");
48
        return data;
49
    }
50
    /**
51
     * @author huangbo@asiainfo.com
52
     * @title: callBizService
53
     * @desc: Dubbo客户端调用服务范例
54
     */
55
    @ResponseBody
56
    @RequestMapping("/biz")
57
    public IData callBizService(IData params) {
58
//        IBizService bizService = (IBizService)SpringManager.takeSpringContext().getBean("bizService"); // 获取远程服务代理
59
    	IData result = bizService.queryUserInfo(params); // 执行远程方法
60
        result.put("rest_result", "测试biz类型服务调用");
61
        return result;
62
    }
63
    
64
    /**
65
     * @author huangbo@asiainfo.com
66
     * @title: callCommonService
67
     * @desc: Dubbo客户端调用通用服务范例
68
     */
69
    @ResponseBody
70
    @RequestMapping("/common")
71
    public IData callCommonService(IData params) {
72
//    	IBizService bizService = (IBizService)SpringManager.takeSpringContext().getBean("bizService"); // 获取远程服务代理
73
//        String serviceName = params.getString("service_name");
74
        String methodName = params.getString("method_name");
75
//        Object service = SpringManager.takeSpringContext().getBean(serviceName);
76
        IData result = null;
77
        try {
78
            result =(IData) ReflectUtil.invokeMethod(bizService, methodName, new Object[]{params}, new Class<?>[]{IData.class});
79
            result.put("rest_result", "测试common类型服务调用");
80
        }catch (Exception e) {
81
            // TODO Auto-generated catch block
82
            IpuUtility.error( methodName + "方法调用异常:" + e.toString());
83
        }
84
        return result;
85
    }
86
}

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

@ -1,363 +0,0 @@
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
	
57
	@ResponseBody
58
    @RequestMapping("/syncIpuConfigUsingIpuCache")
59
    public JMap syncIpuConfigUsingIpuCache(JMap param) throws Exception {
60
		JMap result = new JsonMap();
61
		final String CACHE_KEY = "com:ai:ipu:rest-scaffold:test";
62
		final String DEFAULT_CONN_NAME = "ssn";
63
		String connName = param.getString("connName", DEFAULT_CONN_NAME);
64
		ICache cache = null;
65
		try {
66
			//根据本地的ipu-cache.xml配置操作redis
67
			cache = CacheFactory.getCache(connName);
68
			cache.put(CACHE_KEY, "123");
69
			LOGGER.debug(CACHE_KEY + " is " + cache.get(CACHE_KEY));
70
			
71
			//由于下面要连到其他redis,因此需要将连接关闭/释放
72
			cache.close();
73
			
74
			//从nacos配置中心获取ipu-cache.xml的配置,并加载到ipu-cache的系统配置中
75
			boolean status = service.pullFromServerAndSyncIpuConfig(nacosConfig.getIpuCacheDataid(), nacosConfig.getIpuCacheGroup(), nacosConfig.getIpuCacheTimeoutms(), IpuCacheConfig.getInstance());
76

77
			if (status)
78
			{
79
				//此时虽然还是ssn,但是已经连到配置中心里的redis服务器了
80
				cache = CacheFactory.getCache(connName);
81
				LOGGER.debug(CACHE_KEY + " is " + cache.get(CACHE_KEY));
82
				result.put("result", "从nacos server:" + IpuNacosConfig.getNacosServerAddr() + "同步" + nacosConfig.getIpuCacheDataid() + "成功");
83
			}
84
			else
85
				result.put("result", "从nacos server:" + IpuNacosConfig.getNacosServerAddr() + "同步" + nacosConfig.getIpuCacheDataid() + "失败");
86
	
87
		} catch (Exception e)
88
		{
89
			String errorMsg = "从nacos server:" + IpuNacosConfig.getNacosServerAddr() + "同步" + nacosConfig.getIpuCacheDataid() + "错误:" + e.toString();
90
			LOGGER.error(errorMsg);
91
			result.put("result", errorMsg);
92
		}finally {
93
			cache.close();
94
		}
95
		return result;
96
	}
97
	
98
	@ResponseBody
99
    @RequestMapping("/syncIpuConfigUsingIpuNosql")
100
    public JMap syncIpuConfigUsingIpuNosql(JMap param) throws Exception {
101
		JMap result = new JsonMap();
102
		final String DEFAULT_CONN_NAME = "data";
103
		final String DATABASE_NAME = "test";
104
		final String TABLE_NAME = "mycol1";
105
		String connName = param.getString("connName", DEFAULT_CONN_NAME);
106
		INoSql noSql = null;
107
		try {
108
			//根据本地的ipu-nosql.xml配置操作mongodb
109
			noSql = MongoCacheFactory.getMongoDao(connName, DATABASE_NAME, TABLE_NAME);
110
			noSql.executeInsert(
111
					"insert into mycol1 (name, nickName, remarks, age) values ('test','this is a test','1',20)");
112
			System.out.println("mongodb result: "
113
					+ noSql.executeSelect("select * from mycol1 where name <> 'mike' order by age asc"));
114
			
115
			noSql.commitTransaction();
116
			
117
			//从nacos配置中心获取ipu-nosql.xml的配置,并加载到ipu-nosql的系统配置中
118
			boolean status = service.pullFromServerAndSyncIpuConfig(nacosConfig.getIpuNosqlDataid(), nacosConfig.getIpuNosqlGroup(), nacosConfig.getIpuNosqlTimeoutms(), IpuNoSQLConfig.getInstance());
119

120
			if (status)
121
			{
122
				//此时虽然还是data,但是已经连到配置中心里的mongodb服务器了
123
				noSql = MongoCacheFactory.getMongoDao(connName, DATABASE_NAME, TABLE_NAME);
124
				//ipu-nosql支持3种mongodb操作:mongodb原生操作、标准sql操作、表对象操作
125
				//上面示例是标准sql操作,下面是表对象操作示例
126
				//表对象操作的条件目前仅支持等于。
127
				Map<String, Object> insertData = new HashMap<String, Object>();
128
				insertData.put("name", "test");
129
				insertData.put("nickName", "test");
130
				insertData.put("remarks", "2");
131
				insertData.put("age", 18);
132
				noSql.insert(insertData);
133
				noSql.commitTransaction();
134
				
135
				//查询刚插入的数据
136
				Map<String, Object> selectParam = new HashMap<String, Object>();
137
				selectParam.put("name", "test");
138
				
139
				LOGGER.debug("result is: " + noSql.selectByCond(selectParam));
140
				result.put("result", "从nacos server:" + IpuNacosConfig.getNacosServerAddr() + "同步" + nacosConfig.getIpuNosqlDataid() + "成功");
141
			}
142
			else
143
				result.put("result", "从nacos server:" + IpuNacosConfig.getNacosServerAddr() + "同步" + nacosConfig.getIpuNosqlDataid() + "失败");
144
	
145
		} catch (Exception e)
146
		{
147
			if (noSql != null)
148
				noSql.abortTransaction();
149
			String errorMsg = "从nacos server:" + IpuNacosConfig.getNacosServerAddr() + "同步" + nacosConfig.getIpuNosqlDataid() + "错误:" + e.toString();
150
			LOGGER.error(errorMsg);
151
			result.put("result", errorMsg);
152
		}finally {
153
			
154
		}
155
		return result;
156
	}
157
	
158
	@ResponseBody
159
    @RequestMapping("/syncIpuConfigUsingIpuS3")
160
    public JMap syncIpuConfigUsingIpuS3(JMap param) throws Exception {
161
		JMap result = new JsonMap();
162
		final String DFS_NAME = "default";
163
		final String DEFAULT_BUCKET_NAME = "test";
164
		String bucketName = param.getString("bucketName", DEFAULT_BUCKET_NAME);
165
		
166
		DfsEntity dfsEntity;
167
		IFs client;
168
		try {
169
			//根据本地的ipu-nosql.xml配置操作mongodb
170
			dfsEntity = IpuDfsFactory.getDfsEntity(DFS_NAME);
171
			BucketEntity bucket = new BucketEntity();
172
			bucket.setDfsName(dfsEntity.getDfsName());
173
			bucket.setBucketName(bucketName);
174
			bucket.setRegionName(dfsEntity.getRegionName());
175
			client = IpuDfsFactory.getFsClient(dfsEntity.getDfsName());
176
			
177
			//创建目录,用户需要在minio里有创建目录的权限
178
			client.createBucket(bucket);
179
			
180
			bucket = new BucketEntity();
181
			bucket.setDfsName(dfsEntity.getDfsName());
182
			bucket.setRegionName(dfsEntity.getRegionName());
183
			LOGGER.debug("bucket list: "
184
					+ client.getAllBuckets(bucket));
185
					
186
			//从nacos配置中心获取ipu-s3.xml的配置,并加载到ipu-s3的系统配置中
187
			boolean status = service.pullFromServerAndSyncIpuConfig(nacosConfig.getIpuDfsDataid(), nacosConfig.getIpuDatabaseGroup(), nacosConfig.getIpuDfsTimeoutms(), IpuDfsConfig.getInstance());
188

189
			if (status)
190
			{
191
				//注意,由于系统配置发生了变化,虽然下面的代码和上面一致,但是实际内容不同。
192
				//请不要注释下面的代码,并直接使用上面已经定义好的对象!
193
				dfsEntity = IpuDfsFactory.getDfsEntity(DFS_NAME);
194
				
195
				client = IpuDfsFactory.getFsClient(dfsEntity.getDfsName());
196
				
197
				bucket = new BucketEntity();
198
				bucket.setDfsName(dfsEntity.getDfsName());
199
				bucket.setRegionName(dfsEntity.getRegionName());
200
				LOGGER.debug("bucket list: "
201
						+ client.getAllBuckets(bucket));
202
				
203
				bucket = new BucketEntity();
204
				bucket.setDfsName(dfsEntity.getDfsName());
205
				bucket.setBucketName(bucketName);
206
				bucket.setRegionName(dfsEntity.getRegionName());
207
				
208
				//创建目录,用户需要在minio里有创建目录的权限
209
				client.createBucket(bucket);
210
				
211
				bucket = new BucketEntity();
212
				bucket.setDfsName(dfsEntity.getDfsName());
213
				bucket.setRegionName(dfsEntity.getRegionName());
214
				LOGGER.debug("bucket list: "
215
						+ client.getAllBuckets(bucket));
216
				
217
				result.put("result", "从nacos server:" + IpuNacosConfig.getNacosServerAddr() + "同步" + nacosConfig.getIpuDfsDataid() + "成功");
218
			}
219
			else
220
				result.put("result", "从nacos server:" + IpuNacosConfig.getNacosServerAddr() + "同步" + nacosConfig.getIpuDfsDataid() + "失败");
221
	
222
		} catch (Exception e)
223
		{
224
			String errorMsg = "从nacos server:" + IpuNacosConfig.getNacosServerAddr() + "同步" + nacosConfig.getIpuDfsDataid() + "错误:" + e.toString();
225
			LOGGER.error(errorMsg);
226
			result.put("result", errorMsg);
227
		}finally {
228
			
229
		}
230
		return result;
231
	}
232
	
233
	@ResponseBody
234
    @RequestMapping("/syncIpuConfigUsingIpuDatabase")
235
    public JMap syncIpuConfigUsingIpuDatabase(JMap param) throws Exception {
236
		JMap result = new JsonMap();
237
		final String CONN_NAME = "test";
238
		final String DEFAULT_TABLE_NAME = "ipu_db_demo";
239
		
240
		String tableName = param.getString("tableName", DEFAULT_TABLE_NAME);
241
		
242
		try {
243
			SqlDao dao = IpuDaoManager.takeDao(SqlDao.class, CONN_NAME);
244
			String sql = "select * from " + tableName;
245
			List<Map<String, Object>> dbResult = dao.executeSelect(sql);
246
			LOGGER.debug("查询结果:" + dbResult);
247
					
248
			//从nacos配置中心获取ipu-database.xml的配置
249
			String ipuDatabaseXml = service.pullFromServer(nacosConfig.getIpuDatabaseDataid(), nacosConfig.getIpuDatabaseGroup(), nacosConfig.getIpuDatabaseTimeoutms());
250

251
			//加载到ipu-database的系统配置中
252
			if (!StringUtil.isEmpty(ipuDatabaseXml))
253
			{
254
				Map map = ipuDatabaseXmlToMap(ipuDatabaseXml, "utf-8");	
255
				LOGGER.debug("map:" + map);
256
				Iterator it = map.entrySet().iterator();
257
				while (it.hasNext()) {
258
					Entry entry = (Entry) it.next();
259
					Map<String, Object> attrs = (Map<String, Object>) entry.getValue();
260
					Map<String, String> properties = (Map<String, String>) attrs.get("property");
261
					DataSourceEntity dataSource = new DataSourceEntity();
262
					dataSource.setName((String) entry.getKey());
263
					dataSource.setUrl(properties.get("jdbcUrl"));
264
					dataSource.setUsername(properties.get("username"));
265
					dataSource.setPassword(properties.get("password"));
266
					MybatisUtil.registerDataSource(dataSource.getName(), dataSource);
267

268
				}
269
				//手工增加版本,需要修改ipu-database代码,才能实现自动为connName加版本,此时这里只需要输入test即可。
270
				dao = IpuDaoManager.takeDao(SqlDao.class, CONN_NAME + "1.0");
271
				sql = "select * from " + tableName;
272
				dbResult = dao.executeSelect(sql);
273
				LOGGER.debug("查询结果:" + dbResult);	
274
				
275
				result.put("result", "从nacos server:" + IpuNacosConfig.getNacosServerAddr() + "同步" + nacosConfig.getIpuDatabaseDataid() + "成功");
276
			}
277
			else
278
				result.put("result", "从nacos server:" + IpuNacosConfig.getNacosServerAddr() + "同步" + nacosConfig.getIpuDatabaseDataid() + "失败");
279
	
280
		} catch (Exception e)
281
		{
282
			String errorMsg = "从nacos server:" + IpuNacosConfig.getNacosServerAddr() + "同步" + nacosConfig.getIpuDatabaseDataid() + "错误:" + e.toString();
283
			LOGGER.error(errorMsg);
284
			result.put("result", errorMsg);
285
		}finally {
286
			
287
		}
288
		return result;
289
	}
290
	
291
    /**
292
     * @description 将ipu-database的xml字符串转换成map
293
     * @param xml
294
     * @return Map
295
     * @throws Exception 
296
     */
297
    private Map ipuDatabaseXmlToMap(String xml, String charsetName) throws Exception {
298
        Dom4jHelper dh = new Dom4jHelper(xml, charsetName);
299
        Map<String, ?> originData = dh.getAll();
300
        List<Map<String, Object>> configs = (List<Map<String, Object>>) originData.get("environments");
301
        Map dbsMap = new HashMap<String, Object>();
302
        Map<String, Object> dbMap;
303
        Map<String, String> configAttr;
304
        /*循环缓存实例*/
305
        for (Map<String, Object> config : configs) {
306
            dbMap = new HashMap<String, Object>();
307
            configAttr = new HashMap<String, String>();
308
            
309
            Map<String, String> dbAttrMap = (Map<String, String>) config.get("environment" + Dom4jHelper.SUFFIX_ATTR);
310
            String datdbaseName = dbAttrMap.get("id");
311
            List<Map<String, Object>> dbList = (List<Map<String, Object>>)config.get("environment");
312
            String type = "";
313
            String driver = "";
314
            for(Map<String, Object> map : dbList){
315
            	if(map.get("transactionManager_attr")!=null){
316
                    /*解析缓存实例的服务列表*/
317
                    Map<String, Object> serverMap = (Map<String, Object>)map.get("transactionManager_attr");
318
                    type = (String)serverMap.get("type");
319
                }
320
            	if(map.get("dataSource_attr")!=null){
321
                    /*解析缓存实例的服务列表*/
322
                    Map<String, Object> serverMap = (Map<String, Object>)map.get("dataSource_attr");
323
                    driver = (String)serverMap.get("type");
324
                }
325
            	if(map.get("dataSource")!=null){
326
                    /*解析缓存实例的服务列表*/
327
            		List<Map<String, Object>> serverList = (List<Map<String, Object>>)map.get("dataSource");
328
//                    List<Map<String, Object>> serverList = (List<Map<String, Object>>)serverMap.get("property_attr");
329
                    for(Map<String, Object> configValue : serverList){
330
                    	Map<String, Object> configMap = (Map<String, Object>)configValue.get("property_attr");
331
                    	configAttr.put((String)configMap.get("name"), (String)configMap.get("value"));
332
                    }
333
                                        
334
                }
335
                
336
            }            
337
            dbMap.put("databaseName", datdbaseName+"1.0");
338
            dbMap.put("type", type);
339
            dbMap.put("property", configAttr);
340
            dbMap.put("dataSource_driver", driver);
341
            dbsMap.put(datdbaseName+"1.0", dbMap);
342
        }
343
        return dbsMap;
344
    }
345
    
346
	@ResponseBody
347
    @RequestMapping("/scheduleIpuConfigUsingIpuCache")
348
    public JMap scheduleIpuConfigUsingIpuCache(JMap param) throws Exception {
349
		JMap result = new JsonMap();	
350
		try {		
351
			//监听配置中心里ipu-cache.xml的配置
352
			service.addListener(nacosConfig.getIpuCacheDataid(), nacosConfig.getIpuCacheGroup(), new IpuCacheListener());
353

354
			result.put("result", "监听配置中心:" + IpuNacosConfig.getNacosServerAddr() + "ipu-cache.xml的配置" + nacosConfig.getIpuCacheDataid() + "成功");		
355
		} catch (Exception e)
356
		{
357
			String errorMsg = "监听配置中心:" + IpuNacosConfig.getNacosServerAddr() + "ipu-cache.xml的配置" + nacosConfig.getIpuCacheDataid() + "错误:" + e.toString();
358
			LOGGER.error(errorMsg);
359
			result.put("result", errorMsg);
360
		}
361
		return result;
362
	}
363
}

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

@ -1,53 +0,0 @@
1
package com.ai.ipu.server.demo.control.nacos;
2
3
import java.util.Map;
4
5
6
import org.slf4j.Logger;
7
import org.slf4j.LoggerFactory;
8
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.beans.factory.annotation.Value;
10
import org.springframework.cloud.context.config.annotation.RefreshScope;
11
import org.springframework.web.bind.annotation.GetMapping;
12
import org.springframework.web.bind.annotation.RequestMapping;
13
import org.springframework.web.bind.annotation.ResponseBody;
14
import org.springframework.web.bind.annotation.RestController;
15
import org.springframework.web.client.RestTemplate;
16
17
import com.ai.ipu.server.demo.config.NacosConfig;
18
import com.alibaba.nacos.api.exception.NacosException;
19
import com.google.common.collect.Maps;
20
21
22
@RestController
23
@RefreshScope
24
@RequestMapping("/nacos/discovery/restConsumer")
25
public class SpringCloudConsumerController {
26
	private static final Logger logger = LoggerFactory.getLogger(SpringCloudConsumerController.class);
27
28
    @Autowired
29
    private RestTemplate restTemplate;
30
31
    
32
    @Value("${spring.application.name}")
33
    private String appName;
34
    
35
    @Autowired
36
	NacosConfig config;
37
    
38
	@ResponseBody
39
    @GetMapping(value = "/service002")
40
    public String service002() throws NacosException {
41
		
42
		try {
43
			/*用于ipu-spring-plumlog
44
			Map<String, Object> variables = Maps.newHashMap();
45
			variables.put("message", appName);*/
46
			return restTemplate.getForObject("http://ipu-nacos-cloud-provider" + config.getService002Url() + "?message=" + appName , String.class);
47
		} catch (Exception e) {
48
			logger.error(e.getMessage());
49
		}
50
		
51
        return null;
52
    }
53
}

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

@ -1,40 +0,0 @@
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 05 16 12 9 ? 2021", config.getIpuCacheDataid(), configInfo);
32
			scheduledTask.addCronJob(cron.toString(), config.getIpuCacheDataid(), configInfo);
33
34
		} catch (Exception e) {
35
			log.error("receiveConfigInfo error:", e);
36
		}
37
	}
38
39
}
40

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

@ -1,44 +0,0 @@
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
}

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

@ -1,77 +0,0 @@
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
}  

+ 2 - 16
ipu-rest-demo/src/main/resources/dev/application.properties

@ -9,11 +9,11 @@ spring.mvc.static-path-pattern=/**
9 9
spring.resources.static-locations=classpath:/${web.root}
10 10

11 11
#1.x默认/
12
#server.context-path=/ipu
12
server.context-path=/ipu
13 13
#默认/。注册DispatcherServlet对应path,亦可通过dispatcherRegistration方法配置
14 14
#server.servlet-path=/
15 15
#2.x
16
server.servlet.context-path=/ipu
16
#server.servlet.context-path=/ipu
17 17
#使用maven中的变量替换
18 18
logging.file=target/logs/${project.artifactId}.log
19 19
spring.application.name=${project.artifactId}
@ -49,20 +49,6 @@ management.security.enabled=false
49 49
#spring.devtools.restart.additional-paths=src/main/java
50 50
#spring.devtools.restart.exclude=WEB-INF/**
51 51

52
#使用zookeeper当注册中心
53
spring.main.allow-bean-definition-overriding=true
54
#dubbo.application.name=dubbo_consumer
55
#dubbo.protocol.name=dubbo
56
dubbo.protocol.port=-1
57
dubbo.consumer.check=false
58
dubbo.registry.check=false
59
#dubbo.registry.address=zookeeper://47.105.160.21:2108
60
#dubbo.registry.client=curator
61

62
#使用nacos当注册中心
63
dubbo.application.name=dubbo_consumer
64
dubbo.registry.address=nacos://47.105.160.21:8849
65
dubbo.protocol.name=dubbo
66 52

67 53
#spring-data-es配置
68 54
spring.elasticsearch.rest.uris=http://47.105.160.21:10020

+ 0 - 22
ipu-rest-demo/src/main/resources/dev/dubbo-consumer-simple.xml

@ -1,22 +0,0 @@
1
<?xml version="1.0" encoding="UTF-8"?>
2
<beans xmlns="http://www.springframework.org/schema/beans"
3
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
4
	xsi:schemaLocation="http://www.springframework.org/schema/beans
5
	http://www.springframework.org/schema/beans/spring-beans.xsd
6
	http://code.alibabatech.com/schema/dubbo 
7
	http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
8
	<!-- 全局配置 start=========================== -->
9
	<dubbo:application name="ipu-rest-demo" />
10
	<!-- 关闭重试,延长超时 -->
11
	<dubbo:consumer retries="0" timeout="2000"/>	<!-- client="netty4" -->
12
	<!-- 换成实际地址可以解决无法注册的异常 -->
13
	<dubbo:registry address="127.0.0.1:20880" />
14
	<!-- 全局配置 end=========================== -->
15

16
	<!-- 服务注册配置 start=========================== -->
17
	<dubbo:reference id="bizService"
18
		interface="com.ai.ipu.test.springboot.service.IBizService" />
19
		<dubbo:reference id="ipuService"
20
		interface="com.ai.ipu.test.springboot.service.IpuService" />
21
	<!-- 服务注册配置 end =========================== -->
22
</beans>