1. 程式人生 > >發布Dubbo服務介紹

發布Dubbo服務介紹

say address oca nis except providers 現在 end 我們

我們現在來學習下發布Dubbo服務,主要參考dubbo開發包裏的demo源碼;由淺入深的講解下這個小demo;

首先創建一個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>
View Code

然後定義一個服務接口:

技術分享圖片
 1 package com.java1234.service;
 2  
 3 /**
 4  * 服務提供者接口
 5  * @author Administrator
 6  *
 7  */
 8 public interface DemoProviderService {
 9  
10     public String sayHello(String name);
11 }
View Code

再定義接口實現類:

技術分享圖片
 1 package com.java1234.service.impl;
 2  
 3 import com.java1234.service.DemoProviderService;
 4  
 5 /**
 6  * 服務提供者接口實現類
 7  * @author Administrator
 8  *
 9  */
10 public class DemoProviderServiceImpl implements DemoProviderService{
11  
12     public String sayHello(String name) {
13         return "服務員001";
14     }
15  
16 }
View Code

然後再搞個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.java1234.service.impl.DemoProviderServiceImpl"/>
19  
20     <!-- 聲明需要暴露的服務接口 -->
21     <dubbo:service interface="com.java1234.service.DemoProviderService" ref="demoProviderService"/>
22      
23  
24 </beans>
View Code

測試類:

技術分享圖片
 1 import java.io.IOException;
 2  
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4  
 5 public class ProviderTest {
 6  
 7     public static void main(String[] args) {
 8         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-demo-provider.xml"});
 9         context.start();
10         System.out.println("服務提供者註冊成功(端口:20880)");
11         try {
12             System.in.read();
13         } catch (IOException e) {
14             // TODO Auto-generated catch block
15             e.printStackTrace();
16         }
17         context.close();
18     }
19 }
View Code

然後我們測試發布dubbo服務,

首先我們要先啟動zookeeper服務,

然後我們運行測試類,發布服務註冊到zookeeper註冊中心去;

技術分享圖片

技術分享圖片

說明發布服務OK;

發布Dubbo服務介紹