Skip to content

Commit

Permalink
dubbo+zk 分块化
Browse files Browse the repository at this point in the history
  • Loading branch information
qiurunze committed Feb 15, 2019
1 parent cc2860a commit 8e9f742
Show file tree
Hide file tree
Showing 527 changed files with 201,852 additions and 310 deletions.
2 changes: 1 addition & 1 deletion docs/code-solve.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
#### [为什么threadlocal存储user对象,原理]()
1.并发编程中重要的问题就是数据共享,当你在一个线程中改变任意属性时,所有的线程都会因此受到影响,同时会看到第一个线程修改后的值<br>
有时我们希望如此,比如:多个线程增大或减小同一个计数器变量<br>
但是,有时我们希望确保每个线程,只能工作在它自己的线程实例的拷贝上,同时不会影响其他线程的数据<br>
但是,有时我们希望确保每个线程,只能工作在它自己 的线程实例的拷贝上,同时不会影响其他线程的数据<br>

举例: 举个例子,想象你在开发一个电子商务应用,你需要为每一个控制器处理的顾客请求,生成一个唯一的事务ID,同时将其传到管理器或DAO的业务方法中,
以便记录日志。一种方案是将事务ID作为一个参数,传到所有的业务方法中。但这并不是一个好的方案,它会使代码变得冗余。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public enum ResultStatus {
PASSWORD_EMPTY (30006,"登录密码不能为空!"),
MOBILE_EMPTY (30007,"手机号不能为空!"),
MOBILE_ERROR (30008,"手机号格式错误!"),
MOBILE_NOT_EXIST (30009,"手机号不存在!"),
MOBILE_NOT_EXIST (30009,"账号不存在!"),
PASSWORD_ERROR (30010,"密码错误!"),
USER_NOT_EXIST(30011,"用户不存在!"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public static void setUser(MiaoshaUser user) {
}

public static MiaoshaUser getUser() {

return userHolder.get();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.geekq.miasha.utils;

import com.geekq.miasha.entity.Logininfo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
* @author 邱润泽
*
* thread local 底层实现方法 和 UserContext 类似 本质上都是 每个线程工作都在自己的实例线程上拷贝
*/
public class UserContext2 {

public static final String LOGIN_IN_SESSION = "logininfo";

private static HttpServletRequest getRequest() {
return ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();
}

public static void putLogininfo(Logininfo logininfo) {
getRequest().getSession().setAttribute(LOGIN_IN_SESSION, logininfo);
}

public static Logininfo getCurrent() {
return (Logininfo) getRequest().getSession().getAttribute(
LOGIN_IN_SESSION);
}

}
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
package com.geekq.miasha.vo;

import com.geekq.miasha.validator.MobileCheck;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotNull;

@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class LoginVo {
@NotNull
@MobileCheck
private String mobile ;
private String nickname ;

@NotNull
@Length(min=32)
private String password;

public String getNickname() {
return nickname;
}

public void setNickname(String nickname) {
this.nickname = nickname;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

@Override
public String toString() {
return "LoginVo{" +
"mobile='" + mobile + '\'' +
"nickname='" + nickname + '\'' +
", password='" + password + '\'' +
'}';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface MiaoShaUserMapper {

public void insertMiaoShaUser(MiaoshaUser miaoshaUser);

public int getCountByUserName(@Param("userName")String userName , @Param("userType")int userType);

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@
<update id="update" parameterType="miaoshauser" >
update miaosha_user set password = #{password} where id = #{id}
</update>

<select id="getCountByUserName" resultType="int" >
select count(*)
from miaosha_user where nickname = #{userName}
</select>

</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@
import com.geekq.miaosha.redis.RedisService;
import com.geekq.miasha.entity.IpLog;
import com.geekq.miasha.entity.MiaoshaUser;
import com.geekq.miasha.enums.Constants;
import com.geekq.miasha.enums.MessageStatus;
import com.geekq.miasha.exception.GlobleException;
import com.geekq.miasha.utils.MD5Utils;
import com.geekq.miasha.utils.SnowflakeIdWorker;
import com.geekq.miasha.utils.UUIDUtil;
import com.geekq.miasha.vo.LoginVo;
import com.geekq.miasha.vo.MiaoShaMessageVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -25,9 +21,8 @@
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

import static com.geekq.miasha.enums.enums.ResultStatus.MOBILE_NOT_EXIST;
import static com.geekq.miasha.enums.enums.ResultStatus.PASSWORD_ERROR;
import static com.geekq.miasha.enums.enums.ResultStatus.SYSTEM_ERROR;
import static com.geekq.miasha.enums.Constants.USERTYPE_NORMAL;
import static com.geekq.miasha.enums.enums.ResultStatus.*;


@Service
Expand All @@ -46,6 +41,11 @@ public class MiaoShaUserService {
private MQSender sender ;


public boolean getNickNameCount(String userName){

return miaoShaUserMapper.getCountByUserName(userName,USERTYPE_NORMAL) <=0;

}
public MiaoshaUser getByToken(HttpServletResponse response , String token) {

if(StringUtils.isEmpty(token)){
Expand Down Expand Up @@ -94,19 +94,21 @@ public boolean updatePassword(String token, String nickName, String formPass) {
}


public boolean register(String userName , String passWord , String salt ,
public boolean register(String userName , String passWord ,
HttpServletResponse response , HttpServletRequest request) {
MiaoshaUser miaoShaUser = new MiaoshaUser();
miaoShaUser.setNickname(userName);
String DBPassWord = MD5Utils.formPassToDBPass(passWord , MD5Utils.getSaltT());
//password 应该在前段进行一次MD5 在后端在进行一个MD5 在入库
String salt = MD5Utils.getSaltT();
String DBPassWord = MD5Utils.formPassToDBPass(passWord ,salt);
miaoShaUser.setPassword(DBPassWord);
miaoShaUser.setRegisterDate(new Date());
miaoShaUser.setSalt(salt);
miaoShaUser.setNickname(userName);
try {
miaoShaUserMapper.insertMiaoShaUser(miaoShaUser);
IpLog log = new IpLog(userName,new Date(),request.getRemoteAddr(),
Constants.USERTYPE_NORMAL,null);
USERTYPE_NORMAL,null);

MiaoshaUser user = miaoShaUserMapper.getByNickname(miaoShaUser.getNickname());
if(user == null){
Expand All @@ -129,7 +131,7 @@ public boolean login(HttpServletResponse response , LoginVo loginVo) {
throw new GlobleException(SYSTEM_ERROR);
}

String mobile =loginVo.getMobile();
String mobile =loginVo.getNickname();
String password =loginVo.getPassword();
MiaoshaUser user = getByNickName(mobile);
if(user == null) {
Expand All @@ -139,7 +141,7 @@ public boolean login(HttpServletResponse response , LoginVo loginVo) {
String dbPass = user.getPassword();
String saltDb = user.getSalt();
String calcPass = MD5Utils.formPassToDBPass(password,saltDb);
if(!"b7797cce01b4b131b433b6acf4add449".equals(dbPass)){
if(!calcPass.equals(dbPass)){
throw new GlobleException(PASSWORD_ERROR);
}
//生成cookie 将session返回游览器 分布式session
Expand All @@ -156,7 +158,7 @@ public String createToken(HttpServletResponse response , LoginVo loginVo) {
throw new GlobleException(SYSTEM_ERROR);
}

String mobile =loginVo.getMobile();
String mobile =loginVo.getNickname();
String password =loginVo.getPassword();
MiaoshaUser user = getByNickName(mobile);
if(user == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.geekq.miaosha;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableDubbo
@SpringBootApplication
@MapperScan("com.geekq.miaosha.mapper")
public class GeekQMainApplication {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.geekq.miaosha.config;

import com.geekq.miaosha.access.AccessInterceptor;
import com.geekq.miaosha.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
Expand All @@ -16,12 +16,14 @@ public class WebConfig extends WebMvcConfigurerAdapter {
UserArgumentResolver resolver;

@Autowired
private AccessInterceptor interceptor;
private LoginInterceptor interceptor;

final String[] notLoginInterceptPaths = {"/do_login/**"};
@Override
public void addInterceptors(InterceptorRegistry registry) {
super.addInterceptors(registry);
registry.addInterceptor(interceptor);
registry.addInterceptor(interceptor).addPathPatterns("/**").excludePathPatterns(notLoginInterceptPaths);
// registry.addInterceptor(interceptor);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class BaseController {
@Autowired
RedisService redisService;


public String render(HttpServletRequest request, HttpServletResponse response, Model model, String tplName, KeyPrefix prefix, String key) {
if(!pageCacheEnable) {
return tplName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.geekq.miaosha.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.geekq.miaosha.redis.GoodsKey;
import com.geekq.miaosha.redis.RedisService;
import com.geekq.miaosha.service.GoodsService;
Expand Down Expand Up @@ -39,6 +40,9 @@ public class GoodsController extends BaseController {
@Autowired
private GoodsService goodsService;

@Reference(version = "${demo.service.version}")
private com.geekq.api.service.GoodsService goodsServiceRpc;

@Autowired
ThymeleafViewResolver viewResolver;

Expand All @@ -54,6 +58,7 @@ public class GoodsController extends BaseController {
@ResponseBody
public String list(HttpServletRequest request, HttpServletResponse response, Model model, MiaoshaUser user) {
model.addAttribute("user", user);
// ResultGeekQ<List<com.geekq.api.entity.GoodsVo>> goodsList1 = goodsServiceRpc.listGoodsVo();
List<GoodsVo> goodsList = goodsService.listGoodsVo();
model.addAttribute("goodsList", goodsList);
return render(request,response,model,"goods_list", GoodsKey.getGoodsList,"");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.geekq.miaosha.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.geekq.api.service.DemoService;
import com.geekq.miaosha.redis.redismanager.RedisLua;
import com.geekq.miaosha.service.MiaoShaUserService;
import com.geekq.miasha.enums.resultbean.ResultGeekQ;
Expand All @@ -9,6 +12,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

Expand All @@ -25,6 +29,19 @@ public class LoginController {

@Autowired
private MiaoShaUserService userService;

@Reference(version = "${demo.service.version}")
private DemoService demoService;

@RequestMapping("/sayHello/{name}")
@ResponseBody
public String sayHello(@PathVariable("name") String name) {

return demoService.sayHello(name);
}



@RequestMapping("/to_login")
public String tologin(LoginVo loginVo, Model model) {
logger.info(loginVo.toString());
Expand All @@ -36,10 +53,10 @@ public String tologin(LoginVo loginVo, Model model) {
return "login";
}

@RequestMapping("/do_login")
@RequestMapping("/loginin")
@ResponseBody
public ResultGeekQ<Boolean> dologin(HttpServletResponse response, @Valid LoginVo loginVo) {
ResultGeekQ<Boolean> result = ResultGeekQ.build();
public ResultGeekQ<String> dologin(HttpServletResponse response, @Valid LoginVo loginVo) {
ResultGeekQ<String> result = ResultGeekQ.build();
logger.info(loginVo.toString());
userService.login(response, loginVo);
return result;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.geekq.miaosha.controller;

import com.geekq.miaosha.access.AccessLimit;
import com.geekq.miaosha.interceptor.RequireLogin;
import com.geekq.miaosha.rabbitmq.MQSender;
import com.geekq.miaosha.rabbitmq.MiaoshaMessage;
import com.geekq.miaosha.redis.GoodsKey;
Expand Down Expand Up @@ -63,7 +63,7 @@ public class MiaoshaController implements InitializingBean {
* 5000 * 10
* get post get 幂等 从服务端获取数据 不会产生影响  post 对服务端产生变化
*/
@AccessLimit(seconds = 5, maxCount = 5, needLogin = true)
@RequireLogin(seconds = 5, maxCount = 5, needLogin = true)
@RequestMapping(value="/{path}/do_miaosha", method= RequestMethod.POST)
@ResponseBody
public ResultGeekQ<Integer> miaosha(Model model, MiaoshaUser user, @PathVariable("path") String path,
Expand Down Expand Up @@ -121,7 +121,7 @@ public ResultGeekQ<Integer> miaosha(Model model, MiaoshaUser user, @PathVariable
* -1:秒杀失败
* 0: 排队中
*/
@AccessLimit(seconds = 5, maxCount = 5, needLogin = true)
@RequireLogin(seconds = 5, maxCount = 5, needLogin = true)
@RequestMapping(value = "/result", method = RequestMethod.GET)
@ResponseBody
public ResultGeekQ<Long> miaoshaResult(Model model, MiaoshaUser user,
Expand All @@ -137,7 +137,7 @@ public ResultGeekQ<Long> miaoshaResult(Model model, MiaoshaUser user,
return result;
}

@AccessLimit(seconds = 5, maxCount = 5, needLogin = true)
@RequireLogin(seconds = 5, maxCount = 5, needLogin = true)
@RequestMapping(value = "/path", method = RequestMethod.GET)
@ResponseBody
public ResultGeekQ<String> getMiaoshaPath(HttpServletRequest request, MiaoshaUser user,
Expand Down
Loading

0 comments on commit 8e9f742

Please sign in to comment.