发表于: 2017-04-14 22:46:46

2 1251


今天完成的事情:

1.模糊搜索接口,今天在使用的时候出现了点问题,当开发周期区间起始和结束同时存在的情况下,带有小数,则会报异常。经排查,在起始和结束同时存在的情况下,我做了一个验证,即起始必须小于结束才能通过。if(Integer.valueOf(cycleStart) > Integer.valueOf(cycleFinish));就是这个地方,改成Double.valueOf()判断即可。

2.今天把任务六的任务做了一些:

   1.spring+memcache集成

首先需要引入jar包:(引自胡靖日报:http://www.jnshu.com/daily/18827?uid=6838)

<dependency>
<groupId>com.danga</groupId>
<artifactId>java-memcached</artifactId>
<version>2.6.6</version>
</dependency>

将jar包下载到电脑中

然后在命令行手动添加

mvn install:install-file -Dfile=C:/Users/hujin/Desktop/java-memcached-2.6.6.jar -DgroupId=com.danga -DartifactId=java-memcached -Dversion=2.6.6 -Dpackaging=jar


memcache.properties:

memcache.server=127.0.0.1:11211
#初始连接数
memcache.initConn=20
 #最小连接数
memcache.minConn=10
 #最大连接数
memcache.maxConn=350
 #休眠时间
memcache.maintSleep=3000
# nagle算法
memcache.nagle=false
 #连接超时时间
memcache.socketTO=3000

spring-memecached.xml:

    <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance"
         init-method="initialize"    destroy-method="shutDown">
       <property name="servers">
           <list>
               <value>${memcache.server}</value>
           </list>
       </property>
       <property name="initConn">
           <value>${memcache.initConn}</value>
       </property>
       <property name="minConn">
           <value>${memcache.minConn}</value>
       </property>
       <property name="maxConn">
           <value>${memcache.maxConn}</value>
       </property>
       <property name="maintSleep">
           <value>${memcache.maintSleep}</value>
       </property>
       <property name="nagle">
           <value>${memcache.nagle}</value>
       </property>
       <property name="socketTO">
           <value>${memcache.socketTO}</value>
       </property>
   </bean>
   <bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">
   </bean>
</beans>

web.xml:(需要加上spring-memcached.xml)

<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:ApplicationContext.xml,classpath:spring-memcached.xml</param-value>
</context-param>

ApplicationContext.xml:(spring的配置文件)

<!--引入memcache的配置文件-->
<import resource="classpath:spring-memcached.xml"/>

把memcache.properties 引入:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="locations">
       <list>
           <value>classpath:property/jdbc.properties</value>
           <value>classpath:property/memcache.properties</value>
           <!--要是有多个配置文件,只需在这里继续添加即可 -->
       </list>
   </property>
</bean>

写一个MemcachedUtils工具类:(在网上搜的资料有的是创建一个memcachedService及其实现类,我感觉两者是类似的。)

public final class MemcachedUtils{
/**
    * memcached客户端单例
    */
   private static MemCachedClient cachedClient = new MemCachedClient();
   private MemcachedUtils(){
}

public static boolean add(String key, Object value) {
return cachedClient.add(key, value);
   }

public static boolean add(String key, Object value, Integer expire) {
return cachedClient.add(key, value, expire);
   }

public static boolean put(String key, Object value) {
return cachedClient.set(key, value);
   }

public static boolean put(String key, Object value, Integer expire) {
return cachedClient.set(key, value, expire);
   }

public static boolean replace(String key, Object value) {
return cachedClient.replace(key, value);
   }

public static boolean replace(String key, Object value, Integer expire) {
return cachedClient.replace(key, value, expire);
   }

public static Object get(String key) {
return cachedClient.get(key);
   }
}


一个service实现类的方法:

@Override
public List<Student> findAllStudent() {
List<Student> studentList = null;
   if (MemcachedUtils.get("studentList")!=null){
studentList = (List<Student>) MemcachedUtils.get("studentList");
       System.out.println("使用到了memcache");
       return studentList;
   }
studentList = studentMapper.findAllStudent();
   System.out.println("没有使用到memcache");
   MemcachedUtils.add("studentList",studentList);
   return  studentList;
}

结果:(可以看到第一次没有使用,第二次使用了。前提:memcache工具要打开)

   2.部署两台WEB,使用Nginx的Upstream来做负载

进入nginx.conf

upstream localhost{

server_name  localhost:8080;

server_name  192.168.75.128:8080;

}

    server {

        listen       80;

        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

location / {

   proxy_pass http://localhost;

}

明天计划的事情:

1.项目中新增和编辑接口有问题,还不知道什么原因。

1.学习redis

遇到的问题:

1.如果只有一个memcached的jar包有可能会报错,

Caused by: java.lang.NoClassDefFoundError: org/apache/commons/pool/BasePoolableObjectFactory

在网上搜索说需要把common-pool的包也要导入,即可。

收获:




返回列表 返回列表
评论

    分享到