Parcourir la Source

引入msgframe的Demo使用案例

huangbo 7 ans auparavant
Parent
commit
7039cfabfd

+ 39 - 0
ipu-rest-demo/pom.xml

@ -14,6 +14,7 @@
14 14
	<url>http://maven.apache.org</url>
15 15
16 16
	<repositories>
17
		<!-- IPU相关仓库 -->
17 18
		<repository>
18 19
			<id>ipu</id>
19 20
			<name>ipu repository</name>
@ -26,10 +27,21 @@
26 27
				<updatePolicy>always</updatePolicy>
27 28
			</snapshots>
28 29
		</repository>
30
		<!-- AFI私服,内网私服 -->
31
		<repository>
32
			<id>aif_group</id>
33
			<url>http://10.11.20.81:8080/nexus/content/groups/aif_group/</url>
34
		</repository>
35
		<repository>
36
			<id>msgframe</id>
37
			<name>msgframe</name>
38
			<url>http://10.11.20.81:8080/nexus/content/repositories/msgframe/</url>
39
		</repository>
29 40
	</repositories>
30 41
31 42
	<properties>
32 43
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
44
		<msgframe>2.0.0</msgframe>
33 45
	</properties>
34 46
35 47
	<dependencies>
@ -61,6 +73,18 @@
61 73
			<groupId>com.ai.ipu</groupId>
62 74
			<artifactId>ipu-server-web</artifactId>
63 75
		</dependency>
76
		
77
		<!-- msgframe相关依赖 -->
78
		<dependency>
79
			<groupId>msgframe</groupId>
80
			<artifactId>msgframe-client</artifactId>
81
			<version>${msgframe}</version>
82
		</dependency>
83
		<dependency>
84
			<groupId>msgframe</groupId>
85
			<artifactId>msgframe-common</artifactId>
86
			<version>${msgframe}</version>
87
		</dependency>
64 88
	</dependencies>
65 89
66 90
	<build>
@ -80,6 +104,21 @@
80 104
					<mainClass>com.ai.ipu.server.demo.IpuRestDemoStart</mainClass>
81 105
				</configuration>
82 106
			</plugin>
107
			
108
			<plugin>
109
				<groupId>org.apache.maven.plugins</groupId>
110
				<artifactId>maven-compiler-plugin</artifactId>
111
				<configuration>
112
					<source>1.8</source>
113
					<target>1.8</target>
114
					<encoding>UTF-8</encoding>
115
					<!-- 这里添加本地jar -->
116
					<compilerArguments>
117
 					<bootclasspath>${JAVA_HOME}/jre/lib/rt.jar</bootclasspath>
118
 					<extdirs>${project.basedir}/libs</extdirs>
119
					</compilerArguments>
120
				</configuration>
121
			</plugin>
83 122
		</plugins>
84 123
		<finalName>${project.artifactId}</finalName>
85 124
	</build>

+ 36 - 0
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/control/other/MsgFrameController.java

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

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

7
import com.ai.aif.msgframe.MfProducerClient;
8
import com.ai.aif.msgframe.common.message.MsgFTextMessage;
9
import com.ai.ipu.basic.util.IpuUtility;
10
import com.ai.ipu.restful.util.IpuRestConstant;
11
import com.ailk.common.data.IData;
12
import com.ailk.common.data.impl.DataMap;
13

14
@Controller
15
@RequestMapping("/msgframe")
16
public class MsgFrameController{
17

18
    @ResponseBody
19
    @RequestMapping("/produce")
20
    public IData produceMessage(IData params){
21
        IData result = new DataMap();
22
        try {
23
            MfProducerClient client = new MfProducerClient();
24
            MsgFTextMessage message = new MsgFTextMessage();
25
            message.setText(params.toString());
26
            client.send("myTestTopic", message);
27
        } catch (Throwable e) {
28
            // TODO Auto-generated catch block
29
            e.printStackTrace();
30
            IpuUtility.error(e);
31
        }
32
        result.put(IpuRestConstant.IPU_RESULT_CODE, IpuRestConstant.ResultCode.SUCCESS_CODE);
33
        result.put(IpuRestConstant.IPU_RESULT_INFO, "success");
34
        return result;
35
    }
36
}

+ 48 - 0
ipu-rest-demo/src/main/resources/msgframe-config.xml

@ -0,0 +1,48 @@
1
<?xml version="1.0" encoding="UTF-8"?>
2
<msgframeCfg xmlns="http://www.asiainfo.com/msgframe">
3
	<centerCfg>
4
		<!--[1-1] 中心名称  定义当前中心或者当前系统的名称编码,全局配置唯一性-->
5
		<name>orderCenter</name>
6
		<!-- 全局配置  影响全局的一些参数配置,如入职中心的开关配置,整体的序列化方式等,全局配置唯一性 -->
7
		<globalCfg>
8
			<!-- 消息生产端消息注入处理类,可以不配或者多配,在发送时可以在消息发送前执行,配置类的全路径且继承接口 com.cmos.msgframe.common.IProsInjectionProcessor -->
9
			<!-- 		<prodInjection>sadsa</prodInjection> -->
10
			<!-- 消息消费端消息注入处理类,可以不配或者多配,在消费时可以在消息处理业务前执行,配置类的全路径且继承接口 com.cmos.msgframe.common.IConsInjectionProcessor -->
11
			<!-- 		<consInjection>sadsa</consInjection> -->
12
		</globalCfg>
13
		<!-- 总的主题列表配置信息,包含所有的主题配置信息,全局配置唯一性 -->
14
		<destinations>
15
		    <queue name="myTestTopic" belong="orderCenter"/>
16
		</destinations>
17
		<!-- 消费端的配置信息,主要包含订阅的主题和消息的业务实现类,全局配置唯一性 -->
18
		<subscribes>
19
		    <!-- 主题订阅信息,订阅在<destinations></destinations>标签中的主题消息 -->
20
		    <subscribe subDestination="myTestTopic" consumeType="push">
21
				<implclass>com.ai.ipu.bit.push.msgframe.consumer.ConsumerProcessorImpl</implclass>
22
			</subscribe>
23
		</subscribes>
24
		<!-- 主要配置中心(系统)和其所属集群的关系,全局配置唯一性 -->
25
		<centers>
26
			<center name="orderCenter" containClusters="cluster-1" />
27
		</centers>
28
		<!-- 集群配置,主要配置当前中心或者系统用到的所有集群信息,全局配置唯一性 -->
29
		<clusters>
30
		<!--RocketMQ、Kafka、ActiveMQ  -->
31
			<cluster name="cluster-1" type="RocketMQ">
32
					<!-- 本地搭建的RocketMQ -->
33
  				    <!-- <url>10.211.55.4:9876</url> -->
34
  				    <!--阿里云部署的RocketMQ -->
35
  				    <url>101.200.53.108:9876</url>
36
			</cluster>
37
		</clusters>
38
		<!-- 持久化配置区,主要包括生产端和消费端的异常持久化配置、生产端和消费端的日志持久化配置,全局配置唯一性 -->
39
		<persistence>
40
			<exceptionPersistence>
41
				<!-- 生产者异常处理,必须实现com.ai.aif.msgframe.common.ex.exception.IExceptionPersitence接口 -->
42
				<producerExceptionClass>com.ai.aif.msgframe.producer.ex.ProducerExceptionHandle</producerExceptionClass>
43
				<!-- 消费者异常处理,必须实现com.ai.aif.msgframe.common.ex.exception.IExceptionPersitence接口 -->
44
				<consumerExceptionClass>com.ai.aif.msgframe.consumer.ex.ConsumerExceptionHandle</consumerExceptionClass>
45
			</exceptionPersistence>
46
		</persistence>
47
	</centerCfg>
48
</msgframeCfg>

+ 37 - 0
ipu-service-demo/pom.xml

@ -23,10 +23,21 @@
23 23
				<updatePolicy>always</updatePolicy>
24 24
			</snapshots>
25 25
		</repository>
26
		<!-- AFI私服,内网私服 -->
27
		<repository>
28
			<id>aif_group</id>
29
			<url>http://10.11.20.81:8080/nexus/content/groups/aif_group/</url>
30
		</repository>
31
		<repository>
32
			<id>msgframe</id>
33
			<name>msgframe</name>
34
			<url>http://10.11.20.81:8080/nexus/content/repositories/msgframe/</url>
35
		</repository>
26 36
	</repositories>
27 37
	<properties>
28 38
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
29 39
		<ipu>3.0-SNAPSHOT</ipu>
40
		<msgframe>2.0.0</msgframe>
30 41
	</properties>
31 42
32 43
	<dependencies>
@ -54,6 +65,17 @@
54 65
			<version>4.1.8.Final</version>
55 66
		</dependency>
56 67
		-->
68
		<!-- msgframe相关依赖 -->
69
		<dependency>
70
			<groupId>msgframe</groupId>
71
			<artifactId>msgframe-server</artifactId>
72
			<version>${msgframe}</version>
73
		</dependency>
74
		<dependency>
75
			<groupId>msgframe</groupId>
76
			<artifactId>msgframe-common</artifactId>
77
			<version>${msgframe}</version>
78
		</dependency>
57 79
	</dependencies>
58 80
	
59 81
	<!-- 对应开发、测试、生产三种环境 -->
@ -93,6 +115,21 @@
93 115
					<skip>true</skip>
94 116
				</configuration>
95 117
			</plugin>
118
			
119
			<plugin>
120
				<groupId>org.apache.maven.plugins</groupId>
121
				<artifactId>maven-compiler-plugin</artifactId>
122
				<configuration>
123
					<source>1.8</source>
124
					<target>1.8</target>
125
					<encoding>UTF-8</encoding>
126
					<!-- 这里添加本地jar -->
127
					<compilerArguments>
128
 					<bootclasspath>${JAVA_HOME}/jre/lib/rt.jar</bootclasspath>
129
 					<extdirs>${project.basedir}/libs</extdirs>
130
					</compilerArguments>
131
				</configuration>
132
			</plugin>
96 133
		</plugins>
97 134
		<resources>
98 135
            <resource>

+ 8 - 1
ipu-service-demo/src/main/java/com/ai/ipu/service/DubboServiceStart.java

@ -2,7 +2,9 @@ package com.ai.ipu.service;
2 2

3 3
import org.springframework.context.support.ClassPathXmlApplicationContext;
4 4

5
public class DubboServiceStart {
5
import com.ai.aif.msgframe.consumer.MfServiceStartup;
6

7
public class IpuServiceDemoStart {
6 8
    public final static String MODE = "simple";
7 9
    public final static String DUBBO_PROVIDER_CONFIG = "dubbo-provider-" + MODE + ".xml";
8 10
    public final static String DUBBO_CONSUMER_CONFIG = "dubbo-consumer-" + MODE + ".xml";
@ -12,6 +14,11 @@ public class DubboServiceStart {
12 14
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
13 15
                new String[] {DUBBO_PROVIDER_CONFIG});
14 16
        context.start();
17
        /*启动消息中间件的消费者服务*/
18
        /*启动所有的订阅*/
19
        MfServiceStartup.main();
20
        /*启动指定的订阅*/
21
//        MfConsumerClient.subscribe("myTestTopic", "*", "com.ai.ipu.service.demo.consumer.ConsumerProcessorImpl");
15 22
        System.in.read(); // press any key to exit
16 23
    }
17 24
}

+ 33 - 0
ipu-service-demo/src/main/java/com/ai/ipu/service/demo/consumer/ConsumerProcessorImpl.java

@ -0,0 +1,33 @@
1
package com.ai.ipu.service.demo.consumer;
2

3
import org.springframework.context.support.ClassPathXmlApplicationContext;
4

5
import com.ai.aif.msgframe.common.IConsumerProcessor;
6
import com.ai.aif.msgframe.common.exception.ConsumerException;
7
import com.ai.aif.msgframe.common.message.MsgFMessage;
8
import com.ai.aif.msgframe.common.message.MsgFTextMessage;
9
import com.ai.ipu.service.IpuServiceDemoStart;
10
import com.ai.ipu.service.demo.IDbService;
11
import com.ailk.common.data.IData;
12
import com.ailk.common.data.impl.DataMap;
13

14

15
public class ConsumerProcessorImpl implements IConsumerProcessor {
16

17
    public Object process(MsgFMessage msg) throws ConsumerException{
18
        ClassPathXmlApplicationContext context = 
19
                new ClassPathXmlApplicationContext(new String[] { IpuServiceDemoStart.DUBBO_CONSUMER_CONFIG });
20
        context.start();
21
        String text = ((MsgFTextMessage)msg).getText();
22
        IData param = new DataMap(text);
23
        IDbService dbService = (IDbService) context.getBean("dbService");
24
        try {
25
            IData result = dbService.select(param);
26
            System.out.println("数据库查询结果为:" + result);
27
        }catch (Exception e) {
28
            e.printStackTrace();
29
        }
30
        return true;
31
    }
32

33
}

+ 2 - 2
ipu-service-demo/src/test/java/com/ai/ipu/service/demo/impl/AsyncServiceTest.java

@ -6,7 +6,7 @@ import junit.framework.TestCase;
6 6

7 7
import org.springframework.context.support.ClassPathXmlApplicationContext;
8 8

9
import com.ai.ipu.service.DubboServiceStart;
9
import com.ai.ipu.service.IpuServiceDemoStart;
10 10
import com.ai.ipu.service.demo.IAsyncService;
11 11
import com.ailk.common.data.IData;
12 12
import com.alibaba.dubbo.rpc.RpcContext;
@ -17,7 +17,7 @@ public class AsyncServiceTest extends TestCase{
17 17
    @Override
18 18
    protected void setUp() throws Exception {
19 19
        // TODO Auto-generated method stub
20
        context = new ClassPathXmlApplicationContext(new String[] { DubboServiceStart.DUBBO_CONSUMER_CONFIG });
20
        context = new ClassPathXmlApplicationContext(new String[] { IpuServiceDemoStart.DUBBO_CONSUMER_CONFIG });
21 21
        context.start();
22 22
    }
23 23
    

+ 2 - 2
ipu-service-demo/src/test/java/com/ai/ipu/service/demo/impl/BizServiceTest.java

@ -4,7 +4,7 @@ import junit.framework.TestCase;
4 4

5 5
import org.springframework.context.support.ClassPathXmlApplicationContext;
6 6

7
import com.ai.ipu.service.DubboServiceStart;
7
import com.ai.ipu.service.IpuServiceDemoStart;
8 8
import com.ai.ipu.service.demo.IBizService;
9 9
import com.ailk.common.data.IData;
10 10
import com.ailk.common.data.impl.DataMap;
@ -15,7 +15,7 @@ public class BizServiceTest extends TestCase {
15 15
    @Override
16 16
    protected void setUp() throws Exception {
17 17
        // TODO Auto-generated method stub
18
        context = new ClassPathXmlApplicationContext(new String[] { DubboServiceStart.DUBBO_CONSUMER_CONFIG });
18
        context = new ClassPathXmlApplicationContext(new String[] { IpuServiceDemoStart.DUBBO_CONSUMER_CONFIG });
19 19
        context.start();
20 20
    }
21 21
    

+ 2 - 2
ipu-service-demo/src/test/java/com/ai/ipu/service/demo/impl/CallBackServiceTest.java

@ -4,7 +4,7 @@ import junit.framework.TestCase;
4 4

5 5
import org.springframework.context.support.ClassPathXmlApplicationContext;
6 6

7
import com.ai.ipu.service.DubboServiceStart;
7
import com.ai.ipu.service.IpuServiceDemoStart;
8 8
import com.ai.ipu.service.demo.ICallbackService;
9 9
import com.ai.ipu.service.demo.ICallbackService.ICallbackListener;
10 10

@ -14,7 +14,7 @@ public class CallBackServiceTest extends TestCase {
14 14
    @Override
15 15
    protected void setUp() throws Exception {
16 16
        // TODO Auto-generated method stub
17
        context = new ClassPathXmlApplicationContext(new String[] { DubboServiceStart.DUBBO_CONSUMER_CONFIG });
17
        context = new ClassPathXmlApplicationContext(new String[] { IpuServiceDemoStart.DUBBO_CONSUMER_CONFIG });
18 18
        context.start();
19 19
    }
20 20
    

+ 2 - 2
ipu-service-demo/src/test/java/com/ai/ipu/service/demo/impl/ContextServiceTest.java

@ -4,7 +4,7 @@ import junit.framework.TestCase;
4 4

5 5
import org.springframework.context.support.ClassPathXmlApplicationContext;
6 6

7
import com.ai.ipu.service.DubboServiceStart;
7
import com.ai.ipu.service.IpuServiceDemoStart;
8 8
import com.ai.ipu.service.demo.IContextService;
9 9
import com.ai.ipu.service.dubbo.DubboContextData;
10 10
import com.ailk.common.data.IData;
@ -16,7 +16,7 @@ public class ContextServiceTest extends TestCase{
16 16
    @Override
17 17
    protected void setUp() throws Exception {
18 18
        // TODO Auto-generated method stub
19
        context = new ClassPathXmlApplicationContext(new String[] { DubboServiceStart.DUBBO_CONSUMER_CONFIG });
19
        context = new ClassPathXmlApplicationContext(new String[] { IpuServiceDemoStart.DUBBO_CONSUMER_CONFIG });
20 20
        context.start();
21 21
    }
22 22
    

+ 2 - 2
ipu-service-demo/src/test/java/com/ai/ipu/service/demo/impl/DbServiceTest.java

@ -4,7 +4,7 @@ import junit.framework.TestCase;
4 4

5 5
import org.springframework.context.support.ClassPathXmlApplicationContext;
6 6

7
import com.ai.ipu.service.DubboServiceStart;
7
import com.ai.ipu.service.IpuServiceDemoStart;
8 8
import com.ai.ipu.service.demo.IDbService;
9 9
import com.ailk.common.data.IData;
10 10

@ -14,7 +14,7 @@ public class DbServiceTest extends TestCase {
14 14
    @Override
15 15
    protected void setUp() throws Exception {
16 16
        // TODO Auto-generated method stub
17
        context = new ClassPathXmlApplicationContext(new String[] { DubboServiceStart.DUBBO_CONSUMER_CONFIG });
17
        context = new ClassPathXmlApplicationContext(new String[] { IpuServiceDemoStart.DUBBO_CONSUMER_CONFIG });
18 18
        context.start();
19 19
    }
20 20
    

+ 2 - 2
ipu-service-demo/src/test/java/com/ai/ipu/service/demo/impl/EchoServiceTest.java

@ -4,7 +4,7 @@ import junit.framework.TestCase;
4 4

5 5
import org.springframework.context.support.ClassPathXmlApplicationContext;
6 6

7
import com.ai.ipu.service.DubboServiceStart;
7
import com.ai.ipu.service.IpuServiceDemoStart;
8 8
import com.alibaba.dubbo.rpc.service.EchoService;
9 9

10 10
public class EchoServiceTest extends TestCase{
@ -13,7 +13,7 @@ public class EchoServiceTest extends TestCase{
13 13
    @Override
14 14
    protected void setUp() throws Exception {
15 15
        // TODO Auto-generated method stub
16
        context = new ClassPathXmlApplicationContext(new String[] { DubboServiceStart.DUBBO_CONSUMER_CONFIG });
16
        context = new ClassPathXmlApplicationContext(new String[] { IpuServiceDemoStart.DUBBO_CONSUMER_CONFIG });
17 17
        context.start();
18 18
    }
19 19
    

+ 2 - 2
ipu-service-demo/src/test/java/com/ai/ipu/service/demo/impl/InterceptorServiceTest.java

@ -4,7 +4,7 @@ import junit.framework.TestCase;
4 4

5 5
import org.springframework.context.support.ClassPathXmlApplicationContext;
6 6

7
import com.ai.ipu.service.DubboServiceStart;
7
import com.ai.ipu.service.IpuServiceDemoStart;
8 8
import com.ai.ipu.service.demo.IBizService;
9 9
import com.ailk.common.data.IData;
10 10
import com.ailk.common.data.impl.DataMap;
@ -15,7 +15,7 @@ public class InterceptorServiceTest extends TestCase{
15 15
    @Override
16 16
    protected void setUp() throws Exception {
17 17
        // TODO Auto-generated method stub
18
        context = new ClassPathXmlApplicationContext(new String[] { DubboServiceStart.DUBBO_CONSUMER_CONFIG });
18
        context = new ClassPathXmlApplicationContext(new String[] { IpuServiceDemoStart.DUBBO_CONSUMER_CONFIG });
19 19
        context.start();
20 20
    }
21 21
    

+ 2 - 2
ipu-service-demo/src/test/java/com/ai/ipu/service/demo/impl/MockServiceTest.java

@ -4,7 +4,7 @@ import junit.framework.TestCase;
4 4

5 5
import org.springframework.context.support.ClassPathXmlApplicationContext;
6 6

7
import com.ai.ipu.service.DubboServiceStart;
7
import com.ai.ipu.service.IpuServiceDemoStart;
8 8
import com.ai.ipu.service.demo.IMockService;
9 9
import com.ailk.common.data.IData;
10 10
import com.ailk.common.data.impl.DataMap;
@ -15,7 +15,7 @@ public class MockServiceTest extends TestCase{
15 15
    @Override
16 16
    protected void setUp() throws Exception {
17 17
        // TODO Auto-generated method stub
18
        context = new ClassPathXmlApplicationContext(new String[] { DubboServiceStart.DUBBO_CONSUMER_CONFIG });
18
        context = new ClassPathXmlApplicationContext(new String[] { IpuServiceDemoStart.DUBBO_CONSUMER_CONFIG });
19 19
        context.start();
20 20
    }
21 21
    

+ 2 - 2
ipu-service-demo/src/test/java/com/ai/ipu/service/demo/impl/RestrictedServiceTest.java

@ -2,7 +2,7 @@ package com.ai.ipu.service.demo.impl;
2 2

3 3
import org.springframework.context.support.ClassPathXmlApplicationContext;
4 4

5
import com.ai.ipu.service.DubboServiceStart;
5
import com.ai.ipu.service.IpuServiceDemoStart;
6 6
import com.ai.ipu.service.demo.IMockService;
7 7
import com.ai.ipu.service.demo.IRestrictedService;
8 8
import com.ailk.common.data.IData;
@ -16,7 +16,7 @@ public class RestrictedServiceTest extends TestCase{
16 16
    @Override
17 17
    protected void setUp() throws Exception {
18 18
        // TODO Auto-generated method stub
19
        context = new ClassPathXmlApplicationContext(new String[] { DubboServiceStart.DUBBO_CONSUMER_CONFIG });
19
        context = new ClassPathXmlApplicationContext(new String[] { IpuServiceDemoStart.DUBBO_CONSUMER_CONFIG });
20 20
        context.start();
21 21
    }
22 22
    

+ 2 - 2
ipu-service-demo/src/test/java/com/ai/ipu/service/demo/impl/StubServiceTest.java

@ -4,7 +4,7 @@ import junit.framework.TestCase;
4 4

5 5
import org.springframework.context.support.ClassPathXmlApplicationContext;
6 6

7
import com.ai.ipu.service.DubboServiceStart;
7
import com.ai.ipu.service.IpuServiceDemoStart;
8 8
import com.ai.ipu.service.demo.IStubService;
9 9
import com.ailk.common.data.IData;
10 10
import com.ailk.common.data.impl.DataMap;
@ -15,7 +15,7 @@ public class StubServiceTest extends TestCase{
15 15
    @Override
16 16
    protected void setUp() throws Exception {
17 17
        // TODO Auto-generated method stub
18
        context = new ClassPathXmlApplicationContext(new String[] { DubboServiceStart.DUBBO_CONSUMER_CONFIG });
18
        context = new ClassPathXmlApplicationContext(new String[] { IpuServiceDemoStart.DUBBO_CONSUMER_CONFIG });
19 19
        context.start();
20 20
    }
21 21