引言
本文主要来学习下发布Dubbo服务,主要参考dubbo开发包里面的demo源码;由浅入深的讲解下这个demo。
配置依赖
首先创建一个maven项目dubbo-demo-provider(基于springBoot),修改pom.xml配置依赖项目,引入所需要的包。
<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>
定义接口服务
简单的定义一个服务接口,如下所示:
package com.dubbo.provider.service; /** * 服务提供者接口 */ public interface DemoProviderService { public String sayHello(String name); }
完善刚刚定义服务接口的实现类,如下所示:
package com.dubbo.provider.service.impl; import com.dubbo.provider.service.DemoProviderService; /** * 服务提供者接口实现类 */ public class DemoProviderServiceImpl implements DemoProviderService { @Override public String sayHello(String name) { return "服务员001"; } }
创建dubbo配置文件,dubbo-demo-provider.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="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"> <!-- 提供方应用名称,用于计算依赖关系 --> <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.dubbo.provider.service.impl.DemoProviderServiceImpl" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.dubbo.provider.service.DemoProviderService" ref="demoProviderService" /> </beans>
测试
编写服务提供者注册的测试类,如下所示:
package com.dubbo.provider.tests; 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("服务提供者注册成功(端口:20880)"); try { System.in.read(); }catch (IOException e) { e.printStackTrace(); } context.close(); } }
运行测试,如下图所示,说明发布服务成功。
原文地址:https://www.cnblogs.com/huanghzm/p/11458922.html
时间: 2024-10-16 07:33:16