Dubbo实例快速上手

如不了解Dubbo,请移步了解:什么是Dubbo

Dubbo快速上手(Java Demo)

    ①我们需要搞一个服务提供方(dubbo-server)和服务消费方(dubbo-client)。项目结构图如下:

    ②maven分模块开发,在服务提供方server中A模块提供一个sayHello接口,然后在B模块提供一个sayHello的实现类

/**
 * 接口类
 */
public interface ISayHello {
    public String sayHello(String name);
}
/**
 * 实现类
 */
public class SayHelloImpl implements ISayHello {
    public String sayHello(String name) {
        return "Hello," + name;
    }
}

 ③服务提供方,使用dubbo服务治理,pom.xml引入dubbo依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.6.5</version>
</dependency>
<!-- 解决如下问题:Caused by: java.lang.ClassNotFoundException: io.netty.channel.EventLoopGroup -->
<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.32.Final</version>
</dependency>
<!-- 解决如下问题:Caused by: java.lang.ClassNotFoundException: org.apache.curator.RetryPolicy -->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>2.13.0</version>
</dependency>
<!-- 解决如下问题:Caused by: java.lang.ClassNotFoundException: org.apache.curator.framework.recipes.cache.TreeCacheListener -->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>2.13.0</version>
</dependency>

使用zookeeper中间件治理,添加zookeeper的maven依赖

<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.14</version>
</dependency>

 ④服务提供方,resources文件夹下,配置一个xxx.xml(名称随意,此处定义dubbo-server.xml)文件,用 Spring 配置声明暴露服务

<?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-4.3.xsd        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!-- 服务提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="dubbo-server" owner="lzb" />

    <!-- 注册中心(dubbo支持zookeeper/redis/multicast/simple,用的比较多的是zookeeper) -->
    <dubbo:registry address="zookeeper://192.168.204.201:2181"/>

    <!--dubbo协议:用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--声明需要暴露的服务接口 -->
    <dubbo:service interface="com.test.dubbo.ISayHello" ref="sayHelloService"/>
    <!-- spring声明式bean -->
    <bean id="sayHelloService" class="com.test.dubbo.SayHelloImpl"/>

</beans>

⑤服务提供方,加载Spring配置。

/**
 * 随便定义一个类,加载Spring配置 
 */
public class ProviderMain {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-server.xml"});
        context.start();
        System.in.read();
    }
}

⑥服务消费者,使用dubbo服务治理,pom.xml引入dubbo依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.6.5</version>
</dependency>
<!-- 解决如下问题:Caused by: java.lang.ClassNotFoundException: io.netty.channel.EventLoopGroup -->
<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.32.Final</version>
</dependency>
<!-- 解决如下问题:Caused by: java.lang.ClassNotFoundException: org.apache.curator.RetryPolicy -->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>2.13.0</version>
</dependency>
<!-- 解决如下问题:Caused by: java.lang.ClassNotFoundException: org.apache.curator.framework.recipes.cache.TreeCacheListener -->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>2.13.0</version>
</dependency>

使用zookeeper中间件治理,添加zookeeper的maven依赖

<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.14</version>
</dependency>

⑦服务消费者,resources文件夹下,配置一个xxx.xml(名称随意),此处定义dubbo-client.xml)文件,用 Spring 配置声明暴露服务

<?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-4.3.xsd        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="dubbo-client" owner="lzb"/>

    <!-- 注册中心(以下写法二选一,写法不同,实质相同) -->
    <dubbo:registry address="192.168.204.201:2181" protocol="zookeeper"/>
    <!-- <dubbo:registry address="zookeeper://192.168.204.201:2181"/> -->

    <!-- 生成远程服务代理,可以和本地bean一样使用sayHelloService-->
    <dubbo:reference id="sayHelloService" interface="com.test.dubbo.ISayHello"/>

</beans>

⑧服务消费者,加载Spring配置,并调用远程服务 

/**
 * 随便定义一个类,加载Spring配置 
 */
public class test {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-client.xml"});
        context.start();
        ISayHello entity = (ISayHello)context.getBean("sayHelloService"); // 获取远程服务代理
        String content = entity.sayHello("Dubbo"); // 执行远程方法
        System.out.println(content); // 显示调用结果
    }
}

⑨至此,Dubbo实例完成,注册中心使用ZooKeeper,测试输出调用结果:"Hello,Dubbo"

 dubbo-demo已上传:dubbo_demo.zip。实例仅供上传备份,如需demo,请发表评论留下email,让我看到你哦

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
基于dubbo的代码实例可以通过以下步骤来实现[^2]: 1. 首先,确保你已经安装了Zookeeper,并启动了Zookeeper服务。 2. 创建一个Maven项目,并在pom.xml文件中添加Dubbo和Zookeeper的依赖。 3. 创建一个接口,定义需要暴露的服务方法。 ```java public interface HelloService { String sayHello(String name); } ``` 4. 创建一个实现类,实现接口中定义的服务方法。 ```java public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "Hello, " + name + "!"; } } ``` 5. 在resources目录下创建一个dubbo配置文件dubbo-provider.xml,配置Dubbo的服务提供者。 ```xml <?xml version="1.0" encoding="UTF-8"?> <dubbo:application name="dubbo-demo-provider" /> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <dubbo:protocol name="dubbo" port="20880" /> <dubbo:service interface="com.example.HelloService" ref="helloService" /> <bean id="helloService" class="com.example.HelloServiceImpl" /> ``` 6. 创建一个启动类,用于启动Dubbo服务提供者。 ```java public class Provider { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-provider.xml"); context.start(); System.in.read(); } } ``` 7. 创建一个消费者项目,重复步骤2和3,创建一个接口和实现类。 8. 在resources目录下创建一个dubbo配置文件dubbo-consumer.xml,配置Dubbo的服务消费者。 ```xml <?xml version="1.0" encoding="UTF-8"?> <dubbo:application name="dubbo-demo-consumer" /> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <dubbo:reference id="helloService" interface="com.example.HelloService" /> ``` 9. 创建一个启动类,用于启动Dubbo服务消费者。 ```java public class Consumer { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-consumer.xml"); HelloService helloService = (HelloService) context.getBean("helloService"); String result = helloService.sayHello("World"); System.out.println(result); } } ``` 以上是一个基于Dubbo的简单代码实例,通过配置Dubbo的服务提供者和消费者,可以实现分布式的服务调用。你可以根据自己的需求进行扩展和定制。如果你想了解更多关于Dubbo的使用和原理,可以参考Dubbo的官方文档和源码[^1]。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扛麻袋的少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值