Dubbo框架以zookeeper作为注册中心程序

(1)Dubbo框架

在这里插入图片描述
公共接口

public interface SayHello {
    public String hello(String name);
}

(1)provider pom依赖

<?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>org.example</groupId>
    <artifactId>order-service-provider</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
   //调用公共接口
        <dependency>
            <groupId>groupId</groupId>
            <artifactId>DubboDemo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>
        <!-- 注册中心使用的是zookeeper,引入操作zookeeper的客户端端 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
    </dependencies>
</project>

(2)provider 服务实现

public class HelloService implements SayHello {
    @Override
    public String hello(String name) {
        return "hello world" +name;
    }
}

(3)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">
    <!-- 1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) -->
    <dubbo:application name="order-service-provider"></dubbo:application>

    <!-- 2、指定注册中心的位置 -->
     <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
<!--    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>-->

    <!-- 3、指定通信规则(通信协议?通信端口) -->
    <dubbo:protocol name="dubbo" port="20888" host="localhost"></dubbo:protocol>


    <!-- 注册服务1 -->
    <bean id="userService" class="com.jyk.service.UserServiceImpl"></bean>
    <!-- 4、暴露服务   ref:指向服务的真正的实现对象 -->
    <dubbo:service interface="com.jyk.service.UserService"
                   ref="userService">
    </dubbo:service>
<!--    注册的服务2 -->
    <bean id="helloService" class="com.jyk.service.HelloService"></bean>
    <dubbo:service id="sayHello" interface="com.jyk.service.SayHello"
                   ref="helloService">
    </dubbo:service>

(4)加载xml文件

public class Test {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
        context.start();
        System.in.read();
    }
}

(5)consumer pom

<?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>org.example</groupId>
    <artifactId>user-service-consumer</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>groupId</groupId>
            <artifactId>DubboDemo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>

        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.32.Final</version>
        </dependency>
    </dependencies>
</project>

(6)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://dubbo.apache.org/schema/dubbo"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">


    <context:component-scan base-package="com.jyk.service.impl"></context:component-scan>
    <dubbo:application name="user-service-consumer"></dubbo:application>

<!--    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" check="false" ></dubbo:registry>-->
<!--调试了两台你主要的问题点还是在注册中心,需要特别注意注册中心可能存在一些问题,要特别关注这个问题-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
    <dubbo:reference id="sayHello" interface="com.jyk.service.SayHello" check="false"/>
    <dubbo:consumer check="false"></dubbo:consumer>
</beans>

(7)启动程序

public class Test {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
        context.start();
        SayHello sayHello = (SayHello) context.getBean("sayHello");
        System.out.println("asas");
        String wangming = sayHello.hello("wangming");
        System.out.println(wangming);
        System.in.read();
    }
}

调用成功
代码在添加链接描述
在这个过程中经常会出错,consumer无法接收到注册中心服务
解决方案
(1)看看实体类是否实现Serializable接口
(2)是否将provider注册到Zookeeper中
(3)网络问题,将虚拟机禁用尝试一下
(4)在consumer端的xml文件中添加check="false"尝试一下,consumer启动不去注册中心寻找服务
(5)防火墙关闭尝试一下
(6)版本问题,我用的是2.6.8版本
需要添加pom依赖
这个问题自己不尝试,根本不知道是啥原因一直以为是consumer没有注册到注册中心,需要看一下源码深入的理解相应的应用

		<dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.32.Final</version>
        </dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值