您的位置:澳门402永利com > 计算机 网络 > 框架搭建,订阅模式实现配置动态变更

框架搭建,订阅模式实现配置动态变更

发布时间:2019-11-01 20:01编辑:计算机 网络浏览(154)

    ZooKeeper的Watcher事件机制得以说分布式场景下的观望者形式的实现。基于这一个watcher事件机制,同盟注册到一定的ZNode节点,能够完毕java应用的配备运营时的改观。在就学zookeeper此前,听同事说配置能够在运转时动态更动,感觉不敢相信 无法相信。研习了zookeeper之后,实现那几个功用是很easy的。

    总括下怎么用那多少个常用框架搭建三个Java Web工程,方便今后更敏捷的支付工程。首先得用maven搭建贰个多模块的web工程,这里不再赘言,请参见以前的总计:用Maven命令行创建多模块Web项目

      发布/订阅系统规划起来独有三种方式,推和拉。

    先是导入那个框架所需的maven依赖:

    1. 推方式,服务端负担把改变的多寡推给订阅的客商端。Web即时通讯里的Comet才干便足以兑现这种意义。
    2. 拉方式,也正是客户端按时轮询服务端。拉格局不但有延迟,给服务端带来比超大压力,并且特不算。

    <spring.version>3.2.2.RELEASE</spring.version>
    <v.velocity>1.6.3</v.velocity>
    <v.velocity.tool>1.2</v.velocity.tool>
    <jdbc.driver.version>5.1.15</jdbc.driver.version>

      zookeeper接受的是推拉结合的情势

    <!-- ibatis begin -->
    <dependency>
        <groupId>org.apache.ibatis</groupId>
        <artifactId>ibatis-sqlmap</artifactId>
        <version>2.3.4.726</version>
    </dependency>

    1. 顾客端订阅znode节点
      2. 被订阅的节点发生变化后,zookeeper服务端向顾客端发生多少变动的watcher事件通知
    2. 客商端接收到watcher通告后,主动从服务端拉取更改的数量

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${jdbc.driver.version}</version>
    </dependency>
    <!-- ibatis end -->

      Ali的布署改变中间件diamond,相通是依照推拉结合的形式来促成多少的动态改造。初学zookeeper,写了三个数据库配置动态改换的demo。zookeeper自带的Watcher注册后,数据变动贰回便会自动撤废注册。这一个规划实在反人类,大好些个的开拓者的必要必然是挂号壹次,服务平生。所以转向开源的ZkClient顾客端。
      首先本地须求运转贰个zookeeper的服务端,而且有几个“/db”节点,作者用那么些节点存款和储蓄数据库配置消息。
      客商端工程必要引入zookeeper和zkclient的重视。

    <!-- spring begin -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.9</version>
    </dependency>
    <dependency>
        <groupId>com.github.adyliu</groupId>
        <artifactId>zkclient</artifactId>
        <version>2.1.1</version>
    </dependency>
      客户端demo

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <artifactId>spring-context</artifactId>
        <groupId>org.springframework</groupId>
        <version>${spring.version}</version>
    </dependency>

    import com.github.zkclient.IZkDataListener;
    import com.github.zkclient.ZkClient;

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>

    import java.io.IOException;

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    /**
     * project  : zk
     * package  : PACKAGE_NAME
     * author  : lvsheng
     * date    : 2016/10/5 下午11:17
     */
    public class ZkClientTest {

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
    </dependency>

        public static void main(String[] args) {
            ZkClient zkClient = new ZkClient("127.0.0.1:2181");
            zkClient.subscribeDataChanges("/db", new IZkDataListener() {
                public void handleDataChange(String dataPath, byte[] data) throws Exception {
                    System.out.println(new String(data));
                }

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!-- spring end -->

                public void handleDataDeleted(String dataPath) throws Exception {
                    System.out.println(dataPath);
                }
            });

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>

            try {
                System.in.read();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>

      现在我们来测量试验那些效应,在调控台依次输入如下命令:

    <!-- velocity begin -->
    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity</artifactId>
        <version>${v.velocity}</version>
    </dependency>
    <dependency>
        <groupId>velocity-tools</groupId>
        <artifactId>velocity-tools</artifactId>
        <version>${v.velocity.tool}</version>
    </dependency>
    <!-- velocity end -->

    [zk: localhost:2181(CONNECTED) 48] set /db chat.jdbc.driver=com.mysql.jdbc.Driver|chat.jdbc.url=jdbc:mysql://192.168.146.120:3306/chat_test?useUnicode=true&amp;characterEncoding=GBK|chat.jdbc.maxActive=5

        web.xml里配置spring和spring mvc 入口

    [zk: localhost:2181(CONNECTED) 49] set /db chat.jdbc.driver=com.mysql.jdbc.Driver|chat.jdbc.url=jdbc:mysql://127.0.0.1:3306/chat_test?useUnicode=true&amp;characterEncoding=GBK|chat.jdbc.maxActive=5         

     <servlet>
      <servlet-name>appServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:spring-config.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
     </servlet>

      观望Java代码的输出

     <servlet-mapping>
      <servlet-name>appServlet</servlet-name>
      <url-pattern>/</url-pattern>
     </servlet-mapping>
        DispatcherServlet是spring mvc的保有央求入口的servlet。contextConfigLocation参数配置spring的总安顿文件地点。若是是spring集成struts的话,还亟需单独布置spring的监听器,可是只假设集成spring mvc,则不需求上边这段配置了:

    chat.jdbc.driver=com.mysql.jdbc.Driver|chat.jdbc.url=jdbc:mysql://192.168.146.120:3306/chat_test?useUnicode=true&amp;characterEncoding=GBK|chat.jdbc.maxActive=5
    chat.jdbc.driver=com.mysql.jdbc.Driver|chat.jdbc.url=jdbc:mysql://127.0.0.1:3306/chat_test?useUnicode=true&amp;characterEncoding=GBK|chat.jdbc.maxActive=5

    <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:spring-context.xml</param-value>
    </context-param>

      数据库配置的ip被成功的动态修正。

        spring总布置文件

    ZooKeeper学习总括  http://www.linuxidc.com/Linux/2016-07/133179.htm

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
          xmlns:xsi=""
          xmlns:context=""
          xmlns:aop=""
          xmlns:mvc=""
          xsi:schemaLocation="
       
       
       
       
       
       
          default-autowire="byName">

    Ubuntu 14.04装置布满式存款和储蓄Sheepdog+ZooKeeper  http://www.linuxidc.com/Linux/2014-12/110352.htm

        <context:component-scan base-package="com.jd.im.**"/>

    CentOS 6安装sheepdog 虚构机分布式积存  http://www.linuxidc.com/Linux/2013-08/89109.htm

        <mvc:annotation-driven/>

    ZooKeeper集群配置 http://www.linuxidc.com/Linux/2013-06/86348.htm

        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:im-king.properties</value>
                    <value>classpath:important.properties</value>
                    <value>classpath:jss_config.properties</value>
                </list>
            </property>
        </bean>

    接收ZooKeeper完结布满式共享锁 http://www.linuxidc.com/Linux/2013-06/85550.htm

        <import resource="spring/spring-config-mvc.xml"></import>
        <import resource="spring/spring-config-datasource.xml"/>
    </beans>
        总安排文件里集成了spring mvc配置文件和ibatis数据源的配置文件。

    分布式服务框架 ZooKeeper -- 管理布满式境况中的数据 http://www.linuxidc.com/Linux/2013-06/85549.htm

        spring mvc 配置

    ZooKeeper集群环境搭建施行 http://www.linuxidc.com/Linux/2013-04/83562.htm

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="" xmlns:xsi="" xmlns:context=""
     xmlns:mvc=""
     xsi:schemaLocation="
         
         
         
         
             ;

    ZooKeeper服务器集群景况布署实地度量 http://www.linuxidc.com/Linux/2013-04/83559.htm

     <context:component-scan base-package="com.**.dd.mall.web.controller" />
     <mvc:annotation-driven />
     
     <bean id="dateObjectMapper" class="com.**.dd.mall.web.util.DateObjectMapper"></bean>
     <!-- Json重回 乱码管理 -->
     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
      <property name="messageConverters">
       <list>
        <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
         <property name="objectMapper" ref="dateObjectMapper"></property>
        </bean>
        <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
         <property name="supportedMediaTypes">
          <list>
           <value>text/plain;charset=UTF-8</value>
          </list>
         </property>
        </bean>
        <bean class="org.springframework.http.converter.ResourceHttpMessageConverter" />
        <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
        <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" />
        <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" />
       </list>
      </property>
     </bean>

    ZooKeeper集群安装 http://www.linuxidc.com/Linux/2012-10/72906.htm

     <mvc:resources mapping="/dist/**" location="/dist/" cache-period="3600" />
     <mvc:resources mapping="/js/**" location="/js/" cache-period="3600" />
     <mvc:resources mapping="/css/**" location="/css/" cache-period="3600" />
     <!--<mvc:resources mapping="/fonts/**" location="/fonts/" cache-period="3600" /> -->
     <mvc:resources mapping="/config.json" location="/" cache-period="3600" />
     <mvc:resources mapping="/image/**" location="/image/" cache-period="3600" />

    Zookeeper3.4.6的安装 http://www.linuxidc.com/Linux/2015-05/117697.htm

     <!-- 对转会页面包车型客车路径拆解深入分析。prefix:前缀, suffix:后缀 -->
     <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
      <property name="cache" value="true"></property>
      <property name="suffix" value=".vm"></property>
      <property name="prefix" value=""></property>
      <property name="contentType" value="text/html;charset=utf-8"></property>
      <property name="exposeRequestAttributes" value="true"></property>
      <property name="viewClass" value="org.springframework.web.servlet.view.velocity.VelocityLayoutView"></property>
     </bean>

    本文永远更新链接地址:http://www.linuxidc.com/Linux/2016-10/135844.htm

     <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
      <property name="resourceLoaderPath" value="/WEB-INF/vm/"></property>
      <property name="configLocation" value="classpath:velocity.properties"></property>
     </bean>

    图片 1

     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
      <property name="maxUploadSize" value="10485760" />
      <property name="defaultEncoding" value="utf-8"></property>
     </bean>

     <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
      <!-- 设置为true以忽视对Accept Header的支撑 -->
      <property name="order" value="1" />
      <property name="contentNegotiationManager">
       <bean class="org.springframework.web.accept.ContentNegotiationManager">
        <constructor-arg>
         <bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
          <constructor-arg>
           <map>
            <entry key="json" value="application/json" />
            <entry key="xml" value="application/xml" />
           </map>
          </constructor-arg>
         </bean>
        </constructor-arg>
       </bean>
      </property>
      <property name="ignoreAcceptHeader" value="true" />
      <!-- 在未曾增添名时即: "/user/getUser" 时的默许表现格局 -->
      <property name="defaultContentType" value="text/html" />

      <!-- 用于开启 /user/getUser?format=json 的支撑 -->
      <property name="favorParameter" value="true" />
      <property name="defaultViews">
       <list>
        <!-- for application/json -->
        <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
        </bean>
       </list>
      </property>
     </bean>
     
    </beans>
        viewResolver和velocityConfig节点钦赐了spring mv重返页面用velocity。

        Ibatis配置
        带读写抽离的数据源配置
    <?xml version="1.0" encoding="GBK"?>
    <beans xmlns=""
          xmlns:xsi="" xmlns:tx=""
          xsi:schemaLocation="
         
           
      "
          default-autowire="byName">

        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="${chat.jdbc.driver}"/>
            <property name="url" value="${chat.jdbc.url}"/>
            <property name="username" value="${chat.jdbc.username}"/>
            <property name="password" value="${chat.jdbc.password}"/>
            <property name="maxActive" value="${chat.jdbc.maxActive}"/>
            <!-- sql 心跳 -->
            <property name="testWhileIdle" value="true"/>
            <property name="testOnBorrow" value="false"/>
            <property name="testOnReturn" value="false"/>
            <property name="validationQuery" value="select 1"/>
            <property name="validationQueryTimeout" value="1"/>
            <property name="timeBetweenEvictionRunsMillis" value="60000"/>
            <property name="numTestsPerEvictionRun" value="${chat.jdbc.maxActive}"/>
        </bean>

        <bean id="selectedDataSource" name="selectedDataSource"
              class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="${select.chat.jdbc.driver}"/>
            <property name="url" value="${select.chat.jdbc.url}"/>
            <property name="username" value="${select.chat.jdbc.username}"/>
            <property name="password" value="${select.chat.jdbc.password}"/>
            <property name="maxActive" value="${select.chat.jdbc.maxActive}"/>
            <!-- sql 心跳 -->
            <property name="testWhileIdle" value="true"/>
            <property name="testOnBorrow" value="false"/>
            <property name="testOnReturn" value="false"/>
            <property name="validationQuery" value="select 1"/>
            <property name="validationQueryTimeout" value="1"/>
            <property name="timeBetweenEvictionRunsMillis" value="60000"/>
            <property name="numTestsPerEvictionRun" value="${select.chat.jdbc.maxActive}"/>
        </bean>

        <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
            <property name="configLocations">
                <list>
                    <value>classpath:sqlmap-config.xml</value>
                </list>
            </property>
            <property name="dataSource" ref="dataSource"/>
        </bean>

        <bean id="sqlMapClientRead" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
            <property name="configLocations">
                <list>
                    <value>classpath:sqlmap-config.xml</value>
                </list>
            </property>
            <property name="dataSource" ref="selectedDataSource"/>
        </bean>

        <bean id="selectDao" class="com.jd.im.king.template.dao.impl.SelectDaoImpl">
            <property name="sqlMapClient" ref="sqlMapClientRead"/>
        </bean>

        <bean id="writeDao" class="com.jd.im.king.template.dao.impl.WriteDaoImpl">
            <property name="sqlMapClient" ref="sqlMapClient"/>
        </bean>

        <bean id="transactionManager"
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>

        <tx:annotation-driven transaction-manager="transactionManager"/>

    </beans>

        Ibatis的查询信任SqlMapClientFactoryBean这么些目的的计划。这几个指标同有时候钦赐了ibatis的办事处署文件和数据源。生机勃勃共配了四个,五个承担读,一个承受写。

        Ibatis总布局文件

    <?xml version="1.0" encoding="GBK"?>
    <!DOCTYPE sqlMapConfig  PUBLIC "-//iBATIS.com//DTD SQL Map
    Config 2.0/" ";

    <sqlMapConfig>
        <settings
                cacheModelsEnabled="true"
                enhancementEnabled="true"
                lazyLoadingEnabled="false"
                errorTracingEnabled="true"
                maxRequests="32"
                maxSessions="10"
                maxTransactions="5"
                useStatementNamespaces="true"/>

        <sqlMap resource="sqlmap/word.xml"/>
       
    </sqlMapConfig>

        sqlMap节点担任导入分配置文件,也正是sql文件。能够有八个。

        ibatis的sql分配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" ";
    <sqlMap namespace="word">
     
     <typeAlias alias="Word" type="com.**.im.RiskKeyWord"/>
     
        <insert id="add" parameterClass="Word">
            INSERT
            INTO key_word(lib_id, word)
            VALUES (#lib_id#, #word:VARCHAR#)
            <selectKey resultClass="int" keyProperty="id">
                select last_insert_id() limit 1
            </selectKey>
        </insert>
     
     <update id="update" parameterClass="Word">
      UPDATE key_word
      SET lib_id = #lib_id#, word = #word:VARCHAR#
      WHERE id = #id#
     </update>
     
     <select id="query" parameterClass="int" resultClass="Word">
      SELECT id, lib_id, word
      FROM key_word
      WHERE  id = #id#
     </select>
     
     <delete id="del" parameterClass="int" >
      DELETE
      FROM key_word
      WHERE  id = #id#
     </delete>
    </sqlMap>

        ibatis的分配置文件里都以sql语句。

        至此,那七个框架的搭建完毕。

    SpringMVC总结篇  http://www.linuxidc.com/Linux/2016-06/132659.htm

    Spring+SpringMVC公司连忙支付架构搭建  http://www.linuxidc.com/Linux/2015-09/122942.htm

    SpringMVC的乱码管理  http://www.linuxidc.com/Linux/2015-07/120542.htm

    Spring MVC+Spring3+Hibernate4开荒遭遇搭建 http://www.linuxidc.com/Linux/2013-07/87119.htm 

    Spring MVC整合Freemarker基于注明方式 http://www.linuxidc.com/Linux/2013-02/79660.htm 

    听他们说申明的Spring MVC简介 http://www.linuxidc.com/Linux/2012-02/54896.htm

    SpringMVC详细示例实战教程 http://www.linuxidc.com/Linux/2015-06/118461.htm

    Spring MVC 框架搭建及详整 http://www.linuxidc.com/Linux/2012-01/52740.htm

    SpringMVC 分外管理  http://www.linuxidc.com/Linux/2015-06/119049.htm

    正文永远更新链接地址:http://www.linuxidc.com/Linux/2016-10/135846.htm

    图片 2

    本文由澳门402永利com发布于计算机 网络,转载请注明出处:框架搭建,订阅模式实现配置动态变更

    关键词:

上一篇:单点安装,安装配置分布式文件系统

下一篇:没有了