1.先建一个项目
pom.xml文件
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 <groupId>com.guilf</groupId>
5 <artifactId>com-guilf-provider-service</artifactId>
6 <version>0.0.1-SNAPSHOT</version>
7
8 <properties>
9 <slf4j.version>1.6.6</slf4j.version>
10 <log4j.version>1.2.9</log4j.version>
11 </properties>
12
13 <dependencies>
14 <dependency>
15 <groupId>com.guilf</groupId>
16 <artifactId>com.guilf.api</artifactId>
17 <version>0.0.1-SNAPSHOT</version>
18 </dependency>
19 <!-- dubbo所需要的包 -->
20 <dependency>
21 <groupId>com.alibaba</groupId>
22 <artifactId>dubbo</artifactId>
23 <version>2.5.3</version>
24 </dependency>
25
26 <dependency>
27 <groupId>org.apache.zookeeper</groupId>
28 <artifactId>zookeeper</artifactId>
29 <version>3.4.5</version>
30 </dependency>
31
32 <dependency>
33 <groupId>com.github.sgroschupf</groupId>
34 <artifactId>zkclient</artifactId>
35 <version>0.1</version>
36 </dependency>
37
38 <!-- log start -->
39 <dependency>
40 <groupId>log4j</groupId>
41 <artifactId>log4j</artifactId>
42 <version>${log4j.version}</version>
43 </dependency>
44 <dependency>
45 <groupId>org.slf4j</groupId>
46 <artifactId>slf4j-api</artifactId>
47 <version>${slf4j.version}</version>
48 </dependency>
49 <dependency>
50 <groupId>org.slf4j</groupId>
51 <artifactId>slf4j-log4j12</artifactId>
52 <version>${slf4j.version}</version>
53 </dependency>
54 </dependencies>
55 <!-- maven打包用的 -->
56 <build>
57 <plugins>
58 <plugin>
59 <groupId>org.apache.maven.plugins</groupId>
60 <artifactId>maven-compiler-plugin</artifactId>
61 <configuration>
62 <source>1.5</source>
63 <target>1.5</target>
64 </configuration>
65 </plugin>
66
67 <plugin>
68 <groupId>org.apache.maven.plugins</groupId>
69 <artifactId>maven-source-plugin</artifactId>
70 <version>2.1.2</version>
71 <executions>
72 <execution>
73 <id>attach-sources</id>
74 <phase>verify</phase>
75 <goals>
76 <goal>jar-no-fork</goal>
77 </goals>
78 </execution>
79 </executions>
80 </plugin>
81
82 <plugin>
83 <groupId>org.apache.maven.plugins</groupId>
84 <artifactId>maven-shade-plugin</artifactId>
85 <version>2.3</version>
86 <executions>
87 <execution>
88 <phase>package</phase>
89 <goals>
90 <goal>shade</goal>
91 </goals>
92 <configuration>
93 <transformers>
94 <transformer
95 implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
96 <mainClass>com.guilf.service.impl.TestMain</mainClass>
97 </transformer>
98 <transformer
99 implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
100 <resource>META-INF/spring.handlers</resource>
101 </transformer>
102 <transformer
103 implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
104 <resource>META-INF/spring.schemas</resource>
105 </transformer>
106 </transformers>
107 </configuration>
108 </execution>
109 </executions>
110 </plugin>
111 </plugins>
112 </build>
113
114 </project>
dubbo-provider.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->
<dubbo:application name="dubbo_provider"/>
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.88.134:2181?backup=192.168.88.134:2182,192.168.88.134:2183" check="false"/>
<!-- 要暴露的服务接口 -->
<dubbo:service interface="com.guilf.service.api.GuilfDubboServiceApi" ref="guilfDubboServiceApi"/>
<!--具体实现该接口的 bean-->
<bean id="guilfDubboServiceApi" class="com.guilf.service.impl.GuilfDubboServiceImpl"/>
</beans>
GuilfDubboServiceImpl.java
package com.guilf.service.impl;
import com.guilf.service.api.GuilfDubboServiceApi;
public class GuilfDubboServiceImpl implements GuilfDubboServiceApi
{
public String findUser(String name, String password) {
return "======name:"+name+"=====password:"+password;
}
}
GuilfDubboServiceApi.java
package com.guilf.service.api;
public interface GuilfDubboServiceApi
{
public String findUser(String name, String password);
}
TestMain.java
package com.guilf.service.impl;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestMain {
public static void main(String[] args) {
// 加载dubbo-provider.xml,
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"classpath:conf/dubbo-provider.xml"});
// 启动
context.start();
System.out.println("Press any key to exit.");
try {
// 按任意键退出
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
启动