Dubbo_創建Dubbo服務並在ZooKeeper註冊,然後通過Jar包執行【轉】

原文地址

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
package dub.service.demo;
  
import java.net.InetAddress;
import java.net.UnknownHostException;
  
/**
 * Created by JamesC on 16-10-8.
 */
public class DemoService implements IDemoService {
  
    public String sayHello(String name) {
        String msg = "歡迎您:" + name + " IP地址:" + getIP();
        System.out.println(msg);
        return msg;
    }
  
  
    private String getIP() {
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        String ip = addr.getHostAddress();//獲得本機IP
        String address = addr.getHostName();//獲得本機名稱
        return ip;
    }
}


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
<?xml version="1.0" encoding="UTF-8"?>
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
 
 
    <context:annotation-config/>
 
    <!-- 配置要掃描的包 -->
    <context:component-scan base-package="dub.service.demo"/>
 
    <!-- 提供方應用信息,用於計算依賴關係 -->
    <dubbo:application name="dubbo-service-demo"/>
 
    <!-- 使用zookeeper註冊中心暴露服務地址 -->
    <dubbo:registry protocol="zookeeper" address="zookeeper://192.168.146.130:2181"/>
 
    <!-- 用dubbo協議在20880端口暴露服務 -->
    <dubbo:protocol name="dubbo" port="20880"/>
 
    <!-- 用戶服務接口 -->
    <dubbo:service interface="dub.service.demo.IDemoService" ref="demoService" />
 
    <bean id="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
    <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>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Snapshot Repository</name>
        </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就算顯示啓動成功, 實際上也沒有啓動


發佈了181 篇原創文章 · 獲贊 21 · 訪問量 38萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章