dubbo系列二、dubbo+zookeeper+dubboadmin分布式服务框架搭建(windows平台)

2021-07-09 01:08

阅读:457

标签:demo   alt   安装   http   conf   配置   lease   分享图片   guid   

一、zookeeper配置中心安装

1、下载安装包,zookeeper-3.4.6.tar.gz

2、解压安装包,修改配置文件

      参考zookeeper-3.4.6/conf/zoo_sample.cfg文件,同步录下建立zoo.cfg,配置如下:

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=E:\项目\zookeeper-3.4.6\data
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

3、启动zk

点击E:\项目\zookeeper-3.4.6\bin\zkServer.cmd

socket connection from /192.168.1.100:54836

 二、dubboadmin监控中心的安装配置

1、下载tomcat安装运行

2、下载dubbo-admin-2.5.8.war到tomcat7 \ webapps目录下

3、修改dubbo.properties

重启tomcat、在编译后的文件中找到\WEB-INF文件夹下的dubbo.properties文件,然后进行配置,默认属性配置如下:

dubbo.registry.address=zookeeper://192.168.1.100:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest

4、验证dubbo-admin

重启zk、tomcat、访问:http://192.168.1.100:8080/dubbo-admin-2.5.8  ,进入监控中心的管理界面(默认管理员账户密码为:root,root)

技术分享图片

三、dubbo代码示例

1、公共接口service

package com.dubbo.demo.api;

public interface DemoRpcService {

    /**
     * 测试方法
     * @return
     */
    String getUserName(String uid);
}

pom.xml


         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">
    4.0.0dubbo.demo
    dubbo-api
    1.0-SNAPSHOT

2、生产者代码

package com.dubbo.demo;
import com.dubbo.demo.api.DemoRpcService;
public class DemoRpcServiceImpl implements DemoRpcService {


    public String getUserName(String uid) {
        System.out.println("接收入参:"+uid);
        return "小明";
    }
}

启动代码:

    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context
                = new ClassPathXmlApplicationContext("classpath:dubbo-provider.xml");
        context.start();
        // 阻塞当前进程,否则程序会直接停止
        System.in.read();
    }

 

dubbo-provider.xml


       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">

    
    interface="com.dubbo.demo.api.DemoRpcService" ref="demoRpcService"/>
    class="com.dubbo.demo.DemoRpcServiceImpl"/>

pom.xml


         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">
    4.0.0dubbo.demo
    dubbo-provider
    1.0-SNAPSHOTcom.alibaba
            dubbo
            2.6.0com.101tec
            zkclient
            0.10dubbo.demo
            dubbo-api
            1.0-SNAPSHOT

 

3、消费者代码

public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context
= new ClassPathXmlApplicationContext("classpath:dubbo-consumer.xml");
context.start();

String useId = "123456";
DemoRpcService demoService = (DemoRpcService) context.getBean("demoRpcService");
System.out.println("收到结果"+demoService.getUserName(useId));

// 阻塞当前进程,否则程序会直接停止
System.in.read();
}

dubbo-consumer.xml:


       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">

    
    interface="com.dubbo.demo.api.DemoRpcService" id="demoRpcService"/>

pom.xml


         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">
    4.0.0dubbo.demo
    dubbo-consumer
    1.0-SNAPSHOTcom.alibaba
            dubbo
            2.6.0com.101tec
            zkclient
            0.10dubbo.demo
            dubbo-api
            1.0-SNAPSHOTorg.apache.maven.plugins
                maven-compiler-plugin
                3.7.01.81.8utf-8

4、运行测试

先启动生产者、再启动消费者

Connected to the target VM, address: ‘127.0.0.1:52472‘, transport: ‘socket‘
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
接收入参:123456
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
收到结果小明

5、代码下载地址

GitHub

dubbo系列二、dubbo+zookeeper+dubboadmin分布式服务框架搭建(windows平台)

标签:demo   alt   安装   http   conf   配置   lease   分享图片   guid   

原文地址:https://www.cnblogs.com/wangzhuxing/p/9723236.html


评论


亲,登录后才可以留言!