釋出Dubbo服務
阿新 • • 發佈:2018-12-15
可以從dubbo官網上跳轉到他在GitHub上託管的程式碼中下載demo 新建maven專案 pom.xml新增依賴可以參考下載下來解壓開的的demo: 給自己專案中新增依賴:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
<modelVersion>4.0.0</modelVersion>
<groupId>com.zyf</groupId>
<artifactId>dubbo-demo-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId >
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<!--zookeeper的客戶端依賴 start-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId >
<version>0.10</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<!--zookeeper的客戶端依賴 end-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.0.35.Final</version>
</dependency>
</dependencies>
</project>
配置檔案:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供方應用名稱,用於計算依賴關係 -->
<dubbo:application name="demo-provider" />
<!-- 使用zookeeper註冊中心暴露服務地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 使用dubbo協議,在20880埠暴露服務 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- service實現類作為本地的一個bean -->
<bean id="demoProviderService" class="com.zyf.service.impl.ProviderServiceImpl"/>
<!-- 宣告需要暴露的服務介面 -->
<dubbo:service interface="com.zyf.service.ProviderService" ref="demoProviderService"/>
</beans>
測試類
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class ProviderTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-demo-provider.xml"});
context.start();
System.out.println("服務提供者向zookeeper註冊中心註冊服務成功(埠:20880)");
try {
System.in.read();// press any key to exit
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
context.close();
}
}
啟動zookeeper服務 執行測試類 檢視zookeeper服務