dubbo是目前很是流行的分布式服务技术,不少公司都在用。空闲之余,搭了个helloworld,分享给你们。
本文demo下载
1.下载 zookeeper
zookeeper是服务的注册中心,下载后进入到安装目录G:bakCenterzookeeper-3.3.6bin。
双击zkServer.cmd便可启动注册中心服务。
zkServer.sh【Linux】或zkServer.cmd【Windows】
Zookeeper的配置文件在 conf 目录下,这个目录下有 zoo_sample.cfg 和 log4j.properties,须要将zoo_sample.cfg 更名为 zoo.cfg,Zookeeper在启动时会找这个文件做为默认配置文件html
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. dataDir=/tmp/zookeeper # the port at which the clients will connect clientPort=2181
配置说明能够参考这里
2.服务提供者
定义服务接口:java
package com.hy.service; public interface DemoService { String sayHello(String name); }
实现服务接口:git
package com.hy.service.impl; import com.hy.service.DemoService; public class DemoServiceImpl implements DemoService { public String sayHello(String name) { System.out.println("init : " + name); return "hello " + name; } }
在Spring配置中注册服务。github
<?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="dubbo-demo" /> <!-- zookeeper注册中心 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <dubbo:protocol name="dubbo" port="20880" /> <!-- 和本地bean同样实现服务 --> <bean id="demoService" class="com.hy.service.impl.DemoServiceImpl" /> <!-- 向注册中心注册暴漏服务地址,注册服务 --> <dubbo:service interface="com.hy.service.DemoService" ref="demoService" executes="10" /> </beans>
执行服务提供方主方法启用服务:web
package main; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ProviderMain { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationProvider.xml" }); context.start(); System.out.println("服务注册成功.."); System.in.read(); context.close(); } }
3.dubbo监控中心查看服务下载地址
下载后,将dubbo-admin-2.5.4-SNAPSHOT.war包放置tomcat服务器的webapps目录,启动tomcat服务器,打开下面连接便可查看可用的服务及其状态 http://localhost:8080/dubbo-a...
用户名和密码配置在C:Program FilesApache Software FoundationTomcat 7.0webappsdubbo-admin-2.5.4-SNAPSHOTWEB-INFdubbo.properties文件中spring
dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.admin.root.password=root dubbo.admin.guest.password=guest
4.服务消费者
经过Spring配置订阅服务提供方暴露的服务apache
<?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-of-dubbo-demo" /> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 向注册中心订阅服务 --> <dubbo:reference id="demoService" interface="com.hy.service.DemoService" /> </beans>
执行服务消费方主方法启用服务:tomcat
package main; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.hy.service.DemoService; public class ConsumerMain { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationConsumer.xml" }); context.start(); DemoService service = (DemoService) context.getBean("demoService"); System.out.println(service.sayHello("world")); context.close(); } }
在控制台便可见服务提供方方法的调用结果。服务器