• mac环境下基于jdk1.8,maven搭建dubbo,zookeeper入门小案例


    zookeeper本地安装

    此博客zookeeper安装基于mac上已安装了zsh,zsh安装需先安装homebrew,对于这方面做简单介绍

    终端控制台执行

    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"  安装homebrew

    homebrew是mac上的软件管理包

    成功后执行 brew install zsh     安装zsh

    执行    brew install wget    安装wget

    执行       wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh 安装on my zsh

    执行        brew info zookeeper           查看zookeeper信息

    执行    brew install zookeeper        安装zookeeper 

    cat 查看 /usr/local/etc/zookeeper/zoo.cfg 此配置文件为zookeeper的配置文件

    通过执行以下命令

    zkServer         查看zookeeper是否安装成功

    zkServer status       查看zookeeper运行状态

    zkServer start       启动zookeeper服务

    zkCli            zkCli是zookeeper提供的可查看zookeeper运行状态的工具

    https://github.com/alibaba/dubbo 获取dubbo源代码

    修改dubbo-admin的pom.xml文件

    <dependency>
        <groupId>com.alibaba.citrus</groupId>
        <artifactId>citrus-webx-all</artifactId>
        <version>3.1.6</version>
    </dependency>

     添加spring的exclusion依赖

    <exclusion>
             <groupId>org.springframework</groupId>
             <artifactId>spring</artifactId>
    </exclusion>

    打包dubbo-admin项目,将tomcat的ROOT文件夹清空,将打包完的war包,解压放到tomcat的ROOT文件夹,运行tomcat

    浏览器输入http://localhost:8080/

    账号 root  密码 root

    此demo分为三个项目,api定义暴露的接口,provider依赖api定义接口的具体实现,consumer依赖api完成对接口的调用

    provider的applicationContext.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
            ">
    
        <!-- 具体的实现bean -->
        <bean id="helloService" class="com.input4hua.api.impl.HelloServiceImpl" />
    
        <!-- 提供方应用信息,用于计算依赖关系 -->
        <dubbo:application name="api_hello_provider" />
    
    
        <!-- 使用zookeeper注册中心暴露服务地址 -->
        <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    
        <!-- 用dubbo协议在20880端口暴露服务 -->
        <dubbo:protocol name="dubbo" port="20880" />
    
        <!-- 声明需要暴露的服务接口 引用(ref)具体实现-->
        <dubbo:service interface="com.input4hua.api.inter.HelloService"
            ref="helloService" />
    
    </beans>

     dubbo:service提供"com.input4hua.api.inter.HelloService"接口服务,具体实现为id="helloService"所指"com.input4hua.api.impl.HelloServiceImpl"

    consumer的applicationContext.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="api_hello_consumer" />
    
        <!-- 使用zookeeper注册中心暴露服务地址 -->
        <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    
        <!-- 生成远程服务代理,可以像使用本地bean一样使用helloService -->
        <dubbo:reference id="helloService"
            interface="com.input4hua.api.inter.HelloService" />
    
    </beans>

     dubbo:reference引用"com.input4hua.api.inter.HelloService"接口,具体实现从zookeepr中获取相关提供者路径,从而调用提供者提供的具体服务实现

    api的接口定义

    package com.input4hua.api.inter;
    
    /**
     * Created by wangshiya on 2017/11/11.
     */
    public interface HelloService {
    
        String sayHello(String name);
    }

    provider的具体实现

    package com.input4hua.api.impl;
    
    import com.input4hua.api.inter.HelloService;
    
    /**
     * Created by wangshiya on 2017/11/11.
     */
    public class HelloServiceImpl implements HelloService{
        @Override
        public String sayHello(String name) {
    
            return "Hi "+name+"Welcome to Dubbo";
        }
    }

    provider注册方法

    package com.input4hua.provider.main;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import java.io.IOException;
    
    /**
     * Created by wangshiya on 2017/11/11.
     */
    public class App {
        public static void main(String[] args) throws IOException {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
            context.start();
            System.in.read();
        }
    }

    consumer的调用方法

    package com.input4hua.controller;
    
    import com.input4hua.api.inter.HelloService;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import java.io.IOException;
    
    /**
     * Created by wangshiya on 2017/11/11.
     */
    public class App {
        public static void main(String[] args) throws IOException {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
            HelloService remoteservice = (HelloService) context.getBean("helloService");
            String result = remoteservice.sayHello("Java");
            System.out.println(result);
            System.in.read();
        }
    }

     https://gitee.com/input4hua/dubbo_study/tree/demo_branch/

  • 相关阅读:
    Golden Tiger Claw (KM算法)
    Xor Sum题解>----(思维+递归)
    Mister B and PR Shifts(思维)
    Traffic Real Time Query System-(点双缩点+LCA)
    最大的位或----(极简代码)
    萌萌哒题解_倍增并查集
    连续攻击游戏(递增1)题解------(并查集)
    道路与航线 (连通块Dijkstra+拓扑序||spfa+slf优化)
    美团校招-表达式求值
    天猫前端电话面试
  • 原文地址:https://www.cnblogs.com/input4hua/p/7819995.html
Copyright © 2020-2023  润新知