GitHub地址:https://github.com/vicotorz/zhangdizhihu
开发流程:DataBase-->Model-->Dao-->Service-->Controller-->Test
SQL 建表语句:
评论:comment
id int(11),
content text,
user_id int(11),
entity_id int(11),
entity_type int(11),
created_date datetime,
status int(1)index 'entity-index' ('entity-id' ASC,'entity-type' ASC)
feed流:feed流
id int(11),
created_date datetime,
user_id int(11),
data tinytext,
type int(11)index 'user_index' ('user_id' ASC)
登陆状态:login_ticket
id int(11),
user_id int(11),
ticket varchar(45),
expired datetime,
status int(11)
消息:message
id int(11),
from_id int(11),
to_id int(11),
content text,
created_date datetime,
has_read int(11),
conversation_id varchar(45)index 'conversation_index' ('conversation_id' ASC)
index 'created_date' ('created_date' ASC)
问题:question
id int(11),
title varchar(255),
content text,
user_id int(11),
created_date datetime,
comment_count int(11)index 'date_index' ('created_date' ASC)
用户: user
id int(11),
name varchar(64),
password varchar(128),
salt varchar(32),
head_url varchar(56),primary key('id')
unique key 'name' ('name')
MyBatis:
- application.properties增加数据库连接等配置
- pom.xml引入my-spring-boot-starter、mysql-connector-java
- mybatis-config.xml新增mybatis使用规则
- 复杂的sql写DAO.xml
Fastjson:
jsonObject 传入到Ajax的js中,例如unfllow的json,传入到detail.js中
Redis:
数据结构 | 指令 |
List | lpush(异步队列放入事件)、lpop、blpop、 lindex、lrange、lindex、lrem、linsert、 lset、rpush |
Set | sdiff、smembers(获取状态)、sinter、scard(点赞)、 srem(消除踩)、sadd |
SortedSet | zadd(添加关注)、zscore、zrange(获取粉丝)、 zcount、zrank、zrevrank、zrem(取消关注) |
Hash | hset、hget、hgetAll、hexists、hkeys、hvals |
异步框架:
存入格式:EVENTQUEUE:
{actorId:12, entityId:6910,entityOwnerId:12,entityType:1,exts:{},type:FLLOW}
代码结构:
//fireEvent
public boolean fireEvent(EventModal eventModal){
String json = JSONObject.toJSONString(eventModal);
String key = RedisKeyUtil.getEventQueuekey();
jedisAdapter.lpush(key,json);
return true;
}
EventModel{
EventType type;//Event类型
actorId;//执行用户id
entityType;//entity类型
entityId;//entity id
entityOwnerId;//entity 拥有者id
}
Event Consumer方法:
afterPropertiesSet把一个Handler关心的事件放在config
Map<String,EventHandler> beans = applicationContext.getBeansOfType(EventHandler.class);
- beans内容格式为:(addHandler,com.zhihu.async.handler.AddQuestionHandler)
- 取beans.getValue().getSupportEventTypes()内容
- 添加ADD_QUESTION,com.zhihu.async.handler.AddQuestionHandler
开启线程把优先队列的事件取出来,还原Event
- Thread一直从JedisPool中取有EVENTQUEUE的事件,若没有EVENT_QUEUE字样,表明是新事件
- 还原EVNET JSON.parseobject(message,EventModel.class)
- 若config中没有字样,不能识别
- for(EventHandler handler = config.get(eventModel.getType())) handler.dohandle(eventModel);
在config中找谁处理这个EventHandler
用户登陆流程:
- StringUtils.isBlank
- 生成用户token,加盐:UUID.randomUUID();
- 密码 + 盐 的MD5加密
- 设置有效期expired
记录登陆状态:
Cookie cookie = new Cookie("ticket",map.get("ticket"));
cookie.setPath("/");
response.addCookie(cookie);
return "redirect:/";
敏感词内部:{五={星={红={旗={isEnd=1},isEnd=0},isEnd=0},isEnd=0},isEnd=0}
拦截器:链路回调思想,拦截HttpServletRequest
@Component,implements HandlerInterceptor:preHandler、postHandler、afterCompletion
定义Hostholder(使用ThreadLocal)
extends WebMvcConfiguration:注册定义好的拦截器
WendaConfiguration extends WebMvcConfiguraAdapter{
@Autowired
void addInterceptors(InterceptorRefistry registry){
registry.addInterceptor(...).addPathPatterns(...);
}
}
Timeline功能:(1)推-redis (2)拉-数据库
代码涉及:EventHandler、FeedDao、Service、Modal、Controller、Redis 队列存储
- 触发事件后放入异步队列
- 调用FeedDao存入
- 把这个事件推给所有粉丝,并放入到RedisAdapter中
- 显示:从Jedis中取feed(lrange),若feed在数据库中,则加入显示
邮件功能:
- 开启pop3 / STMP 服务
- 加入Java mail依赖
- 编写实现类:JavaMailSenderImpl(设置name,password,Host,Port,Protocal,encoding)
- 调用:mailSender,sendWithHTMLTemplate(model.getExt("email"),"...",模板)
Solr:
solr_url = "http://...";
HttpSolrClient client = new HttpSolrClient.Builder(solr_url).build();
//查询
SolrQuery query = new SolrQuery(keywrod);
QueryResponse response = client.query(query);
//建索引
SolrInputDocument doc = new SolrInputDocument();
doc.setField("","");
UpdateResponse response = client(doc,...);
七牛云SDK上传:
- 设置七牛keys
- 设置上传空间:buketname
- 秘钥配置:Auth auth = Auth.create(ACCESS_KEY,SECRET_KEY);
- 设置上传对象:UploadManager uploadManager = new UploadManager();
- 设置七牛域名
- 开始上传:auth.uploadToken(bucketname);
- 上传图片(使用到MultipartFile)