下面先來介紹安裝zookeeper:
解壓zookeeper-3.4.6.tar.gz到指定目錄,進入zookeeper-3.4.6\conf目錄並複製zoo_sample.cfg文件更名爲zoo.cfg,由於zookeeper啓動時默認找zoo.cfg這個文件,修改zoo.cfg文件內容以下: javascript
Java代碼 css
核心技術:Maven,Springmvc mybatis shiro, Druid, Restful, Dubbo, ZooKeeper,Redis,FastDFS,ActiveMQ,Nginx html
- # 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=D:\zookeeper-3.4.6\zookeeperinstall\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
到此zookeeper安裝完畢,進入zookeeper-3.4.6\bin目錄,執行zkServer.cmd或者zkServer.sh腳本就能夠啓動zookeeper了,例如在Windows下進入cmd命令行,D:\zookeeper-3.4.6\bin>zkServer.cmd 這裏直接回車便可。
安裝dubbo-admin-2.5.4.war管理控制檯:
把apache-tomcat-6.0.43/webapps/ROOT目錄中的全部內容所有刪除掉,把dubbo-admin-2.5.4.war文件解壓並把所有內容拷貝到apache-tomcat-6.0.43/webapps/ROOT目錄下,以下圖
修改WEB-INF目錄下的dubbo.properties文件:
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
啓動tomcat
訪問http://127.0.0.1:8080/governance/applications/
登陸的用戶名和密碼都是root,不是root/guest
到此爲止dubbo-admin-2.5.4.war管理控制檯安裝完畢。
下面編寫服務提供者代碼:
applicationCustomer.xml文件代碼以下: java
Java代碼 web
- <?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="demoService" class="com.shihuan.zooshare.service.impl.CustomerServiceImpl" />
- <!-- 提供方應用信息,用於計算依賴關係 -->
- <dubbo:application name="shihuan_customer" />
- <!-- 使用multicast廣播註冊中心暴露服務地址
- <dubbo:registry address="multicast://localhost:1234" />-->
- <!-- 使用zookeeper註冊中心暴露服務地址 -->
- <dubbo:registry address="zookeeper://127.0.0.1:2181" />
- <!-- 用dubbo協議在20880端口暴露服務 -->
- <dubbo:protocol name="dubbo" port="20880" />
- <!-- 聲明須要暴露的服務接口 -->
- <dubbo:service interface="com.shihuan.zooshare.service.CustomerService" ref="demoService" />
- </beans>
CustomerService.java文件代碼以下: spring
Java代碼 sql
- package com.shihuan.zooshare.service;
-
- public interface CustomerService {
- public String getName();
- }
CustomerServiceImpl.java代碼以下: express
Java代碼 apache
- package com.shihuan.zooshare.service.impl;
-
- import com.shihuan.zooshare.service.CustomerService;
-
- public class CustomerServiceImpl implements CustomerService {
-
- @Override
- public String getName() {
- System.out.print("shihuan print !!!");
- return "print result !!!";
- }
-
- }
DubooCustomer.java文件代碼以下: json
Java代碼
- package com.shihuan.zooshare.main;
-
- import java.io.IOException;
-
- import org.springframework.beans.BeansException;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class DubooCustomer {
-
- public static void main(String[] args) {
- try {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationCustomer.xml"});
- context.start();
- System.out.println("Press any key to exit.");
-
- System.in.read();
- } catch (BeansException e) {
- System.err.println(e.getMessage());
- e.printStackTrace();
- } catch (IOException e) {
- System.err.println(e.getMessage());
- e.printStackTrace();
- }
- }
-
- }
build.xml文件內容以下:
Java代碼
- <?xml version="1.0" encoding="UTF-8" ?>
- <project name="zooshare" default="genericjar" basedir=".">
- <property name="src" value="${basedir}/src"/>
- <property name="classes" value="${basedir}/bin"/>
- <property name="dest" value="${basedir}/dist"/>
- <property name="zooshare" value="zooshare.jar"/>
- <property name="mainclass" value="com.shihuan.zooshare.main.DubooCustomer" />
-
- <path id="lib-classpath">
- <fileset dir="${basedir}/lib">
- <include name="**/*.jar"/>
- <exclude name="**/*.bak"/>
- </fileset>
- </path>
-
- <target name="clean">
- <delete dir="${basedir}/bin" />
- <delete dir="${basedir}/dist" />
- </target>
-
- <target name="init">
- <mkdir dir="${dest}"/>
- </target>
- <target name="compile" depends="init">
- <javac encoding="utf-8" srcdir="${src}" destdir="${dest}" includeantruntime="false" source="1.6" debug="yes" verbose="yes" failonerror="true" optimize="false">
- <compilerarg line="-encoding UTF-8"/>
- <classpath refid="lib-classpath" />
- </javac>
- <copy todir="${classes}">
- <fileset dir="${src}">
- <include name="**/*.properties"/>
- <include name="**/*.xml"/>
- <exclude name="**/*.bak"/>
- </fileset>
- </copy>
- </target>
-
- <target name="antjar" depends="compile">
- <!--Create a property containing all .jar files,
- prefix lib/, and seperated with a space-->
- <pathconvert property="mf.classpath" pathsep=" ">
- <mapper>
- <chainedmapper>
- <!-- jar包文件只留文件名,去掉目錄信息 -->
- <flattenmapper/>
- <!-- add lib/ prefix -->
- <globmapper from="*" to="lib/*"/>
- </chainedmapper>
- </mapper>
- <path refid="lib-classpath"/>
- </pathconvert>
- <jar destfile="${dest}/zooshare.jar" basedir="${classes}">
- <manifest>
- <attribute name="Main-class" value="${mainclass}"/>
- <attribute name="Class-Path" value="${mf.classpath}"/>
- </manifest>
- </jar>
-
- </target>
-
- <target name="genericjar" depends="antjar"></target>
- </project>
zooshare.jar服務的結構以下圖所示:
startZooshare.bat文件內容以下:
Java代碼
- @echo off
- set CURR_DIR=D:\AppDynamics\dubbo+zookeeper\zooshare
- cd /D %CURR_DIR%
-
- set JAVA_HOME=D:\Java\jdk1.6.0_45
-
- set PATH=%JAVA_HOME%\bin;%PATH%
-
- rem 設置變量爲延遲加載
- setlocal=EnableDelayedExpansion
- set CLASSPATH=.;%JAVA_HOME%\lib;%JAVA_HOME%\jre\lib
- for %%j in (lib\*.jar) DO (
- echo %%j
- set CLASSPATH=!CLASSPATH!;%CURR_DIR%\%%j
- echo %CLASSPATH%
- )
- echo "#############################################"
- echo %CLASSPATH%
- echo "#############################################"
-
- rem set JVM_ARGS="-Xms:512m -XX:MinPermSize=128m"
- rem echo JVM_ARGS=$JVM_ARGS
-
- @echo on
- java -cp %CLASSPATH%;zooshare.jar com.shihuan.zooshare.main.DubooCustomer
startZooshare.sh文件內容以下:
Java代碼
- #!/bin/sh
-
- export CURR_DIR=/home/zoodubbo/
- cd $CURR_DIR
- export JAVA_HOME=/usr/java/jdk1.6.0_45
- #echo JAVA_HOME=$JAVA_HOME
-
- export PATH=$JAVA_HOME/bin:$PATH
- #echo PATH=$PATH
-
- java -version
-
- export CLASSPATH=$CURR_DIR/lib:$CURR_DIR:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
-
- for jarfile in `ls $CURR_DIR/lib/*.jar`
- do
- export CLASSPATH=$CLASSPATH:$jarfile
- done
-
- #echo CLASSPATH=$CLASSPATH
- JVM_ARGS="-Xms:512m -XX:MinPermSize=128m"
- echo JVM_ARGS=$JVM_ARGS
- ulimit -n 400000
- echo "" > nohup.out
- #java org.jboss.netty.bootstrap.Bootstrap
- nohup $JAVA_HOME/bin/java -cp $CLASSPATH:zoodubbo-0.0.1.jar com.shihuan.zoodubbo.C3p0TestMysql &
在Windows環境中運行cmd窗口執行startZooshare.bat就能夠啓動zooshare服務了。
下面來介紹服務消費者代碼:
dubooweb工程所須要的jar文件截圖(這裏要把zooshare.jar加進來)
dubooweb工程整體結構圖
jdbc-context.xml文件代碼以下:
Java代碼
- <?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:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.1.xsd" default-autowire="byName">
-
- <context:property-placeholder location="classpath:*.properties"/>
-
- <!-- 自動掃描組件,須要把controller去掉,不然影響事務管理 -->
- <context:component-scan base-package="com.shihuan.web">
- <context:exclude-filter type="regex" expression="com.shihuan.web.*"/>
- </context:component-scan>
-
- </beans>
springmvc-servlet.xml文件代碼以下:
Java代碼
- <?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:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:util="http://www.springframework.org/schema/util"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.1.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
- http://www.springframework.org/schema/util
- http://www.springframework.org/schema/util/spring-util-3.1.xsd">
-
- <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
- <property name="supportedMediaTypes">
- <list>
- <value>text/html;charset=UTF-8</value>
- </list>
- </property>
- </bean>
-
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
- <property name="messageConverters">
- <util:list id="beanList">
- <ref bean="mappingJacksonHttpMessageConverter" />
- </util:list>
- </property>
- </bean>
-
-
- <!-- 啓動掃描全部的controller -->
- <context:component-scan base-package="com.shihuan.web"/>
-
- <!-- 主要做用於@Controller ,激活該模式
-
- 下面是一種簡寫形式,徹底能夠手動配置替代這種簡寫形式;
- 它會自動註冊DefaultAnnotationHandlerMapping與AnnotationMethodHandlerAdapter 兩個bean,
- 是spring MVC爲@Controllers分發請求所必須的
- -->
- <mvc:annotation-driven/>
-
- <!-- 這裏攔截器還有一種配置方法【針對路徑進行配置】 推薦使用這個,方便直觀-->
- <mvc:interceptors>
- <mvc:interceptor>
- <mvc:mapping path="/*"/>
- <bean class="com.shihuan.web.interceptor.DubboWebInterceptor"></bean>
- </mvc:interceptor>
- </mvc:interceptors>
-
- <!-- 全局配置
- <mvc:interceptors>
- <bean class="com.olm.website.server.web.interceptor.MyInterceptor"></bean>
- </mvc:interceptors>
- -->
- <!-- 配置js,css等靜態文件直接映射到對應的文件夾,不被DispatcherServlet處理 -->
- <mvc:resources location="/resources/**" mapping="/resources"/>
-
- <!-- jsp頁面解析器,當Controller返回XXX字符串時,先經過攔截器,而後該類就會在/WEB-INF/views/目錄下,查找XXX.jsp文件-->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/"></property>
- <property name="suffix" value=".jsp"></property>
- </bean>
- </beans>
applicationConsumer.xml文件代碼以下:
Java代碼
- <?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="consumer-of-shihuan-app" />
- <!-- 使用multicast廣播註冊中心暴露發現服務地址 -->
- <dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
- <!-- 生成遠程服務代理,能夠和本地bean同樣使用demoService -->
- <dubbo:reference id="demoService" interface="com.shihuan.zooshare.service.CustomerService" />
- </beans>
logging.properties文件代碼以下:
Java代碼
- handlers = org.apache.juli.FileHandler, java.util.logging.ConsoleHandler
-
- ############################################################
- # Handler specific properties.
- # Describes specific configuration info for Handlers.
- ############################################################
-
- org.apache.juli.FileHandler.level = FINE
- org.apache.juli.FileHandler.directory = ${catalina.base}/logs
- org.apache.juli.FileHandler.prefix = error-debug.
-
- java.util.logging.ConsoleHandler.level = FINE
- java.util.logging.ConsoleHandler.formatter =java.util.logging.SimpleFormatter
web.xml文件代碼以下:
Java代碼
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
-
- <display-name>dubboweb</display-name>
- <context-param>
- <param-name>dubbowebRootKey</param-name>
- <param-value>dubboweb.root</param-value>
- </context-param>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:jdbc-context.xml,classpath:applicationConsumer.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
-
- <servlet>
- <servlet-name>spring-mvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:springmvc-servlet.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring-mvc</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- <filter>
- <filter-name>encodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
-
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
index.jsp文件代碼以下:
Java代碼
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- %>
-
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
-
- <title>index.jsp starting page</title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- </head>
-
- <body>
- <form action="<%=path%>/duboo1.do" method="post">
- <input type="submit" value="submit" />
- </form>
- </body>
- </html>
DubboWebInterceptor.java文件代碼以下:
Java代碼
- package com.shihuan.web.interceptor;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.springframework.web.servlet.ModelAndView;
- import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
-
- public class DubboWebInterceptor extends HandlerInterceptorAdapter {
-
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
- super.afterCompletion(request, response, handler, ex);
- }
-
- public void postHandler(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
- super.postHandle(request, response, handler, modelAndView);
- }
-
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
- String url = request.getRequestURI();
- System.out.println("ConsumerInterceptor.preHandle()" + url);
-
- return super.preHandle(request, response, handler);
- }
-
- }
DubboWebController.java文件代碼以下:
Java代碼
- package com.shihuan.web.controller;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import com.shihuan.zooshare.service.CustomerService;
-
- @Controller
- @RequestMapping(value="/")
- public class DubboWebController {
-
- @Autowired
- public CustomerService demoService;
-
- @RequestMapping(value="duboo1")
- public String duboo1(){
- System.out.println("come into WebController ......");
- String zoosharestr = demoService.getName();
- System.out.println(zoosharestr);
- return "index";
- }
-
- /*
- @RequestMapping(value="duboo1")
- public String duboo1(){
- System.out.println("jinru......");
- return "index";
- }
- */
-
- }
到此爲止服務消費者代碼編寫完畢。
把dubboweb工程部署到tomcat6的webapps目錄下便可。
【注】:各個模塊的啓動順序不能錯。
第一步:啓動zookeeper服務
第二步:啓動zooshare.jar服務,控制檯應該輸出"Press any key to exit."
第三步:啓動tomcat6
第四步:訪問http://127.0.0.1:8080/governance/applications/
第五步:訪問http://127.0.0.1:8080/dubooweb/
第六步:查看tomcat6控制檯和zooshare.jar服務控制檯是否有正確輸出,tomcat6控制檯應該輸出"come into WebController ......"和"print result !!!",zooshare.jar服務控制檯應該輸出"shihuan print !!!"
都啓動好了後,操做截圖以下:
核心技術:Maven,Springmvc mybatis shiro, Druid, Restful, Dubbo, ZooKeeper,Redis,FastDFS,ActiveMQ,Nginx