dubbo admin监控中心+zookeeper注册中心的简单配置

dubbo+zookeeper注册中心的简单配置

1.新增一个父项目,并在父项目中添加依赖
<dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.7</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
2.在父项目中添加一个子模块dubbo_interface,并新增一个IHelloInterface的接口
package com.ym.dubbo;

public interface IHelloInterface {
    String sayHello(String name);
}
3.在当前父项目中添加一个子模块dubbo_provider
  1. 在当前子模块的pom文件中,添加dubbo_interface的依赖

     <dependencies>
            <dependency>
                <groupId>com.ym</groupId>
                <artifactId>dubbo_interface</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    
  2. 在当前子模块中添加dubbo_interface模块的实现类

    package com.ym.dubbo;
    
    public class HelloName implements IHelloInterface {
    
        @Override
        public String sayHello(String name) {
            return "hello " + name;
        }
    }
    
  3. 在当前子模块的resources下新增spring-dubbo.xml的配置文件(注册中心的IP地址一定要找准)

    <?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">
    
        <!--使用bean,来创建一个helloName的示例对象-->
        <bean id="myDubbo" class="com.ym.dubbo.HelloName" />
    
        <!--定义一个dubbo的应用程序名称,将来会在dubbo的管理中心查看-->
        <dubbo:application name="DubboProvider" />
    
        <!--配置dubbo协议,dubbo,hession,RMI-->
        <dubbo:protocol name="dubbo" port="20880" />
    
        <!--将当前应用注册到zookeeper注册中心服务器上-->
        <dubbo:registry address="zookeeper://192.168.41.106:2181"></dubbo:registry>
    
        <!--将当前服务的接口暴露出去,以提供消费方来消费-->
        <dubbo:service interface="com.ym.dubbo.IHelloInterface" ref="myDubbo" />
    
    </beans>
    
  4. 在当前子模块中新建一个测试类将当前的spring-dubbo.xml文件加载起来

    import org.junit.Test;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class DubboTest {
    
        @Test
        public void testProvider(){
    
            //加载spring-dubbo的配置文件
            new ClassPathXmlApplicationContext("classpath:spring-dubbo.xml");
    
            //打印提示信息
            System.out.println("provider is ready");
    
            //死循环保证 长连接
            while (true){}
        }
    }
    
4.在父项目中添加一个子模块dubbo_consumer
  1. 在子模块的pom文件中添加dubbo_interface的依赖(我们也可以添加dubbo_provider的依赖,从而达到调用dubbo_interface模块中的接口方法,但是一般情况下dubbo_provider不一定是我们写的,所以最好添加dubbo_interface的模块依赖:就像是门户网站上面的天气预报功能,门户网站本身不会"架一个锅"来收集天气信息,但是天气预报服务商会提供一个接口,供门户网站使用)

     <dependencies>
            <dependency>
                <groupId>com.ym</groupId>
                <artifactId>dubbo_interface</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    
  2. 在当前子模块中的resources下新增spring-dubbo.xml的配置文件(注册中心的IP地址一定要找准)

    <?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="DubboConsumer" />
        
        <!--设置注册中心,从该注册中心去消费服务-->
        <dubbo:registry address="zookeeper://192.168.41.106:2181" />
        
        <!--获取提供方提供的接口数据-->
        <dubbo:reference interface="com.ym.dubbo.IHelloInterface" id="testDubbo" />
    
    </beans>
    
  3. 在当前子模块新增测试类加载spring-dubbo的配置信息,并测试数据是否获取

    import com.ym.dubbo.IHelloInterface;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class DubboTest {
        IHelloInterface helloInterface;
    
        @Test
        public void testConsumer(){
    
            //   加载spring-dubbo的配置文件
            ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:spring-dubbo.xml");
    
            //  通过id拿到spring-dubbo中定义的testDubbo节点对应的对象
            helloInterface=ac.getBean("testDubbo",IHelloInterface.class);
    
            //  调用testdubbo对象中的方法,完成测试
            String s = helloInterface.sayHello("zhangsan");
    
            //打印结果
            System.out.println(s);
    
            //死循环保证 长连接
            while (true){}
    
        }
    }
    
5.linux下搭建zookeeper的环境
  1. 通过Xftp将解压后的zookeeper-3.4.14文件放到/opt/下

  2. 关闭防火墙

    systemctl stop firewalld

  3. 切换到zookeeper的bin目录下,然后运行命令启动zookeeper

    ./zkServer.sh start

  4. 命令行中出现以下内容,说明启动成功


    启动zookeeper.png
6.运行dubbo_cunsumer子模块中的测试类验证
  • 如果控制台打印正确的内容,说明已经运行过程已经通了
7.配置dubbo的环境(tomcat+dubbo)
  1. 新建一个dubbo的文件夹,解压一个纯净的tomcat,然后修改tomcat-users.xml的配置文件(添加一下代码)

    <role rolename="manager-gui"/>
     <role rolename="manager-script"/>
     <role rolename="manager-jmx"/>
     <role rolename="manager-status"/>
     <user username="yanm" password="yanm" roles="manager-gui, manager-script, manager-jmx, manager-status"/>
    
  2. 下载dubbo-admin-2.5.8.war并将dubbo-admin-2.5.8.war放到tomcat的webapps包下,启动tomcat(dubbo-admin-2.5.8.war有点问题:如果启动报错,并且进入不了页面,就先启动tomcat,然后再把dubbo-admin-2.5.8.war放到webapps下,然后刷新页面)

  3. 修改tomcat-webapps-(dubbo-admin-2.5.8)-(WEB-INF)-dubbo.properties文件,将里面的IP地址修改成linux中(zookeeper)的IP地址

  4. 启动tomcat服务,用户名和密码都是root,可以在dubbo.properties修改,如果显示如下页面,则说明成功


    dubbo-admin登陆1.png
  • 登陆成功如下
dubbo-admin登陆成功1.png
  • 查看信息


    查看信息.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。