目錄java
標籤(空格分隔): 分佈式git
dubbo是目前國內比較流行的一種分佈式服務治理方案。還有一種就是esb了。通常採用的是基於Apache servicemix 和 Apache Camel和activemq這種方式。這裏先介紹一下dubbo的相關。
dubbo工程通常分爲3個module.一個服務者,一個消費者,一個接口。其中服務者和消費者都依賴於接口文件。工程圖相似以下github
+wordspace |-接口 |-服務者 |-消費者
新建三個maven項目,其中接口/服務者爲普通java項目,之後打成jar包,採用java -jar命令運行。web
public interface OrderInterface { void add(String message); void add(Object o); void orderHasCase(); }
public class OrderServiceImpl implements OrderInterface { public void add(String s) { System.out.println(s); } public void orderHasCase() { } public void add(Object o) { System.out.println("objcect:" + o); } }
在服務者裏添加spring的配置文件,及ip信息等。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: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="orderService" class="com.yks.service.OrderServiceImpl"/> <!-- 提供方應用信息,用於計算依賴關係 --> <dubbo:application name="oms_provider"/> <!-- 使用multicast廣播註冊中心暴露服務地址 --> <!--<dubbo:registry address="multicast://224.1.1.1:1234" />--> <!-- 使用zookeeper註冊中心暴露服務地址 --> <dubbo:registry address="zookeeper://192.168.3.15:2181"/> <!-- 用dubbo協議在20880端口暴露服務 --> <dubbo:protocol name="dubbo" port="20880"/> <!-- 聲明須要暴露的服務接口 --> <dubbo:service interface="com.yks.oms.OrderInterface" ref="orderService"/> </beans>
public class TestMain { public static void main(String[] args) { ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("customer.xml"); classPathXmlApplicationContext.start(); OrderInterface orderService = (OrderInterface) classPathXmlApplicationContext.getBean("orderService"); String message = "test"; orderService.add(message); } }
消費者配置文數據庫
<?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 "> <!-- consumer application name --> <dubbo:application name="consumer-of-helloworld-app"/> <!-- registry address, used for consumer to discover services --> <!--<dubbo:registry address="multicast://224.1.1.1:1234" />--> <dubbo:registry address="zookeeper://192.168.3.15:2181"/> <dubbo:consumer timeout="5000"/> <!-- which service to consume? --> <dubbo:reference id="orderService" interface="com.yks.oms.OrderInterface"/> </beans>
啓動zk sh bin/zkServer.sh start 查看ZK服務狀態: sh bin/zkServer.sh status 中止ZK服務: sh bin/zkServer.sh stop 重啓ZK服務: sh bin/zkServer.sh restart