1.单机安装zooker插件
zooker 安装和IEDA插件
2.安装生成者
package com.unj.dubbotest.provider.impl;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
context.start();
System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
}
}
bean 配置文件:
<?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 -->
<bean id="demoService" class="com.unj.dubbotest.provider.impl.DemoServiceImpl" />
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="xs_provider" />
<!-- 使用multicast广播注册中心暴露服务地址 -->
<!--<dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<!-- 使用zookeeper注册中心暴露服务地址 -即zookeeper的所在服务器ip地址和端口号 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.unj.dubbotest.provider.DemoService"
ref="demoService" />
</beans>
3.安装消费者
package com.alibaba.dubbo.demo.pp;
import java.util.List;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.unj.dubbotest.provider.DemoService;
public class Consumer {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "application.xml" });
context.start();
DemoService demoService = (DemoService) context.getBean("demoService");
String hello = demoService.sayHello("hejingyuan");
System.out.println(hello);
List list = demoService.getUsers();
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
System.in.read();
}
}
相应的配置文件:
<?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="hjy_consumer" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
<dubbo:reference id="demoService"
interface="com.unj.dubbotest.provider.DemoService" />
</beans>
4.运行结果:
4.1 启动zk ,查看服务注册状态
在IEDA的插件中可以看到服务的注册状态。
启动provider:
4.3 启动customer
4.4 结果
3.注意问题:
设置IEDA插件的时,需要关闭Zk 服务器,否则IEDA无法响应。
参考博文:
https://blog.csdn.net/qq_42556955/article/details/81184254
https://blog.csdn.net/sinat_15946141/article/details/79591212
https://blog.csdn.net/hejingyuan6/article/details/47403299
4.下载代码: