初识Dubbo 系列之3-Dubbo 快速启动
快速启动
Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。 |
1
如果不想使用Spring配置,而希望通过API的方式进行调用(不推荐),请参见:API配置 (+) |
1
链接地址服务提供者
(#)
完整安装步骤,请参见:示例提供者安装 (+) |
1
定义服务接口: (该接口需单独打包,在服务提供方和消费方共享)
DemoService.java
1package 2 com.alibaba.dubbo.demo; 3 4 5 6 7 8 9 10 11 12 13 14 public 15 interface 16 DemoService { 17 18 19 20 21 22 23 24 25 26 27 28 29 String sayHello(String name); 30 31 32 33 34 35 36 37 38 39 40 41 } 42
在服务提供方实现接口:(对服务消费方隐藏实现)
DemoServiceImpl.java
1package 2 com.alibaba.dubbo.demo.provider; 3 4 5 6 7 8 9 10 11 12 13 14 import 15 com.alibaba.dubbo.demo.DemoService; 16 17 18 19 20 21 22 23 24 25 26 27 public 28 class 29 DemoServiceImpl 30 implements 31 DemoService { 32 33 34 35 36 37 38 39 40 41 42 43 44 public 45 String sayHello(String name) { 46 47 48 49 50 51 52 return 53 "Hello " 54 + name; 55 56 57 58 59 60 61 } 62 63 64 65 66 67 68 69 70 71 72 73 } 74
用Spring配置声明暴露服务:
provider.xml
1<? 2 xml 3 version 4 = 5 "1.0" 6 encoding 7 = 8 "UTF-8" 9 ?> 10 11 12 13 14 15 < 16 beans 17 xmlns 18 = 19 "http://www.springframework.org/schema/beans" 20 21 22 23 24 25 26 xmlns:xsi 27 = 28 "http://www.w3.org/2001/XMLSchema-instance" 29 30 31 32 33 34 35 xmlns:dubbo 36 = 37 "http://code.alibabatech.com/schema/dubbo" 38 39 40 41 42 43 44 xsi:schemaLocation 45 = 46 "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" 47 > 48 49 50 51 52 53 54 55 56 57 58 59 60 <!-- 提供方应用信息,用于计算依赖关系 --> 61 62 63 64 65 66 67 < 68 dubbo:application 69 name 70 = 71 "hello-world-app" 72 /> 73 74 75 76 77 78 79 80 81 82 83 84 85 <!-- 使用multicast广播注册中心暴露服务地址 --> 86 87 88 89 90 91 92 < 93 dubbo:registry 94 address 95 = 96 "multicast://224.5.6.7:1234" 97 /> 98 99 100 101 102 103 104 105 106 107 108 109 110 <!-- 用dubbo协议在20880端口暴露服务 --> 111 112 113 114 115 116 117 < 118 dubbo:protocol 119 name 120 = 121 "dubbo" 122 port 123 = 124 "20880" 125 /> 126 127 128 129 130 131 132 133 134 135 136 137 138 <!-- 声明需要暴露的服务接口 --> 139 140 141 142 143 144 145 < 146 dubbo:service 147 interface 148 = 149 "com.alibaba.dubbo.demo.DemoService" 150 ref 151 = 152 "demoService" 153 /> 154 155 156 157 158 159 160 161 162 163 164 165 166 <!-- 和本地bean一样实现服务 --> 167 168 169 170 171 172 173 < 174 bean 175 id 176 = 177 "demoService" 178 class 179 = 180 "com.alibaba.dubbo.demo.provider.DemoServiceImpl" 181 /> 182 183 184 185 186 187 188 189 190 191 192 193 </ 194 beans 195 > 196
加载Spring配置:
Provider.java
1import 2 org.springframework.context.support.ClassPathXmlApplicationContext; 3 4 5 6 7 8 9 10 11 12 13 14 public 15 class 16 Provider { 17 18 19 20 21 22 23 24 25 26 27 28 29 public 30 static 31 void 32 main(String[] args) 33 throws 34 Exception { 35 36 37 38 39 40 41 ClassPathXmlApplicationContext context = 42 new 43 ClassPathXmlApplicationContext( 44 new 45 String[] { 46 "http://10.20.160.198/wiki/display/dubbo/provider.xml" 47 }); 48 49 50 51 52 53 54 context.start(); 55 56 57 58 59 60 61 62 63 64 65 66 67 System.in.read(); 68 // 按任意键退出 69 70 71 72 73 74 75 } 76 77 78 79 80 81 82 83 84 85 86 87 } 88
链接地址服务消费者
(#)
完整安装步骤,请参见:示例消费者安装 (+) |
1
通过Spring配置引用远程服务:
consumer.xml
1<? 2 xml 3 version 4 = 5 "1.0" 6 encoding 7 = 8 "UTF-8" 9 ?> 10 11 12 13 14 15 < 16 beans 17 xmlns 18 = 19 "http://www.springframework.org/schema/beans" 20 21 22 23 24 25 26 xmlns:xsi 27 = 28 "http://www.w3.org/2001/XMLSchema-instance" 29 30 31 32 33 34 35 xmlns:dubbo 36 = 37 "http://code.alibabatech.com/schema/dubbo" 38 39 40 41 42 43 44 xsi:schemaLocation 45 = 46 "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" 47 > 48 49 50 51 52 53 54 55 56 57 58 59 60 <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 --> 61 62 63 64 65 66 67 < 68 dubbo:application 69 name 70 = 71 "consumer-of-helloworld-app" 72 /> 73 74 75 76 77 78 79 80 81 82 83 84 85 <!-- 使用multicast广播注册中心暴露发现服务地址 --> 86 87 88 89 90 91 92 < 93 dubbo:registry 94 address 95 = 96 "multicast://224.5.6.7:1234" 97 /> 98 99 100 101 102 103 104 105 106 107 108 109 110 <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> 111 112 113 114 115 116 117 < 118 dubbo:reference 119 id 120 = 121 "demoService" 122 interface 123 = 124 "com.alibaba.dubbo.demo.DemoService" 125 /> 126 127 128 129 130 131 132 133 134 135 136 137 </ 138 beans 139 > 140
加载Spring配置,并调用远程服务:(也可以使用IoC注入)
Consumer.java
1import 2 org.springframework.context.support.ClassPathXmlApplicationContext; 3 4 5 6 7 8 import 9 com.alibaba.dubbo.demo.DemoService; 10 11 12 13 14 15 16 17 18 19 20 21 public 22 class 23 Consumer { 24 25 26 27 28 29 30 31 32 33 34 35 36 public 37 static 38 void 39 main(String[] args) 40 throws 41 Exception { 42 43 44 45 46 47 48 ClassPathXmlApplicationContext context = 49 new 50 ClassPathXmlApplicationContext( 51 new 52 String[] { 53 "http://10.20.160.198/wiki/display/dubbo/consumer.xml" 54 }); 55 56 57 58 59 60 61 context.start(); 62 63 64 65 66 67 68 69 70 71 72 73 74 DemoService demoService = (DemoService)context.getBean( 75 "demoService" 76 ); 77 // 获取远程服务代理 78 79 80 81 82 83 84 String hello = demoService.sayHello( 85 "world" 86 ); 87 // 执行远程方法 88 89 90 91 92 93 94 95 96 97 98 99 100 System.out.println( hello ); 101 // 显示调用结果 102 103 104 105 106 107 108 } 109 110 111 112 113 114 115 116 117 118 119 120 } 121
完