一、安装ZooKeeper(略)

二、创建Dubbo服务

 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就算显示启动成功, 实际上也没有启动

2、源码请从群中下载

Dubbo_创建Dubbo服务并在ZooKeeper注册,然后通过Jar包执行的更多相关文章

  1. dubbo服务治理中间件,zookeeper注册中心

    对传统项目架构进行拆分: 集群概念: 面向服务分布式架构: 服务层提供被注册的对象需要实现序列化接口Serializable: 配置表现层和服务层: 依赖包: 服务层: <!-- 定义dubbo ...

  2. dubbo服务治理中间件,zookeeper注册中心 安装配置

    对传统项目架构进行拆分: 集群概念: 面向服务分布式架构: 服务层提供被注册的对象需要实现序列化接口Serializable: 配置表现层和服务层: 依赖包: 服务层: <!-- 定义dubbo ...

  3. 服务端使用Zookeeper注册服务地址,客户端从Zookeeper获取可用的服务地址。

    一个轻量级分布式RPC框架--NettyRpc - 阿凡卢 - 博客园 http://www.cnblogs.com/luxiaoxun/p/5272384.html 这个RPC框架使用的一些技术所解 ...

  4. dubbo 2.8.4(dubbox)的jar包制作【添加到maven本地仓库】

    1. 下载 网址:https://github.com/hongmoshui/dubbox 2.  解压zip文件 3.  用maven编译文件 如果没有配置全局maven,就直接使用cmd命令行[进 ...

  5. 监控与管理dubbo服务

    Dubbo是阿里多年前开源的一套服务治理框架,在众多互联网企业里应用广泛.本文介绍了一些如何监控与管理dubbo服务.使用的工具与<dubbox 的各种管理和监管>大致相同,本文更侧重于命 ...

  6. kettle_为子server创建carte服务

    原创作品.出自 "深蓝的blog" 博客,深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/46876783 把k ...

  7. dubbo的jmeter压测时jar包的热加载/动态加载

    在做dubbo的jmeter压测时,需要把jar包放入jmeter的lib/ext目录下,但是jmeter启动的时候会自动加载这个目录lib目录及lib/ext目录,这样启动后放入这些目录下的jar包 ...

  8. 关于IDEA的Maven打jar包springboot项目问题,打成可执行jar包,IDEA创建的maven项目和spring initializr项目

    Spring Initializr创建的项目 源文件地址 https://github.com/TaoPanfeng/maven-package 项目的创建步骤 进行打包 clear package ...

  9. 自定义API(Jar包)的创建与使用(简单实现案例)

    @ 目录 学习内容 1. 自定义工具类 2. 导出jar 3. 加载Jar包 4. 调用自定义的API方法 总结 学习内容 1. 自定义工具类 新建一个java项目,然后创建包和工具类StringUt ...

随机推荐

  1. 贪吃蛇(C++实现,VC6.0编译,使用了EasyX图形库)

    程序效果: 代码: //main.cpp 1 #include <iostream> #include<fstream> #include <graphics.h> ...

  2. web 小知识

    document.write和innerHTML的区别   document.write是直接写入到页面的内容流,如果在写之前没有调用document.open, 浏览器会自动调用open.每次写完关 ...

  3. SQL存储过程的调用及写法

    调用函数: public class SqlProcess { ; public DataSet ReturnSet = null; public SqlDataAdapter adapter = n ...

  4. java 22 - 6 多线程之线程调度和设置线程的优先级

    线程调度 假如我们的计算机只有一个 CPU,那么 CPU 在某一个时刻只能执行一条指令,线程只有得到 CPU时间片,也就是使用权,才可以执行指令. 那么Java是如何对线程进行调用的呢? 线程有两种调 ...

  5. luogu10125回文数[noip1999 Day1 T1]

    题目描述 若一个数(首位不为零)从左向右读与从右向左读都一样,我们就将其称之为回文数. 例如:给定一个10进制数56,将56加65(即把56从右向左读),得到121是一个回文数. 又如:对于10进制数 ...

  6. bzoj2748[HAOI2012]音量调节(背包问题的方案)

    Description 一个吉他手准备参加一场演出.他不喜欢在演出时始终使用同一个音量,所以他决定每一首歌之前他都要改变一次音量.在演出开始之前,他已经做好了一个列表,里面写着在每首歌开始之前他想要改 ...

  7. java 继承(下)

    1.抽象方法一定要写在抽象类中. 2.抽象类只在描述该事事务应该具备的东西. 3.抽象只能是抽象类和抽象方法. 4,抽象类没有任何抽象方法,这种类是不让创建对象. private  static

  8. css3 @font-face设置嵌入字体

    @font-face能够加载服务器端的字体文件,让浏览器端可以显示用户电脑里没有安装的字体

  9. How To Create an IE-Only Stylesheet

    https://css-tricks.com/how-to-create-an-ie-only-stylesheet/ https://css-tricks.com/snippets/css/css- ...

  10. Scrapy 爬虫

    Scrapy 爬虫 使用指南 完全教程   scrapy note command 全局命令: startproject :在 project_name 文件夹下创建一个名为 project_name ...