由于喜欢尝鲜,于是在Java9出来以后就马上把jdk换成了jdk1.9,结果最近刚学dubbo的时候一直都配置不成功,于是退回jdk1.8。一下就是配置dubbo的过程
安装Zookeeper
- 下载Zookeeper,然后把conf目录下的
zoo.sample.cfg
改为zoo.cfg
- 修改
zoo.cfg
,将dataDir修改为指定目录
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/eros/data/zookeeper
clientPort=2181
maxClientCnxns=60
安装tomcat
下载dubbo-admin包
如果是使用jdk8的话,可以在网上下载别人已经修改好的包,jdk1.8-dubbo-admin
下载完成后将war包放在tomcat的webapps目录下
启动Zookeeper服务器
sudo ./zkServer.sh start
启动tomcat
sudo ./startup.sh
访问http://localhost:8080/
观察是否启动成功
在浏览器输入http://localhost:8080/dubbo-admin-2.5.4/
,用户名和密码都是root
在Common模块中定义所有要暴露的接口
public interface IService {
int calculate(int x,int y);
}
启动Server
dubbo.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">
<bean id="providerImpl" class="org.eros.dubbo.server.service.ProviderServiceImpl"/>
<!-- 用dubbo协议在19091端口暴露服务 -->
<dubbo:protocol name="dubbo" port="19091"/>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="ProviderService"/>
<dubbo:service interface="org.eros.dubbo.common.entity.IService" ref="providerImpl" timeout="100000"/>
</beans>
Spring配置类
@Configuration
@ComponentScan
@ImportResource("classpath:dubbo.xml")
@EnableAspectJAutoProxy
public class Configure {
}
接口的具体实现
public class ProviderServiceImpl implements IService{
@Override
public int calculate(int x, int y) {
return x+y;
}
}
import java.io.IOException;
public class Main {
private static final Logger logger= LogManager.getLogger(Main.class);
public static void main(String[] args) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(Configure.class);
context.start();
logger.debug("提供者服务已注册成功!");
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
查看控制台
启动客户端
dubbo.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:application name="consumer"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:reference interface="org.eros.dubbo.common.entity.IService" id="providerService" timeout="100000" check="false"/>
</beans>
Spring配置文件
@Configuration
@ComponentScan
@ImportResource("classpath:dubbo.xml")
public class Configure {
}
public class Main {
private static final Logger logger= LogManager.getLogger(Main.class);
public static void main(String[] args) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(Configure.class);
context.start();
IService providerService= (IService) context.getBean("providerService");
int result=providerService.calculate(3,7);
logger.debug("result is "+result);
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
查看控制台
附:
Server.pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>DubboDemo</artifactId>
&