安裝好 vmware12 虛擬機,在虛擬機中安裝好centos7系統, 修改 NET 模式的子網。java
tar -zxvf /root/soft/zookeeper-3.4.6.tar.gz -C /usr/local/ git
cp /usr/local/zookeeper-3.4.6/conf/zoo_sample.cfg zoo.cfggithub
mkdir dataweb
dataDir= /usr/local/zookeeper-3.4.6/dataspring
進入zookeeper的bin目錄下:cd /usr/local/zookeeper-3.4.6/bin/ apache
./zkServer.sh start //啓動json
./zkServer.sh status //查看狀態centos
./zkServer.sh stop //關閉spring-mvc
1、首先在pom文件中添加相關依賴mvc
1 <dependency> 2 <groupId>com.alibaba</groupId> 3 <artifactId>dubbo</artifactId> 4 <version>2.8.4</version> 5 </dependency> 6 <dependency> 7 <groupId>org.apache.zookeeper</groupId> 8 <artifactId>zookeeper</artifactId> 9 <version>3.4.7</version> 10 </dependency> 11 <dependency> 12 <groupId>com.github.sgroschupf</groupId> 13 <artifactId>zkclient</artifactId> 14 <version>0.1</version> 15 </dependency>
2、在application-service.xml中
1 <!--將要放入zookeeper服務中心的服務名稱(模塊)--> 2 <dubbo:application name="pyg_sellergoods_service" /> 3 <!-- 配置服務所在位置(包名) --> 4 <dubbo:annotation package="cn.chao.pyg.sellergoods.service.impl" /> 5 <!--加載dubbo配置,下面的端口表明發佈在zookeeper註冊中心中的服務佔有的端口號, 6 默認爲20880,若是有多個服務,則不能重複--> 7 <dubbo:protocol port="20881" name="dubbo" /> 8 <!--2.zookeeper服務註冊的地址--> 9 <dubbo:registry address="zookeeper://192.168.25.128:2181"/>
3、在application-web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:mvc="http://www.springframework.org/schema/mvc" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/mvc 9 http://www.springframework.org/schema/mvc/spring-mvc.xsd 10 http://code.alibabatech.com/schema/dubbo 11 http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 12 13 <mvc:annotation-driven> 14 <!--使用fastjson進行java對象與json字符串的互相轉換--> 15 <mvc:message-converters register-defaults="true"> 16 <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> 17 <property name="supportedMediaTypes" value="application/json"/> 18 <property name="features"> 19 <array> 20 <value>WriteMapNullValue</value> 21 <value>WriteDateUseDateFormat</value> 22 </array> 23 </property> 24 </bean> 25 </mvc:message-converters> 26 </mvc:annotation-driven> 27 28 <!--指定模塊名稱--> 29 <dubbo:application name="pyg_manager_web" /> 30 <!-- 指定誰去服務中心獲取service--> 31 <dubbo:annotation package="cn.chao.pyg.manager.controller"/> 32 <!--zookeper服務中心的地址--> 33 <dubbo:registry address="zookeeper://192.168.25.128:2181"/> 34 </beans>
4、在service模塊、controller模塊中的註解
1 service模塊中 2 /** 3 * 注入到spring容器中 4 * 注意:這裏必須是dubbo的service註解 5 * import com.alibaba.dubbo.config.annotation.Service; 6 */ 7 @Resource 8 private TbBrandMapper tbBrandMapper; 9 10 11 controller模塊中 12 /** 13 * 從zookeeper中拿到的service 14 * Reference註解是dubbo包中的 15 * 注意:import com.alibaba.dubbo.config.annotation.Reference; 16 */ 17 @Reference 18 private IBrandService iBrandService;