Dubbo示例

  • Post author:
  • Post category:其他




2.4 快速开始



2.4.1 注册中心安装

Dubbo可以接入注册中心有Nacos、Zookeeper、Multicast、Redis、Simple等注册中心,官网推荐使用Zookeeper作为注册中心,那么首先先搭建Zookeeper注册中心,当然搭建Zk注册中心非常简单步骤如下:

#单机版安装
#1. 网站下下载相应的注册中心
https://archive.apache.org/dist/zookeeper/    

#2. 在zookeeper/bin下启动zkServer.cmd会报Error processing \conf\zoo.cfg,原因是没有zoo.cfg配置文件

#3. copy一份conf/zoo_sample.cfg文件修改名字为zoo.cfg文件修改dataDir=windows具体路径

#4. 启动zookeeper/bin下启动zkServer.cmd,此时正常启动,注册中心zookeeper单机版安装完毕
<dubbo:register address="zookeeper://ip:port">


#集群安装(一主两从架构)
#1. copy一份conf/zoo_sample.cfg文件修改名字为zoo.cfg文件修改dataDir=windows具体路径
  分别修改1,2,3,机器上port
  server.1=ip1:2888:3888 #三台不同的机器,第一个端口号通信使用,第二端口号选举;
  server.2=ip2:2888:3888
  server.3=ip3:2888:3888
#2. 在dataDir目录下创建myid
  1 #第一台myid文件内容
  2 #第二胎myid文件内容
  3 #第三胎myid文件内容

#3.分别启动1,2,3机器下的zookeeper组成一个集群

<dubbo:register address="zookeeper://ip1:port1?backup=ip2:port2,ip3:port3">
#或
<dubbo:registry protocol="zookeeper" address="ip1:port1,ip2:port2,ip3:port3" />



2.4.2 Dubbo Admin

1. 修改 /WEB-INF/dubbo.properties的注册中心及密码;
dubbo.registry.address=zookeeper://127.0.0.1:2081
dubbo.admin.root.password=root
dubbo.admin.guest.password=root
2. war包直接启动

在这里插入图片描述



2.4.3快速开始



2.4.3.1 API配置

创建API ,maven项目,目的定义接口,在Provider进行实现,在Consumer进行调用。

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


2.4.3.2 服务提供方

POM文件引入API

 	<dependencies>
     <!--dubbo 2.7.8 引入-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
     <!--zookeeper Client客户端(需注意curator和zk匹配版本)--->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.8.0</version>
        </dependency>
     <!--引入API-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

定义SayHello实现逻辑

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

dubbo服务提供者配置文件

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 提供方应用信息,用于计算依赖关系   application必填-->
    <dubbo:application name="hello-world-app"  />

    <!-- 使用zookeeper注册中心暴露服务地址  registry必填-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

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

    <!-- 声明需要暴露的服务接口 暴露的服务必填,再ServiceConfig会进行相关校验
	if (StringUtils.isEmpty(interfaceName)) { 接口为null报错
            throw new IllegalStateException("<dubbo:service interface=\"\" /> interface not allow null!");
        } 
	public void checkRef() {
        // reference should not be null, and is the implementation of the given interface
        if (ref == null) { 为null报错
            throw new IllegalStateException("ref not allow null!");
        }
        if (!interfaceClass.isInstance(ref)) { //ref没有实现interface接口会报错
            throw new IllegalStateException("The class "
                    + ref.getClass().getName() + " unimplemented interface "
                    + interfaceClass + "!");
        }
    }
-->
    <dubbo:service interface="com.jyk.demo.SayHello" ref="demoService"/>

    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="demo.UserService" />
</beans>

Spring容器启动Provider

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


2.4.3.3 服务消费者配置
	<dependencies>
     <!--dubbo 2.7.8 引入-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
     <!--zookeeper client--->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.8.0</version>
        </dependency>
     <!--引入API-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

Dubbo服务消费者配置

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样
	ConfigValidationUtils.validateApplicationConfig(getApplication()); //源码会校验 null或长度大于200报错
	-->
    <dubbo:application name="consumer-of-helloworld-app"  />

    <!-- 使用zookeeper注册中心暴露发现服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

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

spring启动远程调用

public class UserServiceDemo {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); //加载xml配置文件
        SayHello demoService = context.getBean("demoService", SayHello.class); //获取单例bean对象
        String name = demoService.sayHello("zhangsan"); //远程过程调用
        System.out.println(name); // hello lisi 
    }
}

如果启动消费者报错,说明服务消费方没有启动:

Exception in thread "main" org.apache.dubbo.rpc.RpcException: No provider available from registry 127.0.0.1:2080 for service com.dubbo.Hello on consumer 192.168.0.106





版权声明:本文为jiyukun1原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。