Dubbo_创建Dubbo服务并在ZooKeeper注册,然后通过Jar包执行【转】

来源:互联网 发布:华为交换机 端口详解 编辑:程序博客网 时间:2024/06/06 16:34

原文地址

http://www.cnblogs.com/gossip/p/5945848.html

原文

1、DemoService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
packagedub.service.demo;
  
importjava.net.InetAddress;
importjava.net.UnknownHostException;
  
/**
 * Created by JamesC on 16-10-8.
 */
publicclass DemoService implements IDemoService {
  
    publicString sayHello(String name) {
        String msg ="欢迎您:" + name + " IP地址:" + getIP();
        System.out.println(msg);
        returnmsg;
    }
  
  
    privateString getIP() {
        InetAddress addr =null;
        try{
            addr = InetAddress.getLocalHost();
        }catch (UnknownHostException e) {
            e.printStackTrace();
        }
        String ip = addr.getHostAddress();//获得本机IP
        String address = addr.getHostName();//获得本机名称
        returnip;
    }
}


2、dubbo-provider.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.2.xsd
            http://code.alibabatech.com/schema/dubbo 
            http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 
 
    <context:annotation-config/>
 
    <!-- 配置要扫描的包 -->
    <context:component-scanbase-package="dub.service.demo"/>
 
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:applicationname="dubbo-service-demo"/>
 
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registryprotocol="zookeeper"address="zookeeper://192.168.146.130:2181"/>
 
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocolname="dubbo"port="20880"/>
 
    <!-- 用户服务接口 -->
    <dubbo:serviceinterface="dub.service.demo.IDemoService"ref="demoService"/>
 
    <beanid="demoService"class="dub.service.demo.DemoService"/>
 
</beans>

3、pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>dub-service-demo</groupId>
    <artifactId>dub-service-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
 
    <name>dub-service-demo</name>
    <url>http://maven.apache.org</url>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
 
    <distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://192.168.146.129:8081/nexus/content/repositories/releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://192.168.146.129:8081/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
 
    <dependencies>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.0.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.0.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.0.6.RELEASE</version>
        </dependency>
 
 
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
        </dependency>
 
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.3</version>
        </dependency>
 
 
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>
    </dependencies>
 
    <build>
        <!--打包生成的jar包文件名-->
        <finalName>dub-service-demo</finalName>
 
        <resources>
            <resource>
                <targetPath>${project.build.directory}/classes</targetPath>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <!--<include>**/*.xml</include>-->
                    <!--<include>**/*.properties</include>-->
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <!-- 结合com.alibaba.dubbo.container.Main -->
            <resource>
                <!--配置文件拷贝的对象目录-->
                <targetPath>${project.build.directory}/classes/META-INF/spring</targetPath>
                <!--配置文件必须放在spring文件夹下,否则dubbo即使显示启动成功,实际上也没有启动成功-->
                <directory>src/main/resources/spring</directory>
                <filtering>true</filtering>
                <includes>
                    <!--  <include>spring-context.xml</include>-->
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
 
        <plugins>
            <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <classesDirectory>target/classes/</classesDirectory>
                    <archive>
                        <manifest>
                            <mainClass>com.alibaba.dubbo.container.Main</mainClass>
                            <!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
                            <useUniqueVersions>false</useUniqueVersions>
                            <addClasspath>true</addClasspath>
 
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>.</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <!--打包阶段拷贝依赖文件-->
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <type>jar</type>
                            <includeTypes>jar</includeTypes>
                           <!-- <useUniqueVersions>false</useUniqueVersions>-->
                            <!--打包依赖文件的输出路径-->
                            <outputDirectory>
                                ${project.build.directory}/lib
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
 
    </build>
 
</project>


三、打包Dubbo服务

1、使用maven进行打包,先运行clean  再运行install


四、启动Dubbo服务

1、把jar文件和lib文件夹拷贝到C盘DemoServiceJars目录下

2、命令行定位到C盘DemoServiceJars目录下,执行java -jar dub-service-demo.jar

3、ZooKeeper检测到该服务

4、Linux启动Dubbo的方式也是执行:java -jar dub-service-demo.jar&


五、注意事项

1、配置文件dubbo-provider.xml必须放在resource-->spring文件夹下,否则dubbo就算显示启动成功, 实际上也没有启动


阅读全文
0 0
原创粉丝点击