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(); } }
posted on 2017-11-12 02:34 听风tingfeng 阅读(865) 评论(0) 编辑 收藏 举报