数据采集系统(数据调查)学习总结(五)

        开始我们说分表,分库能有效降低数据库的存储,访问压力,这个没错,但这只能是治标不治本,什么是本,本就是减少对数据库的访问,还不能降低用户量,也才是最终目标,那如何达到这个目标那,我们分析下权限列表是不是每次都要访问数据库,于是我们用存入application中的方法来应对的,那我们每张调查无数人用,是不是也没有必要每次都查询数据库吧,我们只需在用户有了存入操作后才刷新调查不就行了,这样就有效的减少了访问量,于是我们引入缓存思想。

        一般通过框架我们最常使用的就是hibernate的二级缓存功能,所谓二级缓存,就是在内存中分出不同的缓存区,主要有:类缓存,集合缓存,更新时间戳缓存(每张表最后的更新时间),以及查询缓存。我们根据项目需要,开启不同的缓存区,比如查询缓存区,但是二级缓存在session中时一直有效的,无疑增大了session的压力,我们知道user和权限列表以及一些参数也经常放到session中,所以基于hibernate的二级缓存粒度较细,我们不采用这种优化。

        除了hibernate的缓存机制,其实spring框架也有缓存模块,相对他的粒度粗,便于控制,那我们就用它来减少数据库的访问,以下总结加载流程:

集成spring的缓存模块(子项目)实现对service的缓存代理,降低数据负载压力.
--------------------------------------------------------------
1.引入类库
    spring-modules-cache.jar
 
    [第三方缓存供应商]
    backport-util-concurrent.jar
    com.springsource.org.apache.commons.logging-1.1.1.jar
    ehcache-1.5.0.jar
    jsr107cache-1.1.jar
2.配置ehcache配置文件,指定缓存的过期策略. (hibernate中也有相应的配置)
    [conf/ehcache.xml]
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
        <diskStore path="java.io.tmpdir"/>
        <defaultCache
                maxElementsInMemory="10000"
                eternal="false"
                timeToIdleSeconds="120"
                timeToLiveSeconds="120"
                overflowToDisk="true"
                maxElementsOnDisk="10000000"
                diskPersistent="false"
                diskExpiryThreadIntervalSeconds="120"
                memoryStoreEvictionPolicy="LRU"
                />
        <cache name="surveyCache"
                maxElementsInMemory="10000"
                eternal="false"
                timeToIdleSeconds="120"
                timeToLiveSeconds="120"
                overflowToDisk="true"
                maxElementsOnDisk="10000000"
                diskPersistent="false"
                diskExpiryThreadIntervalSeconds="120"
                memoryStoreEvictionPolicy="LRU"
                />
    </ehcache>
3.配置spring的缓存模块的配置文件
    [conf/service-spring-cache.xml]
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
        xsi:schemaLocation="http://www.springframework.org/schema/beans  
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
                            http://www.springmodules.org/schema/ehcache
                            http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd">
        <!-- 指定ehcache的配置文件 -->
        <ehcache:config configLocation="classpath:ehcache.xml"/>
        
        <!-- 配置缓存代理 --> (这里我们将查询的方法开启缓存机制,执行surveyCache的xml配置,将写操作定义清除缓存,保证信息实时性。因此我们说之前项目开发制定相同方法动作前缀,有利于更好的控制方法)
        <ehcache:proxy id="surveyServiceCacheProxy" refId="surveyService">
            <!-- 缓存数据 -->
            <ehcache:caching methodName="get*" cacheName="surveyCache"/>
            <ehcache:caching methodName="load*" cacheName="surveyCache"/>
            <ehcache:caching methodName="find*" cacheName="surveyCache"/>
            <ehcache:caching methodName="is*" cacheName="surveyCache"/>
            
            <!-- 清理缓存 -->
            <ehcache:flushing methodName="save*" cacheNames="surveyCache" />
            <ehcache:flushing methodName="update*" cacheNames="surveyCache" />
            <ehcache:flushing methodName="delete*" cacheNames="surveyCache" />
            <ehcache:flushing methodName="batch*" cacheNames="surveyCache" />
        </ehcache:proxy>
    </beans>
4.将service缓存代理注入给action (对要开启缓存的模块加入缓存代理,这里提一下,缓存代理在哪个位置加入合适,我们要实现的是当相同方法查询时,直接调用缓存内容,那为避免不必要数据,我们就在action和service中间,日志,事务代理之前加入缓存代理,这样就达到目的)
    public class SurveyAction ...{
        ...
        //注入缓存代理
        @Resource(name="surveyServiceCacheProxy")
        private SurveyService surveyService ;
    }
 
5.配置web.xml文件,一同加载spring缓存配置文件.
    <!-- 通过上下文参数指定spring配置文件的位置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:beans.xml,classpath:schedules.xml,classpath:service-spring-cache.xml</param-value>
    </context-param>

到这里,我们就可以启动项目查看效果,至于怎呒看出来有缓存那,那就是我们之前hibernate的show_sql功能了,反复查看,看看是否生成新的sql语句即可。

下面我提一下后面开发的远程调用功能,以及统一接口公开的方法,我们使用了webservice框架,稍后我们更新我对webservice的学习,他的好处就是可以跨平台,跨语言,适于分布式系统。我们常用的股票查询,天气预报查询接口,大部分主程序都是基于它开发的。

spring远程调用:
------------------------
-----------server-----------
1.创建web项目
2.添加spring类库
    org.springframework.aop-3.1.0.RELEASE.jar
    org.springframework.asm-3.1.0.RELEASE.jar
    org.springframework.beans-3.1.0.RELEASE.jar
    org.springframework.context-3.1.0.RELEASE.jar
    org.springframework.context.support-3.1.0.RELEASE.jar
    org.springframework.core-3.1.0.RELEASE.jar
    org.springframework.web-3.1.0.RELEASE.jar
    org.springframework.web.servlet-3.1.0.RELEASE.jar
    org.springframework.expression-3.1.0.RELEASE.jar
 
    com.springsource.org.aopalliance-1.0.0.jar
    com.springsource.org.apache.commons.logging-1.1.1.jar
3.创建接口和实现类
    public interface WelcomeService {
        public void sayHello(String name);
    }
 
    public class WelcomeServiceImpl implements WelcomeService {
        public void sayHello(String name) {
            System.out.println(name);
        }
    }
4.配置spring配置文件.
    [web-inf/${servler-name}-servlet.xml]
    <?xml version="1.0"?>
    <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"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:cache="http://www.springframework.org/schema/cache"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd ">
        
        <!-- pojo -->
        <bean id="welcomeService" class="cn.itcast.spring.rpc.service.WelcomeServiceImpl" />
        
        <!-- 导出器,将pojo对象转变成controller,处理请求 -->
        <bean name="/ws.service" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
            <property name="serviceInterface">
                <value>cn.itcast.spring.rpc.service.WelcomeService</value>
            </property>
            <property name="service" ref="welcomeService" />
        </bean>
    </beans>
 
-----------client-----------
5.创建java项目
6.引入类库
    同服务端.
7.复制服务端接口到客户端. (最简单的方法就是客户端项目buildSourcce ->addproject将服务器加载进来)
8.创建src/client.xml spring配置文件
    <?xml version="1.0"?>
    <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"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:cache="http://www.springframework.org/schema/cache"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd ">
        <!-- 客户端代理 -->
        <bean id="wsClient" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
            <property name="serviceUrl">
                <value>http://localhost:8080/lsn_springrpc_0909_server/ws.service</value>
            </property>
            <property name="serviceInterface">
                <value>cn.itcast.spring.rpc.service.WelcomeService</value>
            </property>
        </bean>
    </beans>
9.创建测试类
    public class App {
        public static void main(String[] args) {
            ApplicationContext ac = new ClassPathXmlApplicationContext("client.xml");
            WelcomeService ws = (WelcomeService) ac.getBean("wsClient");
            ws.sayHello("tom");
        }
    }
10.运行
    先启动服务器端
    在启动客户端.
 
将项目中的统计服务对外公开,供第三方系统整合
-------------------------------------------
--------- server ---------
1.引入类库
    org.springframework.web.servlet-3.1.0.RELEASE.jar
2.配置web.xml文件DispatcherServlet
    <!--  配置spring远程调用使用的分发器servlet -->
    <servlet>
        <servlet-name>service</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:service-spring-http-inovker.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>service</servlet-name>
        <url-pattern>/*.service</url-pattern>
    </servlet-mapping>
3.配置spring远程调用配置文件
    [conf/service-spring-http-invoker.xml]
    <?xml version="1.0"?>
    <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"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:cache="http://www.springframework.org/schema/cache"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd ">
        
        <!-- 远程调用统计服务类 -->
        <bean name="/ss.service" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
            <property name="serviceInterface" value="cn.itcast.surveypark.service.StatisticsService" />
            <property name="service" ref="statisticsService" />
        </bean>
    </beans>
 
-------  client  ----------
4.添加客户端配置bean.
    <!-- 客户端代理 -->
    <bean id="ssClient" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
        <property name="serviceUrl">
            <value>http://localhost:8000/lsn_surveypark0909/ss.service</value>
        </property>
        <property name="serviceInterface">
            <value>cn.itcast.surveypark.service.StatisticsService</value>
        </property>
    </bean>
5.测试

以上就是我对数据调查系统的学习总结,其中还有很多知识点没有提及,比如开始做的sso单点登录功能等,写这呒多只是让自己好好回顾以下学习过的东西,感觉自己老了,真的是几天不学就都忘了。引鉴。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值