JAVA全系列 教程
3762个小节阅读:7092.6k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
“秒杀”是网络卖家发布一些超低价格的商品,所有买家在同一时间网上抢购的一种销售方式。秒杀技术实现核心思想是运用缓存减少数据库瞬间的访问压力。在秒杀时,首先会将数据库的秒杀商品同步到缓存中,用户从缓存中查询秒杀商品,抢购商品时减少缓存中的库存数量。产生的秒杀订单先写到缓存,付款成功后再写入数据库。
创建名为shopping_seckill_service
的SpringBoot工程,添加相关依赖。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- MyBatisPlus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.itbaizhan</groupId>
<artifactId>shopping_common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
<!-- nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
设置该工程的父工程为shopping
。
xxxxxxxxxx
<parent>
<groupId>com.itbaizhan</groupId>
<artifactId>shopping</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
给shopping
工程设置子模块
xxxxxxxxxx
<!-- 子模块 -->
<modules>
<!-- 秒杀服务 -->
<module>shopping_seckill_service</module>
</modules>
在Nacos配置中心创建配置文件shopping_seckill_service-dev.yaml
x# 端口号
server:
port: 9005
# 日志格式
logging:
pattern:
console: '%d{HH:mm:ss.SSS} %clr(%-5level) --- [%-15thread] %cyan(%-50logger{50}):%msg%n'
spring:
application:
name: shopping_seckill_service #服务名
cloud:
nacos:
discovery:
server-addr: 192.168.0.99:8848 # 注册中心地址
# 数据源
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///baizhanshopping?serverTimezone=UTC
username: root
password: root
# redis
data:
redis:
host: 192.168.0.99
port: 6379
timeout: 30000
jedis:
pool:
max-idle: 8
max-wait: -1
max-active: 8
min-idle: 0
#配置mybatis-plus
mybatis-plus:
global-config:
db-config:
# 表名前缀
table-prefix: bz_
# 主键生成策略为自增
id-type: auto
configuration:
# 关闭列名自动驼峰命名规则映射
map-underscore-to-camel-case: false
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #开启sql日志
dubbo:
application:
name: shopping_seckill_service #服务名
serialize-check-status: DISABLE
check-serializable: false
protocol:
name: dubbo # 通讯协议
port: -1 # 端口号,-1表示自动扫描可用端口。
registry:
address: nacos://192.168.0.99:8848 # 注册中心
编写配置文件bootstrap.yml
,连接配置中心
xxxxxxxxxx
spring:
profiles:
# 环境
active: dev
cloud:
nacos:
# 注册中心
config:
server-addr: 192.168.0.99:8848 #注册中心地址
namespace: 5455dc7a-b343-4866-a973-96d5cc1d9312 #命名空间
file-extension: yaml #文件名后缀
prefix: shopping_seckill_service #文件名
启动类扫描Mapper
xxxxxxxxxx
@EnableDiscoveryClient
@EnableDubbo
@RefreshScope
@SpringBootApplication
@MapperScan("com.itbaizhan.shopping_seckill_service.mapper")
public class ShoppingSeckillServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ShoppingSeckillServiceApplication.class, args);
}
}
创建名为shopping_seckill_customer_api
的SpringBoot工程,添加相关依赖。
xxxxxxxxxx
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
<!-- nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.itbaizhan</groupId>
<artifactId>shopping_common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
设置该工程的父工程为shopping
。
xxxxxxxxxx
<parent>
<groupId>com.itbaizhan</groupId>
<artifactId>shopping</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
给shopping
工程设置子模块
xxxxxxxxxx
<!-- 子模块 -->
<modules>
<!-- 秒杀api -->
<module>shopping_seckill_customer_api</module>
</modules>
在Nacos配置中心编写配置文件shopping_seckill_customer_api-dev.yaml
xxxxxxxxxx
# 端口号
server:
port: 8007
# 日志格式
logging:
pattern:
console: '%d{HH:mm:ss.SSS} %clr(%-5level) --- [%-15thread] %cyan(%-50logger{50}):%msg%n'
# Nacos
spring:
application:
name: shopping_seckill_customer_api
cloud:
nacos:
discovery:
server-addr: 192.168.0.99:8848
dubbo:
application:
#项目名字
name: shopping_seckill_customer_api
protocol:
name: dubbo
port: -1
registry:
# 注册地址
address: nacos://192.168.0.99:8848
编写配置文件bootstrap.yml
,连接配置中心
xxxxxxxxxx
spring:
profiles:
# 环境
active: dev
cloud:
nacos:
# 注册中心
config:
server-addr: 192.168.0.99:8848 #注册中心地址
namespace: 5455dc7a-b343-4866-a973-96d5cc1d9312 #命名空间
file-extension: yaml #文件名后缀
prefix: shopping_seckill_customer_api
编写启动类
xxxxxxxxxx
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
@EnableDiscoveryClient
@RefreshScope
public class ShoppingUserCustomerApiApplication {
public static void main(String[] args) {
SpringApplication.run(ShoppingUserCustomerApiApplication.class, args);
}
}