Dubbo项目简单实践

1.美图

在这里插入图片描述

上一节:https://blog.csdn.net/qq_21383435/article/details/81076625

1.zookeeper集群

zookeeper伪集群模式

2.新建maven项目DubboTest

<groupId>com.lcc.dubbo</groupId>
 <artifactId>DubboTest</artifactId>
 <version>1.0-SNAPSHOT</version>

然后引入包

<dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.6</version>
        </dependency>
        <!-- 添加zk客户端依赖 -->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <!-- 添加对dubbo-common的依赖,使用UserService -->
        <dependency>
            <groupId>per.lx</groupId>
            <artifactId>dubbo-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

然后项目结构如下

这里写图片描述

2.1 生产者,提供者,provider

package com.lcc.dubbo.test.service;

/**
 * Created by lcc on 2018/7/17.
 */
public interface DemoService {
    String sayHello(String name);
}
package com.lcc.dubbo.test.service.impl;

import com.lcc.dubbo.test.service.DemoService;

/**
 * Created by lcc on 2018/7/17.
 */
public class DemoServiceImpl implements DemoService {
    public String sayHello(String name) {
        return "Hello " + name;
    }
}
package com.lcc.dubbo.test;

/**
 * Created by lcc on 2018/7/17.
 */
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
        context.start();
        System.in.read(); // 按任意键退出
    }
}

/Users/lcc/IdeaProjects/DubboTest/src/main/resources/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="lcc_provider" />
    <!-- 配置zookeeper的地址,集群地址用逗号隔开 -->
    <dubbo:registry protocol="zookeeper" address="lcc:2181,lcc:2182,lcc:2183" />
    <!-- 用dubbo协议在29014端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="29014" />
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.lcc.dubbo.test.service.DemoService" ref="demoService" />
    <!-- 具体的实现bean -->
    <bean id="demoService" class="com.lcc.dubbo.test.service.impl.DemoServiceImpl" />
</beans>

2.2 消费者

package com.lcc.dubbo.test;

/**
 * Created by lcc on 2018/7/17.
 */
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lcc.dubbo.test.service.DemoService;

public class Consumer {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
        context.start();
        DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
        String hello = demoService.sayHello("lcc"); // 执行远程方法
        System.out.println( hello ); // 显示调用结果
    }
}

/Users/lcc/IdeaProjects/DubboTest/src/main/resources/consumer.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="lcc_consumer" />
    <!-- 配置zookeeper的地址,集群地址用逗号隔开 -->
    <dubbo:registry protocol="zookeeper" address="lcc:2181,lcc:2182,lcc:2183" />
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="demoService" interface="com.lcc.dubbo.test.service.DemoService" />
</beans>

然后分别运行provider

这里写图片描述

消费者

这里写图片描述

可以看到我们调用了服务

2.3 zookeeper查看

运行provider前

[zk: localhost:2181(CONNECTED) 35] ls /
[zookeeper]

运行provider后

[zk: localhost:2181(CONNECTED) 38] ls /
[zookeeper, dubbo]
[zk: localhost:2181(CONNECTED) 39] ls /dubbo
[com.lcc.dubbo.test.service.DemoService]
[zk: localhost:2181(CONNECTED) 40] ls /dubbo/com.lcc.dubbo.test.service.DemoService
[configurators, providers]
[zk: localhost:2181(CONNECTED) 41] ls /dubbo/com.lcc.dubbo.test.service.DemoService/providers
[dubbo%3A%2F%2F192.168.1.47%3A29014%2Fcom.lcc.dubbo.test.service.DemoService%3Fanyhost%3Dtrue%26application%3Dlcc_provider%26dubbo%3D2.5.6%26generic%3Dfalse%26interface%3Dcom.lcc.dubbo.test.service.DemoService%26methods%3DsayHello%26pid%3D4131%26side%3Dprovider%26timestamp%3D1531809097088]
[zk: localhost:2181(CONNECTED) 42] ls /

再次运行Consumer后

zk: localhost:2181(CONNECTED) 42] ls /
[zookeeper, dubbo]
[zk: localhost:2181(CONNECTED) 43] ls /dubbo
[com.lcc.dubbo.test.service.DemoService]
[zk: localhost:2181(CONNECTED) 44] ls /dubbo/com.lcc.dubbo.test.service.DemoService/
Command failed: java.lang.IllegalArgumentException: Path must not end with / character
[zk: localhost:2181(CONNECTED) 45] ls /dubbo/com.lcc.dubbo.test.service.DemoService
[consumers, configurators, routers, providers]
[zk: localhost:2181(CONNECTED) 46] ls /dubbo/com.lcc.dubbo.test.service.DemoService/consumers
[]

3.注意:

我们如果与运行一次后,修改包名后,再次运行会报错,因为已经在zookeeper中注册过了,可以修改配置文件xml或者删除掉zookeeper中的注册信息

Caused by: com.alibaba.dubbo.remoting.RemotingException: Failed to bind NettyServer on /192.168.1.47:29014, cause: Failed to bind to: /0.0.0.0:29014
	at com.alibaba.dubbo.remoting.transport.AbstractServer.<init>(AbstractServer.java:67)
	at com.alibaba.dubbo.remoting.transport.netty.NettyServer.<init>(NettyServer.java:63)
	at com.alibaba.dubbo.remoting.transport.netty.NettyTransporter.bind(NettyTransporter.java:33)
	at com.alibaba.dubbo.remoting.Transporter$Adaptive.bind(Transporter$Adaptive.java)
	at com.alibaba.dubbo.remoting.Transporters.bind(Transporters.java:57)
	at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchanger.bind(HeaderExchanger.java:41)
	at com.alibaba.dubbo.remoting.exchange.Exchangers.bind(Exchangers.java:71)
	at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol.createServer(DubboProtocol.java:277)
	... 24 more
Caused by: org.jboss.netty.channel.ChannelException: Failed to bind to: /0.0.0.0:29014
	at org.jboss.netty.bootstrap.ServerBootstrap.bind(ServerBootstrap.java:303)
	at com.alibaba.dubbo.remoting.transport.netty.NettyServer.doOpen(NettyServer.java:94)
	at com.alibaba.dubbo.remoting.transport.AbstractServer.<init>(AbstractServer.java:62)
	... 31 more
Caused by: java.net.BindException: Address already in use
	at sun.nio.ch.Net.bind0(Native Method)
	at sun.nio.ch.Net.bind(Net.java:433)
	at sun.nio.ch.Net.bind(Net.java:425)
	at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
	at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)

删除指令

delete path [version]

删除指定节点数据,其version参数的作用于set指定一致

delete /node_1/node_1_10000000001

整个节点全删除

注意:delete只能删除不包含子节点的节点,如果要删除的节点包含子节点,使用rmr命令

rmr /node_1
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

九师兄

你的鼓励是我做大写作的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值