目录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