(三)發布Dubbo服務
阿新 • • 發佈:2018-08-07
nbsp 6.0 main 發布 static ast vid [] 成功
我們現在來學習下發布Dubbo服務,主要參考dubbo開發包裏的demo源碼;由淺入深的講解下這個小demo;
github地址:https://github.com/apache/incubator-dubbo/tree/dubbo-2.6.3
首先創建一個maven項目dubbo-demo-provider
pom.xml加入依賴:
1 <dependencies> 2 <dependency> 3 <groupId>com.alibaba</groupId> 4 <artifactId>dubbo</artifactId> 5 <version>2.6.0</version> 6 </dependency> 7 <dependency> 8 <groupId>com.101tec</groupId> 9 <artifactId>zkclient</artifactId> 10 <version>0.10</version> 11 </dependency> 12 <dependency> 13 <groupId>org.apache.curator</groupId> 14 <artifactId>curator-framework</artifactId> 15 <version>4.0.1</version> 16 </dependency> 17 <dependency> 18 <groupId>com.alibaba</groupId> 19 <artifactId>fastjson</artifactId> 20 <version>1.2.46</version> 21 </dependency> 22 <dependency> 23 <groupId>log4j</groupId> 24 <artifactId>log4j</artifactId> 25 <version>1.2.17</version> 26 </dependency> 27 <dependency> 28 <groupId>org.slf4j</groupId> 29 <artifactId>slf4j-api</artifactId> 30 <version>1.7.25</version> 31 </dependency> 32 <dependency> 33 <groupId>org.apache.commons</groupId> 34 <artifactId>commons-lang3</artifactId> 35 <version>3.4</version> 36 </dependency> 37 <dependency> 38 <groupId>io.netty</groupId> 39 <artifactId>netty-all</artifactId> 40 <version>4.0.35.Final</version> 41 </dependency> 42 </dependencies>
然後定義一個服務接口:
1 package com.wishwzp.service; 2 3 /** 4 * 服務提供者接口 5 * @author Administrator 6 * 7 */ 8 public interface DemoProviderService { 9 public String sayHello(String name); 10 }
再定義接口實現類:
1 package com.wishwzp.service.impl; 2 3 import com.wishwzp.service.DemoProviderService; 4 5 /** 6 * 服務提供者接口實現類 7 * @author Administrator 8 * 9 */ 10 public class DemoProviderServiceImpl implements DemoProviderService{ 11 public String sayHello(String name) { 12 return "服務員001"; 13 } 14 }
然後再搞個dubbo配置文件dubbo-demo-provider.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 4 xmlns="http://www.springframework.org/schema/beans" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 6 http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 7 8 <!-- 提供方應用名稱, 用於計算依賴關系 --> 9 <dubbo:application name="demo-provider"/> 10 11 <!-- 使用zookeeper註冊中心暴露服務地址 --> 12 <dubbo:registry address="zookeeper://127.0.0.1:2181"/> 13 14 <!-- 使用dubbo協議在20880端口暴露服務 --> 15 <dubbo:protocol name="dubbo" port="20880"/> 16 17 <!-- service實現類作為本地的一個bean --> 18 <bean id="demoProviderService" class="com.wishwzp.service.impl.DemoProviderServiceImpl"/> 19 20 <!-- 聲明需要暴露的服務接口 --> 21 <dubbo:service interface="com.wishwzp.service.DemoProviderService" ref="demoProviderService"/> 22 23 </beans>
測試類:
1 import org.springframework.context.support.ClassPathXmlApplicationContext; 2 3 import java.io.IOException; 4 5 public class ProviderTest { 6 public static void main(String[] args) { 7 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-demo-provider.xml"}); 8 context.start(); 9 System.out.println("服務提供者註冊成功(端口:20880)"); 10 try { 11 System.in.read(); 12 } catch (IOException e) { 13 e.printStackTrace(); 14 } 15 } 16 }
然後我們測試發布dubbo服務,
首先我們要先啟動zookeeper服務,
然後我們運行測試類,發布服務註冊到zookeeper註冊中心去;
說明發布服務OK;
(三)發布Dubbo服務