相同的步骤创建服务提供者
=====================================================================
创建公共接口,就这一个功能
/**
- 接口:定义相关的行为
*/
public interface UserService {
public String sayHello(String msg);
}
1.添加依赖
服务提供者需要依赖dubbo,zookeeper,spring和commons等,具体如下:
com.dpb
dubbo-commos
1.0-SNAPSHOT
org.springframework
spring-context
4.3.21.RELEASE
org.slf4j
slf4j-log4j12
1.7.25
com.alibaba
dubbo
2.5.3
spring
org.springframework
com.github.sgroschupf
zkclient
0.1
2.接口实现
引入了commons模块的依赖,我们就可以创建公共接口的实现类了,具体如下:
/**
-
@program: dubbo-parent
-
@description: 公共接口的实现类
-
@author: 波波烤鸭
-
@create: 2019-05-13 20:34
*/
public class UserServiceImpl implements UserService {
@Override
public String sayHello(String msg) {
System.out.println(“服务端接收:”+msg);
return “你好啊”;
}
}
3.相关配置
创建spring的配置文件,如下:
<?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:context=“http://www.springframework.org/schema/context”
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://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name=“dubboProvider” />
<dubbo:registry protocol=“zookeeper”
address=“zk00:2181,zk01:2181,zk02:2181” />
<dubbo:protocol name=“dubbo” port=“20880” />
<dubbo:monitor protocol=“registry” />
<dubbo:service interface=“com.dpb.service.UserService” ref=“userService”
group=“dubbo” version=“1.0.0” timeout=“3000”/>
4.添加日志文件
添加一个log4j.properties文件,内容如下:
log4j.rootLogger=INFO,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
5.启动服务
创建启动程序,如下:
/**
-
@program: dubbo-parent
-
@description: 主方法
-
@author: 波波烤鸭
-
@create: 2019-05-13 20:39
*/
public class App {
public static void main(String[] args) throws Exception{
ApplicationContext ac = new ClassPathXmlApplicationContext(“applicationContext.xml”);
//挂起当前线程,如果没有改行代码,服务提供者进程会消亡,服务消费者就发现不了提供者了
Thread.currentThread().join();
}
}
完成目录结构
启动程序
服务端启动成功~
zookeeper中也可以查看到相关信息
1.添加相关依赖
依赖和provider差不多,具体如下:
com.dpb
dubbo-commos
1.0-SNAPSHOT
org.springframework
spring-context
4.3.21.RELEASE
org.slf4j
slf4j-log4j12
1.7.25
com.alibaba
dubbo
2.5.3
spring
org.springframework
com.github.sgroschupf
zkclient
0.1
2.配置配置文件
添加spring的配置文件,配置dubbo消费者的相关信息
<beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xmlns:context=“http://www.springframework.org/schema/context”
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://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name=“dubboConsumer” />
<dubbo:registry protocol=“zookeeper”
address=“zk00:2181,zk01:2181,zk02:2181” />
<dubbo:monitor protocol=“registry” />
<dubbo:reference id=“userService” interface=“com.dpb.service.UserService”
group=“dubbo” version=“1.0.0” timeout=“3000”/>
3.添加日志文件
添加一个log4j.properties文件,内容如下:
log4j.rootLogger=INFO,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout