开源分散式服务框架 Dubbo( 二 )


可视化的服务治理与运维提供丰富服务治理、运维工具:随时查询服务元数据、服务健康状态及调用统计,实时下发路由策略、调整配置参数 。
例子服务端定义一个Service Interface:(HelloService.java)package com.alibaba.hello.api;public interface HelloService{  String sayHello(String name);}接口的实现类:(HelloServiceImpl.java)package com.alibaba.hello.impl;import com.alibaba.hello.api.HelloService;public  class  HelloServiceImpl  implements  HelloService{    public  String  sayHello(String  name){        return  "Hello" + name;    }}Spring配置:(provider.xml)<?xmlversion="1.0"encoding="UTF-8"?><beans......>    <!--Applicationname-->    <dubbo:applicationname="hello-world-app"/>    <!--registryaddress,usedforservicetoregisteritself-->    <dubbo:registryaddress="multicast://224.5.6.7:1234"/>    <!--exposethisservicethroughdubboprotocol,throughport20880-->    <dubbo:protocolname="dubbo"port="20880"/>    <!--whichserviceinterfacedoweexpose?-->    <dubbo:serviceinterface="com.alibaba.hello.api.HelloService"ref="helloService"/>    <!--designateimplementation-->    <beanid="helloService"class="com.alibaba.hello.impl.HelloServiceImpl"/></beans>测试代码:(Provider.java)importorg.springframework.context.support.ClassPathXmlApplicationContext;public  class  Provider{    public  static  void  main(String[]  args){        ClassPathXmlApplicationContext  context = new  ClassPathXmlApplicationContext(newString[]{"provider.xml"});        //启动成功,监听连线埠为20880System.in.read();//按任意键退出    }}客户端Spring配置档案:(consumer.xml)<?xmlversion="1.0"encoding="UTF-8"?><beans xmlns=......>    <!--consumerapplicationname-->    <dubbo:applicationname="consumer-of-helloworld-app"/>    <!--registryaddress,usedforconsumertodiscoverservices-->    <dubbo:registryaddress="multicast://224.5.6.7:1234"/>    <!--whichservicetoconsume?-->    <dubbo:referenceid="helloService"interface="com.alibaba.hello.api.HelloService"/></beans>客户端测试代码:(Consumer.java)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(newString[]{"consumer.xml"});        HelloService  helloService = (HelloService)context.getBean("helloService");        //getserviceinvocationproxyStringhello=helloService.sayHello("world");        //doinvoke!System.out.println(hello);        //cool,howareyou~    }}