网约车项目学习笔记

编程入门 行业动态 更新时间:2024-10-16 02:30:57

网约车项目<a href=https://www.elefans.com/category/jswz/34/1770117.html style=学习笔记"/>

网约车项目学习笔记

网约车项目学习笔记

本文章只是网约车项目的学习笔记。刚开始学习,后续更新项目内容分析。

1.先是Eureka服务器

基本没有配什么  只是起了一个服务注册,然后配了个断开监听 还有一个安全服务。

断开监听   在application.yml  设置了关闭自我保护  设置了检查断开为5秒。

package com.online.taxi.eureka.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
/*** @author yueyi2019*/
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {// 关闭csrf
//		http.csrf().disable(); /** 默认情况下添加SpringSecurity依赖的应用每个请求都需要添加CSRF token才能访问,Eureka客户端注册时并不会添加,所以需要配置/eureka/**路径不需要CSRF token。*/http.csrf().ignoringAntMatchers("/eureka/**");// 开启认证支持HttpBasichttp.authorizeRequests().anyRequest().authenticated().and().httpBasic(); }
}

 

package com.online.taxi.eureka;import org.springframework.cloudflix.eureka.server.event.EurekaInstanceCanceledEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;@Component
public class CustomEvent {@EventListenerpublic void listen(EurekaInstanceCanceledEvent e) {System.out.println(e.getServerId()+"下线事件");//发送邮件,短信,电话。}
}

2.Ribben服务器

当导入这个项目的时候首先这个项目会报错,因为有个依赖无法从maven下载。(当然无法下载了。。我们又没有他的本地maven库)  还好他提供了这个项目,将common这个项目打包

然后引入该项目中  就不会报错了。还有一些get方法报错  这个没什么大问题,这里他用的是注解生成的,idea没有相关插件的话调用该方法会报红。一般我自己用的是idea的自动生成。

		<!-- 引用自定义通用组件 -->
<!--		<dependency>-->
<!--			<groupId>com.online.taximon</groupId>-->
<!--			<artifactId>online-taxi-common</artifactId>-->
<!--			<version>0.0.1-SNAPSHOT</version>-->
<!--		</dependency>-->

然后ribben服务器就没什么说的了。。。里面重写了一部分轮询逻辑,写了短信验证等,大部分逻辑已经删除,应该是保密需要,也可能是这几个方法是预留方法。也可能使我太菜了没看懂,重在学习嘛。

3.订单服务器

这个项目的核心(应该是)

数据库部分就不说了  只说锁部分。

本来我预想的很复杂,后来想了想订单号和车是一对一的,不会有那么高的并发。。同一个订单最多也就100个去抢。。

package com.online.taxi.order.controller;import com.online.taximon.dto.BaseResponse;
import com.online.taximon.dto.ResponseResult;
import com.online.taxi.order.service.GrabService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;/*** @author yueyi2019*/
@RestController
@RequestMapping("/grab")
public class GrabOrderController {@Autowired// 无锁@Qualifier("grabNoLockService")// jvm锁
//    @Qualifier("grabJvmLockService")// mysql锁
//    @Qualifier("grabMysqlLockService")// 单个redis
//    @Qualifier("grabRedisLockService")//单个redisson
//    @Qualifier("grabRedisRedissonService")    // 红锁
//    @Qualifier("grabRedisRedissonRedLockLockService")private GrabService grabService;@GetMapping("/do/{orderId}")public String grab(@PathVariable("orderId") int orderId, int driverId){grabService.grabOrder(orderId,driverId);ResponseResult.success(new BaseResponse());return "";}
}

 

通过上面的代码可以看出,他设计了很多种锁,接下来我说一说我对这几种锁的理解。

jvm锁 

这个没啥说的  直接用String记个单号  然后用synchronized锁上,谁抢到就是谁的,因为synchronized会自动释放锁,也不用担心锁回收什么的。

mysql锁 

package com.online.taxi.order.service.impl;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import com.online.taximon.dto.ResponseResult;
import com.online.taximon.entity.OrderLock;
import com.online.taxi.order.lock.MysqlLock;
import com.online.taxi.order.service.GrabService;
import com.online.taxi.order.service.OrderService;/*** @author yueyi2019*/
@Service("grabMysqlLockService")
public class GrabMysqlLockServiceImpl implements GrabService {@Autowiredprivate MysqlLock lock;@AutowiredOrderService orderService;ThreadLocal<OrderLock> orderLock = new ThreadLocal<>();@Overridepublic ResponseResult grabOrder(int orderId , int driverId){//生成keyOrderLock ol = new OrderLock();ol.setOrderId(orderId);ol.setDriverId(driverId);orderLock.set(ol);lock.setOrderLockThreadLocal(orderLock);lock.lock();
//        System.out.println("司机"+driverId+"加锁成功");try {System.out.println("司机:"+driverId+" 执行抢单逻辑");boolean b = orderService.grab(orderId, driverId);if(b) {System.out.println("司机:"+driverId+" 抢单成功");}else {System.out.println("司机:"+driverId+" 抢单失败");}} finally {lock.unlock();}return null;}
}
package com.online.taxi.order.lock;import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;import com.online.taximon.entity.OrderLock;
import com.online.taxi.order.dao.OrderLockMapper;import lombok.Data;
import lombok.Getter;
import lombok.Setter;@Service
@Data
public class MysqlLock implements Lock {@Autowiredprivate OrderLockMapper mapper;private ThreadLocal<OrderLock> orderLockThreadLocal ;@Overridepublic void lock() {// 1、尝试加锁if(tryLock()) {return;}// 2.休眠try {Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}// 3.递归再次调用lock();}/*** 	非阻塞式加锁,成功,就成功,失败就失败。直接返回*/@Overridepublic boolean tryLock() {try {mapper.insertSelective(orderLockThreadLocal.get());System.out.println("加锁对象:"+orderLockThreadLocal.get());return true;}catch (Exception e) {return false;}}@Overridepublic void unlock() {mapper.deleteByPrimaryKey(orderLockThreadLocal.get().getOrderId());System.out.println("解锁对象:"+orderLockThreadLocal.get());orderLockThreadLocal.remove();}@Overridepublic void lockInterruptibly() throws InterruptedException {// TODO Auto-generated method stub}@Overridepublic boolean tryLock(long time, TimeUnit unit) throws InterruptedException {// TODO Auto-generated method stubreturn false;}@Overridepublic Condition newCondition() {// TODO Auto-generated method stubreturn null;}}

用两个表去维护锁。

表一用来记录订单各种信息。

表二用来表示锁,具体原理如下

<insert id="insertSelective" parameterType="com.online.taximon.entity.OrderLock" >insert into tbl_order_lock<trim prefix="(" suffix=")" suffixOverrides="," ><if test="orderId != null" >order_id,</if><if test="driverId != null" >driver_id,</if></trim><trim prefix="values (" suffix=")" suffixOverrides="," ><if test="orderId != null" >#{orderId},</if><if test="driverId != null" >#{driverId},</if></trim></insert>

用版本号去实现也一样。

无锁

无锁不提了。

redis锁

package com.online.taxi.order.service.impl;import java.util.concurrent.TimeUnit;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;import com.online.taximon.dto.ResponseResult;
import com.online.taximon.entity.OrderLock;
import com.online.taxi.order.lock.MysqlLock;
import com.online.taxi.order.lock.RedisLock;
import com.online.taxi.order.service.GrabService;
import com.online.taxi.order.service.OrderService;/*** @author yueyi2019*/
@Service("grabRedisLockService")
public class GrabRedisLockServiceImpl implements GrabService {@AutowiredStringRedisTemplate stringRedisTemplate;@AutowiredOrderService orderService;@Overridepublic ResponseResult grabOrder(int orderId , int driverId){//生成keyString lock = "order_"+(orderId+"");/**  情况一,如果锁没执行到释放,比如业务逻辑执行一半,运维重启服务,或 服务器挂了,没走 finally,怎么办?*  加超时时间*/
//    	boolean lockStatus = stringRedisTemplate.opsForValue().setIfAbsent(lock.intern(), driverId+"");
//    	if(!lockStatus) {
//    		return null;
//    	}/**  情况二:加超时时间,会有加不上的情况,运维重启*/
//    	boolean lockStatus = stringRedisTemplate.opsForValue().setIfAbsent(lock.intern(), driverId+"");
//    	stringRedisTemplate.expire(lock.intern(), 30L, TimeUnit.SECONDS);
//    	if(!lockStatus) {
//    		return null;
//    	}/** 情况三:超时时间应该一次加,不应该分2行代码,* */boolean lockStatus = stringRedisTemplate.opsForValue().setIfAbsent(lock.intern(), driverId+"", 30L, TimeUnit.SECONDS);if(!lockStatus) {return null;}try {System.out.println("司机:"+driverId+" 执行抢单逻辑");boolean b = orderService.grab(orderId, driverId);if(b) {System.out.println("司机:"+driverId+" 抢单成功");}else {System.out.println("司机:"+driverId+" 抢单失败");}} finally {/*** 这种释放锁有,可能释放了别人的锁。*/
//        	stringRedisTemplate.delete(lock.intern());/*** 下面代码避免释放别人的锁*/if((driverId+"").equals(stringRedisTemplate.opsForValue().get(lock.intern()))) {stringRedisTemplate.delete(lock.intern());}}return null;}
}

因为是一对一,不存在什么超买超卖问题,就真给锁上就行。

超买超卖问题

(自己感觉可以这么处理)

redis初筛一次,在初筛后面加上mysql版本号自旋。

或者redis队列,之后剪一下,剪到固定长度,然后入库。

redisson版本

redis官方推荐的分布式锁java框架。里面有各种锁的实现,下面的实现简单来说就是把id锁上,然后处理。

package com.online.taxi.order.service.impl;import java.util.concurrent.TimeUnit;import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;import com.online.taximon.dto.ResponseResult;
import com.online.taximon.entity.OrderLock;
import com.online.taxi.order.lock.MysqlLock;
import com.online.taxi.order.lock.RedisLock;
import com.online.taxi.order.service.GrabService;
import com.online.taxi.order.service.OrderService;/*** @author yueyi2019*/
@Service("grabRedisRedissonService")
public class GrabRedisRedissonServiceImpl implements GrabService {@AutowiredRedissonClient redissonClient;@AutowiredOrderService orderService;@Overridepublic ResponseResult grabOrder(int orderId , int driverId){//生成keyString lock = "order_"+(orderId+"");RLock rlock = redissonClient.getLock(lock.intern());try {// 此代码默认 设置key 超时时间30秒,过10秒,再延时rlock.lock();System.out.println("司机:"+driverId+" 执行抢单逻辑");boolean b = orderService.grab(orderId, driverId);if(b) {System.out.println("司机:"+driverId+" 抢单成功");}else {System.out.println("司机:"+driverId+" 抢单失败");}} finally {rlock.unlock();}return null;}
}

感觉应该没什么了,我再看看,如果有别的主要的地方我再添加。

 

 

 

 

 

 

附项目地址.git    这个项目是开源的吧,能直接搜到的,有什么问题的话,请在评论区回复,然后我把这篇文章下了。

更多推荐

网约车项目学习笔记

本文发布于:2024-03-10 07:26:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1727354.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:学习笔记   项目   网约车

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!