Dubbo(2)发布Dubbo服务
主要参考Dubbo源码包里面的dubbo-demo源码;
1.项目结构:
2.pom.xml中的依赖:

<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.cy</groupId> <artifactId>dubbo-provider</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.0</version> </dependency> <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> <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>
3.服务提供者接口:DemoProviderService:
package com.cy.service; /** * 服务提供者接口 * @author CY * */ public interface DemoProviderService { String sayHello(String name); }
4.服务提供者接口实现类:DemoProviderServiceImpl:
package com.cy.service.impl; import com.cy.service.DemoProviderService; /** * 服务提供者接口实现类 * @author CY * */ public class DemoProviderServiceImpl implements DemoProviderService { public String sayHello(String name) { return "Hello," + name; } }
5.dubbo配置文件:dubbo-provider.xml:
<?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.cy.service.impl.DemoProviderServiceImpl"/> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.cy.service.DemoProviderService" ref="demoProviderService"/> </beans>
6.测试类:ProviderTest:
import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ProviderTest { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-provider.xml"}); context.start(); System.out.println("服务提供者向zookeeper注册中心注册服务成功(端口:20880)"); try { System.in.read(); // press any key to exit } catch (IOException e) { e.printStackTrace(); } context.close(); } }
7.测试:
先启动zookeeper服务,运行zkServer.cmd;
在运行ProviderTest.java,console打印:
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 服务提供者向zookeeper注册中心注册服务成功(端口:20880)
可以看到zkServer的命令窗口打印:
2018-09-16 11:12:57,314 [myid:] - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxnFactory@215] - Accepted socket connection from /127.0.0.1:59894 2018-09-16 11:12:57,321 [myid:] - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:ZooKeeperServer@938] - Client attempt ing to establish new session at /127.0.0.1:59894 2018-09-16 11:12:57,376 [myid:] - INFO [SyncThread:0:ZooKeeperServer@683] - Established session 0x1000786f7320000 with negotiated timeout 30000 for client /127.0.0.1:59894 2018-09-16 11:12:57,424 [myid:] - INFO [ProcessThread(sid:0 cport:2181)::PrepRequestProcessor@653] - Got user-level Kee perException when processing sessionid:0x1000786f7320000 type:create cxid:0x4 zxid:0xa txntype:-1 reqpath:n/a Error Path :/dubbo/com.cy.service.DemoProviderService/configurators Error:KeeperErrorCode = NodeExists for /dubbo/com.cy.service.De moProviderService/configurators
zookeeper接受了59894端口程序的链接:
C:\Users\CY>netstat -aon | findstr "59894"
TCP 127.0.0.1:2181 127.0.0.1:59894 ESTABLISHED 9192 正是test虚拟机java.exe程序;