2017年2月20日Dubbo遠程調用服務框架部署

Registry能夠用ZooKeeper實現java

在Monitor裏面安裝Dubbo-monitorspring

在Provider端引入dubbo的依賴,使用spring配置註冊中心和監控中心,並註冊服務api

<?xml version="1.0" encoding="UTF-8"?>
<beans ......>
  <!-- Application name -->
  <dubbo:application name="hello-world-app" />
  <!-- registry address, used for service to register itself -->
  <dubbo:registry address="multicast://224.5.6.7:1234" />
  <!-- expose this service through dubbo protocol, through port 2 0 8 8 0 -->
  <dubbo:protocol name="dubbo" port="2 0 8 8 0" />
  <!-- which service interface do we expose? -->
  <dubbo:service interface="com.alibaba.hello.api.HelloService" ref="helloService" />
  <!-- designate implementation -->
  <bean id="helloService" class="com.alibaba.hello.impl.HelloServiceImpl" />
</beans>

而後用一個包含main函數的類啓動這個配置文件app

import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) {
  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"}); //啓動成功,監聽端口爲2 0 8 8 0
  System.in.read(); // 按任意鍵退出
  }
}

在Consumer端引入dubbo的依賴,使用spring配置註冊中心和監控中心ide

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=......>
<!-- consumer application name -->
<dubbo:application name="consumer-of-helloworld-app" />
<!-- registry address, used for consumer to discover services -->
<dubbo:registry address="multicast://224.5.6.7:1234" />
<!-- which service to consume? -->
<dubbo:reference id="helloService" interface="com.alibaba.hello.api.HelloService" />
</beans>

最後在客戶端消費服務函數

import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.hello.api.HelloService;
public class Consumer {
  public static void main(String[] args) {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"});
    HelloService helloService = (HelloService)context.getBean("helloService"); // get service invocation proxy
    String hello = helloService.sayHello("world"); // do invoke!
    System.out.println( hello ); // cool, how are you~
  }
}
相關文章
相關標籤/搜索