zoukankan      html  css  js  c++  java
  • zookeeper + dubbo + spring boot

    最近开始接触了分布式的一些东西,这里给自己作一个学习笔记。

    这里只是做一个运行demo,具体的理论知识就不在这里阐述了。

    1.zookeeper的安装与启动

    下载地址:http://www.apache.org/dyn/closer.cgi/zookeeper

    下载完成之后,解压,进入到"conf"目录下,新建一个"zoo.cfg"

    内容如下:

    tickTime=2000  
    dataDir= /Users/chenqimiao/zookeeper-3.4.8/data 
    dataLogDir=/Users/chenqimiao/zookeeper-3.4.8/logs  
    clientPort=2181

    参数说明:

    #tickTime: zookeeper中使用的基本时间单位, 毫秒值.
    #dataDir: 数据目录. 可以是任意目录.
    #dataLogDir: log目录, 同样可以是任意目录. 如果没有设置该参数, 将使用和#dataDir相同的设置.
    #clientPort: 监听client连接的端口号.

    执行

    以上介绍的就是zookeeper的单机模式。当然还有伪分布式和分布式的部署方式,这里就不再阐述,有兴趣的可以自行百度。

    2.dubbo-admin的部署

    介于每个人机子的环境都不尽相同,我建议大家可以自行打包所需war包。

    我这里给出一个dubbo的下载地址:http://github.com/alibaba/dubbo,下载全部的文件之后。

    执行如下命令

    之后找到dubbo-master/dubbo-admin/target路径下面的dubbo-admin-2.5.4-SNAPSHOT.war包。

    把这个包丢到tomcat进行部署就ok了。

     

    3.利用IDEA构建spring boot生产者和消费者

    一路next完成之后,在pom中加入如下依赖(dubbo和zookeeper)

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.4.10</version>
                <exclusions>
                    <exclusion>
                        <artifactId>spring</artifactId>
                        <groupId>org.springframework</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.4.6</version>
                <exclusions>
                    <exclusion>
                        <artifactId>slf4j-log4j12</artifactId>
                        <groupId>org.slf4j</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.1</version>
            </dependency>

    接下来先构建一个生产者

    来一个测试接口:

    package com.example.service;
    
    /**
     * Created by chenqimiao on 17/3/24.
     */
    public interface TestService {
    
        String sayHello(String name);
    
    }

    来一个实现类

    package com.example.service.impl;
    
    import com.example.service.TestService;
    
    /**
     * Created by chenqimiao on 17/3/24.
     */
    public class TestServiceImpl implements TestService {
    
        @Override
        public String sayHello(String name) {
    
            return "Hello " + name + "!";
        }
    }

    在resource下面加入一个providers.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="dubbo-provider" owner="dubbo-provider"/>
        <dubbo:registry address="zookeeper://localhost:2181" check="false" subscribe="false"/>
        <dubbo:service interface="com.example.service.TestService" ref="testService"/>
        <bean id="testService" class="com.example.service.impl.TestServiceImpl"></bean>
    
    </beans>

    启动类:

    package com.example;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ImportResource;
    
    @SpringBootApplication
    @ImportResource(value = {"classpath:providers.xml"})
    public class DubboProviderApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DubboProviderApplication.class, args);
        }
    }

    最后在application.properties中加入启动端口:

    server.port=8011

      at last run application!

      那么我们就可以在admin管理界面,看到这个注册的生产者服务

    同样的道理 接下来我们来构建消费者 。消费者的依赖和生产者一致,这里不再重复。

    在resource下加入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="dubbo-consumer"/>
        <dubbo:registry check="false" address="zookeeper://localhost:2181"/>
        <dubbo:reference interface="com.example.service.TestService" id="testService"/>
    </beans>

    来一个controller

    package com.example.controller;
    
    import com.example.service.TestService;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import javax.annotation.Resource;
    
    /**
     * Created by chenqimiao on 17/3/24.
     */
    @Controller
    public class TestController {
    
        @Resource
        private TestService testService;
        @RequestMapping(value = "/" ,produces = "application/json;charset=utf-8")
        @ResponseBody
        public String test(){
            return testService.sayHello("Chenqimiao");
        }
    }

    启动之后,首先我们发现服务有了消费者

      

     紧接着,我们用chrome请求restful接口,消费者服务通过RPC调用请求生产者提供的服务,经过两层服务调用返回后,chrome得到最终的response。

    大致这样就完成了简单的分布式项目构建。但在其中我碰到了一个小小问题 ,不知道社区的朋友有没有办法!

    IDEA不会检测dubbo的远程调用,导致这里误报了一个ERROR,有朋友知道怎么关闭这个错误嘛?

  • 相关阅读:
    Python代码优化概要
    OllyDbg 使用笔记 (一)
    Java报表FineReport在医院院长查询分析系统中有什么用
    MongoDB下载安装測试及使用
    你不可能讲清楚的4个开源协议!!!
    MR之SequenceFile具体解释
    深入浅出AOP(四)--AOP的实现步骤
    SRM 587 Div II L3:ThreeColorabilityEasyy
    转义及编码(u, x)
    转义及编码(u, x)
  • 原文地址:https://www.cnblogs.com/think-in-java/p/6611249.html
Copyright © 2011-2022 走看看