虽然dubbo 已经不更新了,A公司已一直在用HSF,潮流追随者也在使用spring cloud,最近须要用dubbo,发一贴dubbo Demo.html
1. 安装zookeeper ,前面的博中已有介绍,略去。spring
2. 建服务端apache
/** * Created by on 2017/7/26. */ public interface CenterService { List<String> getPermissions(Long id); }
/** * Created by on 2017/7/26. */ public class CenterServiceImpl implements CenterService { public List<String> getPermissions(Long id) { List<String> demo = new ArrayList<String>(); demo.add(String.format("The left Id is :%d", id - 1)); demo.add(String.format("This Id is :%d", id)); demo.add(String.format("The next Id is:%d", id + 1)); return demo; }
3. providerapi
/** * Created by on 2017/7/26. */ public class DubboProvider { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo-provider.xml"); System.out.println(context.getDisplayName() + ": here"); context.start(); System.out.println("服务已经启动..."); System.in.read(); }
spring 配置文件 dubbo-provider.xmltomcat
<?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-admin 或 dubbo-monitor 会显示这个名字,方便辨识--> <dubbo:application name="demotest-provider" owner="programmer" organization="dubbox"/> <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper--> <dubbo:registry address="zookeeper://localhost:2181"/> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!--使用 dubbo 协议实现定义好的 api.CenterService 接口--> <dubbo:service interface="com.dennis.dubbo.provider.CenterService" ref="centerService" protocol="dubbo" /> <!--具体实现该接口的 bean--> <bean id="centerService" class="com.dennis.dubbo.provider.impl.CenterServiceImpl"/> </beans>
3. 建客户端app
/**
* Created by on 2017/7/26.
*/
public class ConsumerService {
public static void main(String[] args) {
//测试常规服务
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("dubbo-consumer.xml");
context.start();
System.out.println("consumer start");
// DemoServiceImpl de=new DemoServiceImpl();
CenterService centerService = context.getBean(CenterService.class);
System.out.println("consumer");
System.out.println(centerService.getPermissions(1L));
}
}
spring 配置 dubbo-consumer.xmlide
<?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="demotest-consumer" owner="programmer" organization="dubbox"/> <!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送--> <dubbo:registry address="zookeeper://localhost:2181"/> <!--使用 dubbo 协议调用定义好的 api.CenterService 接口--> <dubbo:reference id="centerService" interface="com.dennis.dubbo.provider.CenterService" /> </beans>
客户端 没有CenterService 会报错的,我在客户端建了一个同名接口测试
/** * Created by on 2017/7/26. */ public interface CenterService { List<String> getPermissions(Long id); }
4.启动zookeeperspa
启动服务端:code
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
org.springframework.context.support.ClassPathXmlApplicationContext@3fa77460: here
服务已经启动...
启动客户端:
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
consumer start
consumer
[The left Id is :0, This Id is :1, The next Id is:2]
5.验证
时间有限 ,没有在tomcat下加 duboo-admin
打开zookeeper的log文件, 里面对provider consumer进行了计数,应该是OK的。
随便一搞,若有不正确的地方请你们批评指正。