发表于: 2017-06-28 13:27:07

1 930


今日完成

看了一上午师兄们的日报找思路,方向是pom web.xml properties引入memcache,然后在service层实现一个基本逻辑。

如果memcache中有数据,读取memcache。如果没有,从数据库中读取数据。

今天实现这个业务逻辑吧。

添加依赖

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

没有遇到师兄的版本问题,应该是阿里镜像的功劳

集成memcached后需要将bean序列化

序列化:就是把一个对象用二进制的表示出来,这样才能把对象写入到输出流中,用来存储或传输类似说我第一个字节表示什么属性名词,第二个字节表示什么属性值,第几个字段表示有几个属性等的意思;

对应到项目中就是说,user这个bean实现了序列化之后,才能保存在memcached缓存中,

反序列化:就是通过序列化后的字段还原成这个对象本身。但标识不被序列化的字段是不会被还原的。

如一般人员的密码信息等属性需要标识不被序列化。防止网络传输被窃取,特别是web程序。

实现序列化

实现对象的序列化有两种方式:(1)实现Serializable接口 (2)实现Externalible接口

接口仅为标记接口,不包含任何方法定义,表示实现了该接口的类可以被序列化,且实现该接口的类的所有子类都可以被序列化。而且实现了该接口的类默认为自动序列化,即对象中的所有字段都将被可序列化。

public class Student implements Serializable

项目集成memcached方法,新建MemcachedUtils工具类,新建memcached.properties和spring-memcachedxml配置文件

网上可以找到MemcachedUtils.java封装好了一些基本操作

memcached.properties

#设置Memcached服务器参数
#设置服务器地址
memcached.server=127.0.0.1:11211
#容错
memcached.failOver=true
#设置初始连接数
memcached.initConn=20
#设置最小连接数
memcached.minConn=10
#设置最大连接数
memcached.maxConn=250
#设置连接池维护线程的睡眠时间
memcached.maintSleep=3000
#设置是否使用Nagle算法(Socket的参数),如果是true在写数据时不缓冲,立即发送出去
memcached.nagle=false
#设置socket的读取等待超时时间
memcached.socketTO=3000

spring-memcached.xml

<?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:jee="http://www.springframework.org/schema/jee"
      xmlns:tx="http://www.springframework.org/schema/tx" 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.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
      default-lazy-init="false" >
<bean id="memCachedPool" class="com.whalin.MemCached.SockIOPool" factory-method="getInstance" init-method="initialize" destroy-method="shutDown" >
<constructor-arg>
<value>memCachedPool</value>
</constructor-arg>
<property name="servers">
<list>
<value>${memcached.server}</value>
</list>
</property>
<property name="initConn">
<value>${memcached.initConn}</value>
</property>

<property name="minConn">
<value>${memcached.minConn}</value>
</property>

<property name="maxConn">
<value>${memcached.maxConn}</value>
</property>

<property name="maintSleep">
<value>${memcached.maintSleep}</value>
</property>

<property name="nagle">
<value>${memcached.nagle}</value>
</property>

<property name="socketTO">
<value>${memcached.socketTO}</value>
</property>
</bean>
<bean id="memCachedClient" class="com.whalin.MemCached.MemCachedClient">
<constructor-arg>
<value>memCachedPool</value>
</constructor-arg>
</bean>
</beans>

然后StudentServiceImpl类中重新定义查找学生方法,改为两种情况,一种是当memcached缓存中存在所需数据则从缓存中取,否则从数据库取,并添加至缓存

@Service
public class StudentServiceImpl implements StudentService {
@Resource
   private StudentDao studentDao;
public List<Student> select() {
return studentDao.select();
}
public List<Student> getAllStudent() {
List<Student> listStudent = null;
if (MemcachedUtils.get("listStudent") !=null){
listStudent = (List<Student>) MemcachedUtils.get("listStudent");
System.out.print("所请求数据是从缓存中取出");
return listStudent;
}
listStudent = studentDao.select();
MemcachedUtils.add("listStudent",listStudent);
System.out.print("所请求数据是从数据库中查询取出,已添加至缓存中");
return listStudent;
}
}

然后在spring中加载spring-memcached配置文件

<import resource="classpath:conf/spring-memcached.xml"/>

记得在web.xml中添加spring-memcached.xml加载

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

java.lang.IllegalArgumentException: Could not resolve placeholder 'memcached.server' in string value "${memcached.server}"

http 503错误

网上查询应该是memcached utils调用的name和配置文件中的不一致

找实例对比下查错

收获

困难

503错误,正在排查

明日计划

改bug


返回列表 返回列表
评论

    分享到