Bladeren bron

Dubbo的最小Demo

huangbo 6 jaren geleden
bovenliggende
commit
7f010c7c11

+ 1 - 0
dubbo-example/.gitignore

@ -0,0 +1 @@
1
/.settings/

+ 17 - 0
dubbo-example/.project

@ -0,0 +1,17 @@
1
<?xml version="1.0" encoding="UTF-8"?>
2
<projectDescription>
3
	<name>dubbo-example</name>
4
	<comment></comment>
5
	<projects>
6
	</projects>
7
	<buildSpec>
8
		<buildCommand>
9
			<name>org.eclipse.m2e.core.maven2Builder</name>
10
			<arguments>
11
			</arguments>
12
		</buildCommand>
13
	</buildSpec>
14
	<natures>
15
		<nature>org.eclipse.m2e.core.maven2Nature</nature>
16
	</natures>
17
</projectDescription>

+ 3 - 0
dubbo-example/dubbo-service-provider/.gitignore

@ -0,0 +1,3 @@
1
/.settings/
2
/target/
3
/.classpath

+ 23 - 0
dubbo-example/dubbo-service-provider/.project

@ -0,0 +1,23 @@
1
<?xml version="1.0" encoding="UTF-8"?>
2
<projectDescription>
3
	<name>dubbo-service-provider</name>
4
	<comment></comment>
5
	<projects>
6
	</projects>
7
	<buildSpec>
8
		<buildCommand>
9
			<name>org.eclipse.jdt.core.javabuilder</name>
10
			<arguments>
11
			</arguments>
12
		</buildCommand>
13
		<buildCommand>
14
			<name>org.eclipse.m2e.core.maven2Builder</name>
15
			<arguments>
16
			</arguments>
17
		</buildCommand>
18
	</buildSpec>
19
	<natures>
20
		<nature>org.eclipse.jdt.core.javanature</nature>
21
		<nature>org.eclipse.m2e.core.maven2Nature</nature>
22
	</natures>
23
</projectDescription>

+ 29 - 0
dubbo-example/dubbo-service-provider/pom.xml

@ -0,0 +1,29 @@
1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
	<modelVersion>4.0.0</modelVersion>
4
	<parent>
5
		<groupId>com.ai.ipu.example</groupId>
6
		<artifactId>dubbo-example</artifactId>
7
		<version>1.0-SNAPSHOT</version>
8
	</parent>
9

10
	<artifactId>dubbo-service-provider</artifactId>
11
	<name>dubbo-service-provider</name>
12
	<description>dubbo服务提供者</description>
13
	
14
	<properties>
15
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16
	</properties>
17

18
	<dependencies>
19
		<dependency>
20
			<groupId>junit</groupId>
21
			<artifactId>junit</artifactId>
22
			<scope>test</scope>
23
		</dependency>
24
		<dependency>
25
			<groupId>com.ai.ipu.server</groupId>
26
			<artifactId>ipu-service</artifactId>
27
		</dependency>
28
	</dependencies>
29
</project>

+ 17 - 0
dubbo-example/dubbo-service-provider/src/main/java/com/ai/ipu/dubbo/provider/DubboServiceProviderStart.java

@ -0,0 +1,17 @@
1
package com.ai.ipu.dubbo.provider;
2

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

5
public class DubboServiceProviderStart {
6
    public final static String MODE = "simple";
7
    public final static String DUBBO_PROVIDER_CONFIG = "dubbo-provider-" + MODE + ".xml";
8
    public final static String DUBBO_CONSUMER_CONFIG = "dubbo-consumer-" + MODE + ".xml";
9
    
10
    @SuppressWarnings("resource")
11
    public static void main(String[] args) throws Exception {
12
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
13
                new String[] {DUBBO_PROVIDER_CONFIG});
14
        context.start();
15
        System.in.read(); // press any key to exit
16
    }
17
}

+ 6 - 0
dubbo-example/dubbo-service-provider/src/main/java/com/ai/ipu/dubbo/provider/HelloService.java

@ -0,0 +1,6 @@
1
package com.ai.ipu.dubbo.provider;
2

3
public interface HelloService {
4
    
5
    public String hello(String name);
6
}

+ 10 - 0
dubbo-example/dubbo-service-provider/src/main/java/com/ai/ipu/dubbo/provider/impl/HelloServiceImpl.java

@ -0,0 +1,10 @@
1
package com.ai.ipu.dubbo.provider.impl;
2

3
import com.ai.ipu.dubbo.provider.HelloService;
4

5
public class HelloServiceImpl implements HelloService{
6

7
    public String hello(String name) {
8
        return "hello" + name;
9
    }
10
}

+ 32 - 0
dubbo-example/dubbo-service-provider/src/main/resources/dubbo-provider-simple.xml

@ -0,0 +1,32 @@
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"
4
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
5
	xsi:schemaLocation="http://www.springframework.org/schema/beans
6
	http://www.springframework.org/schema/beans/spring-beans.xsd
7
	http://code.alibabatech.com/schema/dubbo 
8
	http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
9
    <!-- 全局配置 start=========================== -->
10
    <dubbo:application name="ipu-service-demo" logger="log4j"> <!-- 日志适配 -->
11
    	<dubbo:parameter key="dump.directory" value="/tmp" /> <!-- 线程栈自动dump -->
12
    </dubbo:application>
13
    <!-- 过滤器配置,服务延迟暴露 -->
14
    <dubbo:provider filter="IpuDubboServiceGlobalFilter" delay="-1" /><!-- server="netty4"  -->
15
    <!-- 服务协议 -->
16
    <dubbo:protocol name="dubbo" port="20880"/>
17
    <!-- 广播的方式注册 -->
18
    <dubbo:registry address="127.0.0.1:20880"/>
19
    <!-- 提供simple注册中心 -->
20
    <dubbo:service interface="com.alibaba.dubbo.registry.RegistryService" ref="registryService" registry="N/A" ondisconnect="disconnect" callbacks="1000">
21
        <dubbo:method name="subscribe"><dubbo:argument index="1" callback="true" /></dubbo:method>
22
        <dubbo:method name="unsubscribe"><dubbo:argument index="1" callback="false" /></dubbo:method>
23
    </dubbo:service>
24
    <!-- 简单注册中心实现,可自行扩展实现集群和状态同步 -->
25
    <bean id="registryService" class="com.alibaba.dubbo.registry.simple.SimpleRegistryService" />
26
    <!-- 全局配置 end=========================== -->
27
    
28
    <!-- 服务注册配置 start=========================== -->
29
    <dubbo:service interface="com.ai.ipu.dubbo.provider.HelloService" ref="helloService" token="true"/>
30
    <bean id="helloService" class="com.ai.ipu.dubbo.provider.impl.HelloServiceImpl"/>
31
    <!-- 服务注册配置 end =========================== -->
32
</beans>

+ 24 - 0
dubbo-example/dubbo-service-provider/src/test/java/com/ai/ipu/dubbo/provider/HelloServiceTest.java

@ -0,0 +1,24 @@
1
package com.ai.ipu.dubbo.provider;
2

3
import junit.framework.TestCase;
4

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

7
public class HelloServiceTest extends TestCase {
8
    private ClassPathXmlApplicationContext context;
9

10
    @Override
11
    protected void setUp() throws Exception {
12
        // TODO Auto-generated method stub
13
        context = new ClassPathXmlApplicationContext(new String[] { DubboServiceProviderStart.DUBBO_CONSUMER_CONFIG });
14
        context.start();
15
    }
16
    
17

18
    public void testHello() {
19
        // TODO Auto-generated method stub
20
        HelloService helloService = (HelloService)context.getBean("helloService"); // 获取远程服务代理
21
        String result = helloService.hello("测试名称"); // 执行远程方法
22
        System.out.println(result); // 显示调用结果
23
    }
24
}

+ 19 - 0
dubbo-example/dubbo-service-provider/src/test/resources/dubbo-consumer-simple.xml

@ -0,0 +1,19 @@
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-service-demo" />
10
	<!-- 关闭重试,延长超时 -->
11
	<dubbo:consumer retries="0" timeout="2000"/>	<!-- client="netty4" -->
12
	<dubbo:registry address="127.0.0.1:20880" />
13
	<!-- 全局配置 end=========================== -->
14

15
	<!-- 服务注册配置 start=========================== -->
16
	<dubbo:reference id="helloService"
17
		interface="com.ai.ipu.dubbo.provider.HelloService" />
18
	<!-- 服务注册配置 end =========================== -->
19
</beans>

+ 35 - 0
dubbo-example/pom.xml

@ -0,0 +1,35 @@
1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
	<modelVersion>4.0.0</modelVersion>
4
	<parent>
5
		<groupId>com.ai.ipu.server</groupId>
6
		<artifactId>ipu-service-libs</artifactId>
7
		<version>3.1-SNAPSHOT</version>
8
	</parent>
9
	
10
	<groupId>com.ai.ipu.example</groupId>
11
	<artifactId>dubbo-example</artifactId>
12
	<version>1.0-SNAPSHOT</version>
13
	<packaging>pom</packaging>
14
	
15
	<name>dubbo-example</name>
16
	<description>dubbo范例</description>
17
	<modules>
18
		<module>dubbo-service-provider</module>
19
	</modules>
20
	
21
	<repositories>
22
		<repository>
23
			<id>ipu</id>
24
			<name>ipu repository</name>
25
			<url>http://114.215.100.48:9090/nexus/content/groups/public/</url>
26
			<releases>
27
				<enabled>true</enabled>
28
			</releases>
29
			<snapshots>
30
				<enabled>true</enabled>
31
				<updatePolicy>always</updatePolicy>
32
			</snapshots>
33
		</repository>
34
	</repositories>
35
</project>