Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
wang
/
golang-open-platform
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
90b216c0
authored
Mar 30, 2021
by
wang
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
^
parent
aad13101
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
10 additions
and
70 deletions
.gitignore
readme.md
.gitignore
View file @
90b216c0
...
...
@@ -28,3 +28,4 @@ cmd.exe~
/regconfig
/cmd/http/regTest
/cmd/http/redisTest
/cmd/cron/push
readme.md
View file @
90b216c0
## golang 商品微服务
## 主动推送数据给京东
### 项目启动
一:启动http 对外服务:
cd cmd/http
go build -o ./http ./http_server.go
chmod -R 755 http
chmod +x http
运行-线上: ./http -config=../../conf/prod
运行-开发: ./http -config=../../conf/dev
二:增加定时任务:
cd cmd/cron
go build -o ./cron ./cron_server.go
chmod -R 755 cron
chmod +x cron
运行-线上: ./cron -config=../../conf/prod
运行-开发: ./cron -config=../../conf/dev
一共两个任务,最好都配置supervisor
### 目录结构
├── bat //运行脚本或者protoc生成脚本
├── boot //启动文件,比如配置加载,数据库加载等等
├── cmd //服务的主入口,可直接go run执行
├── conf //配置文件目录
├── framework //gin框架接入
├── controller //顾名思义
├── dao //数据访问层
├── model //模型目录
├── pkg //公用包目录,比如配置读取,获取数据库实例,通用函数或者变量等都可以从这里获取
├── protopb //生成的protos.pb.go文件
├── protos //原始的protos文件
├── service //业务逻辑操作所在目录
### 已经封装的模块以及用法
#### 1.配置读取
接在
```conf/config.ini```
文件添加自己的配置项
然后使用
```config.Get("xxx.xxx").String()```
或者
```config.Get("xxx.xxx").Int()```
即可
如果想要获取某个节点下的所有值,可以用
```config.GetSectionValues("xxx")```
如果想要去获取更多其它的值,可以用暴露出来的配置变量自己去获取,比如:
```config.Cfg.Section("xxx").Key("xxx")```
配置包的文档 : https://ini.unknwon.io/docs
#### 2.mysql连接实例
使用
```mysql.GetDB()```
即可获取对应的实例,不同数据库的连接可以自己定义不同的GetDB方法
gorm包的文档 : https://gorm.io/zh_CN/docs/index.html
#### 3.redis使用
可以直接调用
```gredis.Hset(),gredis.Hget()```
等方法,具体使用请参考代码,其中返回的结果是一个redis对象,需要自己转成自己需要的类型,比如 :
```redis.String(gredis.HGet("test",1))```
.
redis包文档 : https://pkg.go.dev/gopkg.in/DataDog/dd-trace-go.v1/contrib/garyburd/redigo?tab=doc
#### 4.mgo连接实例
使用
```mongo.GetDB()```
即可,不同数据库的连接可以自己定义不同的GetDB方法
然后用C方法指定集合进行操作即可:
```mongo.GetDB().C("xxx").Find(bson.M{}).Count()```
.
mgo包的文档 : https://godoc.org/gopkg.in/mgo.v2
文档2 : https://zhuanlan.zhihu.com/p/99731299
#### 5.钉钉群消息以及短信
直接在pkg/message包调用对应的函数即可:
钉钉的为
```message.DingDingPush```
#### 6.商品信息获取
直接调用
```service.GetGoodsInfo```
函数即可,传入goods_id的字符串,多个goods_id用逗号隔开(商品服务的请求本来亦是如此),会返回json字符串,然后可以根据自己的需求用gjson获取对应的信息即可
#### 7.ES的基本请求
直接调用
```es.CurlES```
函数即可,传入索引名称和请求的json参数即可,得到的返回结果是ES的原生结果,可以根据自己的需求用gjson获取对应的信息
#### 7.队列推送
直接调用
```mq.PushMsg("xian_test","test")```
函数即可(调用方式保持和PHP一致),第一个参数队列名同时也是路由名,第二个是要传的值
#### 一:脚本编译:
##### 编译,在根目录执行
export GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
go build -o ./cmd/cron/push ./cmd/cron/push.go
### 项目启动
./cmd/cron/push -config=./conf/prod/
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment