[完結(jié)19章]SpringBoot開(kāi)發(fā)雙11商品服務(wù)系統(tǒng)
網(wǎng)盤地址:https://pan.baidu.com/s/1P5gnZrPoml2RY5csThM4zw 提取碼: vd4p
騰訊微云下載地址:https://share.weiyun.com/o6TKslZV 密碼:qn8bim
如何使用SpringBoot開(kāi)發(fā)一款關(guān)于雙11商品服務(wù)的系統(tǒng)?今天就給大家說(shuō)道說(shuō)道,希望對(duì)大家的學(xué)習(xí)有所幫助!
1.什么是SpringBoot?
Spring 的誕?是為了簡(jiǎn)化 Java 程序的開(kāi)發(fā)的,? Spring Boot 的誕?是為了簡(jiǎn)化 Spring 程序開(kāi)發(fā)的。
Spring Boot是由Pivotal團(tuán)隊(duì)提供的基于Spring的框架,該框架使用了特定的方式來(lái)進(jìn)行配置,從而使開(kāi)發(fā)人員不再需要定義樣板化的配置。Spring Boot集成了絕大部分目前流行的開(kāi)發(fā)框架,就像Maven集成了所有的JAR包一樣,Spring Boot集成了幾乎所有的框架,使得開(kāi)發(fā)者能快速搭建Spring項(xiàng)目。
2.SpringBoot的優(yōu)點(diǎn)
快速集成框架,Spring Boot 提供了啟動(dòng)添加依賴的功能,?于秒級(jí)集成各種框架。
內(nèi)置運(yùn)?容器,?需配置 Tomcat 等 Web 容器,直接運(yùn)?和部署程序。
快速部署項(xiàng)?,?需外部容器即可啟動(dòng)并運(yùn)?項(xiàng)?。
可以完全拋棄繁瑣的 XML,使?注解和配置的?式進(jìn)?開(kāi)發(fā)。
?持更多的監(jiān)控的指標(biāo),可以更好的了解項(xiàng)?的運(yùn)?情況
后端配置
1.1創(chuàng)建Springboot工程
打開(kāi)idea->file->new->project
選擇spring Initializer進(jìn)行配置,java版本選擇8,點(diǎn)擊next
- internal 應(yīng)用代碼
? ? - controllers 控制器模塊
? ? ? - admin 后端控制器
? ? ? - front 前端控制器
? ? - listen redis監(jiān)聽(tīng)器
? ? - models 模型模塊
? ? - service 服務(wù)模塊
? ? ? - product_serive 商品服務(wù)
? ? ? - wechat_menu_serive 微信公眾號(hào)菜單服務(wù)
? ? ? ......
- conf 公共配置
? -config.yml yml配置文件
? -config.go 配置解析,轉(zhuǎn)化成對(duì)應(yīng)的結(jié)構(gòu)體
??
- middleware 中間件
? ? - AuthCheck.go? jwt接口權(quán)限校驗(yàn)
- cors.go 跨域處理
......
- pkg 程序應(yīng)用包
? - app
? - base
? - casbin
? - jwt
? - qrcode
? .....
- routere 路由
- logs 日志存放
- runtime 資源目錄
首先,我仔細(xì)分析了需求,并且根據(jù)業(yè)務(wù)邏輯設(shè)計(jì)了合適的接口。
對(duì)于多表關(guān)聯(lián)查詢,我使用了MyBatis的注解來(lái)編寫SQL語(yǔ)句,并通過(guò)@One和@Many等注解來(lái)實(shí)現(xiàn)結(jié)果集的映射。
對(duì)于數(shù)據(jù)分頁(yè),我使用了MyBatis-Plus提供的Page對(duì)象,并結(jié)合相關(guān)方法來(lái)實(shí)現(xiàn)數(shù)據(jù)分頁(yè)查詢。
2. 上課中的優(yōu)秀項(xiàng)目
在課堂上,我完成了一個(gè)優(yōu)秀的項(xiàng)目,主要是學(xué)生實(shí)體類的增刪改查功能。通過(guò)這個(gè)項(xiàng)目,我鞏固了對(duì)Spring Boot框架的理解和掌握。
具體實(shí)現(xiàn)如下:
//初始化redis
err := cache.InitRedis(cache.DefaultRedisClient, &redis.Options{
Addr:? ? ? ? global.CONFIG.Redis.Host,
Password:? ? global.CONFIG.Redis.Password,
IdleTimeout: global.CONFIG.Redis.IdleTimeout,
}, nil)
if err != nil {
if err != nil {
global.LOG.Error("InitRedis error ", err, "client", cache.DefaultRedisClient)
panic(err)
}
panic(err)
}
//初始化mysql
err = db.InitMysqlClient(db.DefaultClient, global.CONFIG.Database.User,
global.CONFIG.Database.Password, global.CONFIG.Database.Host,
global.CONFIG.Database.Name)
if err != nil {
global.LOG.Error("InitMysqlClient error ", err, "client", db.DefaultClient)
panic(err)
}
global.Db = db.GetMysqlClient(db.DefaultClient).DB
開(kāi)發(fā)步驟
SpringBoot 開(kāi)發(fā)起來(lái)特別簡(jiǎn)單,分為如下幾步:
創(chuàng)建新模塊,選擇Spring初始化,并配置模塊相關(guān)基礎(chǔ)信息
選擇當(dāng)前模塊需要使用的技術(shù)集
開(kāi)發(fā)控制器類
運(yùn)行自動(dòng)生成的Application類
知道了 SpringBoot 的開(kāi)發(fā)步驟后,接下來(lái)我們進(jìn)行具體的操作
shutdown.NewHook().Close(
//關(guān)閉http server
func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
logging.Error("http server shutdown err", err)
}
},
func() {
//關(guān)閉kafka producer(特別是異步生產(chǎn)者,強(qiáng)制關(guān)閉會(huì)導(dǎo)致丟消息)
if err := mq.GetKafkaSyncProducer(mq.DefaultKafkaSyncProducer).Close(); err != nil {
logging.Error("kafka shutdown err", err)
}
},
func() {
//關(guān)閉mysql
if err := db.CloseMysqlClient(db.DefaultClient); err != nil {
logging.Error("mysql shutdown err", err)
}
},
func() {
//關(guān)閉redis
if err := cache.GetRedisClient(cache.DefaultRedisClient).Close(); err != nil {
logging.Error("redis shutdown err", err)
}
},
)
//也可以自己實(shí)現(xiàn)優(yōu)雅關(guān)閉
//signals := make(chan os.Signal, 0)
//signal.Notify(signals, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
//s := <-signals
//global.LOG.Warn("shop receive system signal:", s)
//ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
//defer cancel()
//err := server.Shutdown(ctx)
//if err != nil {
// global.LOG.Error("http server error", err)
//}
//mq.GetKafkaSyncProducer(mq.DefaultKafkaSyncProducer).Close()
選擇 Spring Initializr ,用來(lái)創(chuàng)建 SpringBoot 工程
以前我們選擇的是 Maven ,今天選擇 Spring Initializr 來(lái)快速構(gòu)建 SpringBoot 工程。而在 Module SDK 這一項(xiàng)選擇我們安裝的 JDK 版本。
type StoreProduct struct {
Image? ? ? ? string? ? ? ? ?`json:"image" valid:"Required;"`
SliderImage? string? ? ? ? ?`json:"slider_image" valid:"Required;"`
StoreName? ? string? ? ? ? ?`json:"store_name" valid:"Required;"`
StoreInfo? ? string? ? ? ? ?`json:"store_info" valid:"Required;"`
Keyword? ? ? string? ? ? ? ?`json:"keyword" valid:"Required;"`
CateId? ? ? ?int? ? ? ? ? ? `json:"cate_id" valid:"Required;"`
ProductCate? *StoreCategory `json:"product_cate" gorm:"foreignKey:CateId;association_autoupdate:false;association_autocreate:false"`
Price? ? ? ? float64? ? ? ? `json:"price" valid:"Required;"`
VipPrice? ? ?float64? ? ? ? `json:"vip_price" valid:"Required;"`
OtPrice? ? ? float64? ? ? ? `json:"ot_price" valid:"Required;"`
Postage? ? ? float64? ? ? ? `json:"postage" valid:"Required;"`
UnitName? ? ?string? ? ? ? ?`json:"unit_name" valid:"Required;"`
Sort? ? ? ? ?int16? ? ? ? ? `json:"sort" valid:"Required;"`
Sales? ? ? ? int? ? ? ? ? ? `json:"sales" valid:"Required;"`
Stock? ? ? ? int? ? ? ? ? ? `json:"stock" valid:"Required;"`
IsShow? ? ? ?*int8? ? ? ? ? `json:"is_show" valid:"Required;"`
IsHot? ? ? ? *int8? ? ? ? ? `json:"is_hot" valid:"Required;"`
IsBenefit? ? *int8? ? ? ? ? `json:"is_benefit" valid:"Required;"`
IsBest? ? ? ?*int8? ? ? ? ? `json:"is_best" valid:"Required;"`
IsNew? ? ? ? *int8? ? ? ? ? `json:"is_new" valid:"Required;"`
Description? string? ? ? ? ?`json:"description" valid:"Required;"`
IsPostage? ? *int8? ? ? ? ? `json:"is_postage" valid:"Required;"`
GiveIntegral int? ? ? ? ? ? `json:"give_integral" valid:"Required;"`
Cost? ? ? ? ?float64? ? ? ? `json:"cost" valid:"Required;"`
IsGood? ? ? ?*int8? ? ? ? ? `json:"is_good" valid:"Required;"`
Ficti? ? ? ? int? ? ? ? ? ? `json:"ficti" valid:"Required;"`
Browse? ? ? ?int? ? ? ? ? ? `json:"browse" valid:"Required;"`
IsSub? ? ? ? *int8? ? ? ? ? `json:"is_sub" valid:"Required;"`
TempId? ? ? ?int64? ? ? ? ? `json:"temp_id" valid:"Required;"`
SpecType? ? ?int8? ? ? ? ? ?`json:"spec_type" valid:"Required;"`
IsIntegral? ?*int8? ? ? ? ? `json:"isIntegral" valid:"Required;"`
Integral? ? ?int32? ? ? ? ? `json:"integral" valid:"Required;"`
BaseModel
}
//定義商品消息結(jié)構(gòu)
type ProductMsg struct {
Operation string `json:"operation"`
*StoreProduct
}
切換web服務(wù)器
現(xiàn)在我們啟動(dòng)工程使用的是 tomcat 服務(wù)器,那能不能不使用 tomcat 而使用 jetty 服務(wù)器,jetty 在我們 maven 高級(jí)時(shí)講 maven 私服使用的服務(wù)器。而要切換 web 服務(wù)器就需要將默認(rèn)的 tomcat 服務(wù)器給排除掉,怎么排除呢?使用 exclusion 標(biāo)簽
func (e *StoreProductController) Post(c *gin.Context) {
var (
dto? dto2.StoreProduct
appG = app.Gin{C: c}
)
httpCode, errCode := app.BindAndValid(c, &dto)
if errCode != constant.SUCCESS {
appG.Response(httpCode, errCode, nil)
return
}
productService := product_service.Product{
Dto: dto,
}
model, err := productService.AddOrSaveProduct()
if err != nil {
appG.Response(http.StatusInternalServerError, constant.FAIL_ADD_DATA, nil)
return
}
//發(fā)消息隊(duì)列
defer func() {
operation := product.OperationCreate
if dto.Id > 0 {
operation = product.OperationUpdate
}
productMsg := models.ProductMsg{
operation,
&model,
}
msg, _ := json.Marshal(productMsg)
p, o, e := mq.GetKafkaSyncProducer(mq.DefaultKafkaSyncProducer).Send(&sarama.ProducerMessage{
Topic: product.Topic,
Key:? ?mq.KafkaMsgValueStrEncoder(strconv.FormatInt(dto.Id, 10)),
Value: mq.KafkaMsgValueEncoder(msg),
},
)
if e != nil {
global.LOG.Error("send product msg error ", e, "partition :", p, "offset :", o, "id :", dto.Id)
}
}()
appG.Response(http.StatusOK, constant.SUCCESS, nil)
}
[完結(jié)19章]SpringBoot開(kāi)發(fā)雙11商品服務(wù)系統(tǒng)的評(píng)論 (共 條)
