Commit 54b20142 by 孙龙

init

parents
## caipu-server
### v1.0.0
1. 上线功能xxx
# Owner
# Author
# Reviewer
# caipu-server
## 项目简介
1.GO服务DEMO
2.运行命令
go run cmd/main.go -conf=./configs/
package main
import (
"flag"
"context"
"kaopu-server/internal/service"
"github.com/bilibili/kratos/pkg/conf/paladin"
"github.com/bilibili/kratos/pkg/log"
)
func main() {
//获取输入参数
flag.Parse()
if err := paladin.Init(); err != nil {
panic(err)
}
//初始化日志目录
log.Init(&log.Config{Dir: "logs"})
defer log.Close()
log.Info("caipu-server start")
//声明service层
svc := service.New()
ctx := context.Background()
//调用会员服务
svc.GetUserList(ctx)
}
[log]
dir = "./logs/"
\ No newline at end of file
File mode changed
[server]
addr = "0.0.0.0:8000"
timeout = "1s"
demoExpire = "24h"
[demo]
name = "caipu-server"
proto = "tcp"
addr = "127.0.0.1:11211"
active = 50
idle = 10
dialTimeout = "100ms"
readTimeout = "200ms"
writeTimeout = "300ms"
idleTimeout = "80s"
[demo]
addr = "localhost:3306"
dsn = "root:root@tcp(localhost:3306)/cndzys?timeout=1s&readTimeout=1s&writeTimeout=1s&parseTime=true&loc=Local&charset=utf8mb4,utf8"
readDSN = ["root:root@tcp(127.0.0.2:3306)/cndzys?timeout=1s&readTimeout=1s&writeTimeout=1s&parseTime=true&loc=Local&charset=utf8mb4,utf8","root:root@tcp(127.0.0.3:3306)/cndzys?timeout=1s&readTimeout=1s&writeTimeout=1s&parseTime=true&loc=Local&charset=utf8,utf8mb4"]
active = 20
idle = 10
idleTimeout ="4h"
queryTimeout = "200ms"
execTimeout = "300ms"
tranTimeout = "400ms"
demoExpire = "24h"
[demo]
name = "caipu-server"
proto = "tcp"
addr = "127.0.0.1:6389"
idle = 10
active = 10
dialTimeout = "1s"
readTimeout = "1s"
writeTimeout = "1s"
idleTimeout = "10s"
module kaopu-server
go 1.12
require (
github.com/bilibili/kratos v0.3.1
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/pkg/errors v0.8.1
)
This diff is collapsed. Click to expand it.
package dao
import (
"context"
"time"
"kaopu-server/internal/model"
"github.com/bilibili/kratos/pkg/cache/memcache"
"github.com/bilibili/kratos/pkg/cache/redis"
"github.com/bilibili/kratos/pkg/conf/paladin"
"github.com/bilibili/kratos/pkg/database/sql"
"github.com/bilibili/kratos/pkg/log"
xtime "github.com/bilibili/kratos/pkg/time"
)
// Dao dao interface
type Dao interface {
Close()
Ping(ctx context.Context) (err error)
GetUserInfoByUserId(ctx context.Context,user_id int64) (r *model.Users,err error)
GetUserList(ctx context.Context) (r []*model.Users,err error)
}
// dao dao.
type dao struct {
db *sql.DB
redis *redis.Pool
redisExpire int32
mc *memcache.Memcache
mcExpire int32
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
// New new a dao and return.
func New() (Dao) {
var (
dc struct {
Demo *sql.Config
}
rc struct {
Demo *redis.Config
DemoExpire xtime.Duration
}
mc struct {
Demo *memcache.Config
DemoExpire xtime.Duration
}
)
checkErr(paladin.Get("mysql.toml").UnmarshalTOML(&dc))
checkErr(paladin.Get("redis.toml").UnmarshalTOML(&rc))
checkErr(paladin.Get("memcache.toml").UnmarshalTOML(&mc))
return &dao{
// mysql
db: sql.NewMySQL(dc.Demo),
// redis
redis: redis.NewPool(rc.Demo),
redisExpire: int32(time.Duration(rc.DemoExpire) / time.Second),
// memcache
mc: memcache.New(mc.Demo),
mcExpire: int32(time.Duration(mc.DemoExpire) / time.Second),
}
}
// Close close the resource.
func (d *dao) Close() {
d.mc.Close()
d.redis.Close()
d.db.Close()
}
// Ping ping the resource.
func (d *dao) Ping(ctx context.Context) (err error) {
if err = d.pingMC(ctx); err != nil {
return
}
if err = d.pingRedis(ctx); err != nil {
return
}
return d.db.Ping(ctx)
}
func (d *dao) pingMC(ctx context.Context) (err error) {
if err = d.mc.Set(ctx, &memcache.Item{Key: "ping", Value: []byte("pong"), Expiration: 0}); err != nil {
log.Error("conn.Set(PING) error(%v)", err)
}
return
}
func (d *dao) pingRedis(ctx context.Context) (err error) {
conn := d.redis.Get(ctx)
defer conn.Close()
if _, err = conn.Do("SET", "ping", "pong"); err != nil {
log.Error("conn.Set(PING) error(%v)", err)
}
return
}
package dao
import (
"context"
"kaopu-server/internal/model"
"github.com/bilibili/kratos/pkg/database/sql"
"github.com/pkg/errors"
)
const (
_user_Info_One = "SELECT user_id, user_name FROM ys_users WHERE user_id=?"
)
//通过UID获取用户信息
func (d *dao) GetUserInfoByUserId(ctx context.Context, user_id int64) (r *model.Users, err error) {
var row *sql.Row
r = new(model.Users)
row = d.db.QueryRow(ctx, _user_Info_One, user_id)
if err = row.Scan(&r.User_id, &r.User_name); err != nil {
if err == sql.ErrNoRows {
err = nil
r = nil
return
}
err = errors.WithStack(err)
return
}
return
}
//列表方式获取会员信息
func (d *dao) GetUserList(ctx context.Context) (res []*model.Users, err error) {
var (
rows *sql.Rows
)
if rows, err = d.db.Query(ctx, "SELECT user_id, user_name FROM ys_users "); err != nil {
err = errors.WithStack(err)
return
}
defer rows.Close()
for rows.Next() {
r := &model.Users{}
if err = rows.Scan(&r.User_id, &r.User_name); err != nil {
err = errors.WithStack(err)
res = nil
return
}
res = append(res, r)
}
err = rows.Err()
return
}
\ No newline at end of file
package model
// Kratos hello kratos.
type Kratos struct {
Hello string
}
\ No newline at end of file
package model
// Kratos hello kratos.
type Users struct {
User_id int64 `json:"user_id"`
User_name string `json:"user_name"`
}
\ No newline at end of file
package service
import (
"context"
"fmt"
"kaopu-server/internal/dao"
"kaopu-server/internal/model"
"github.com/bilibili/kratos/pkg/conf/paladin"
"github.com/pkg/errors"
)
// Service service.
type Service struct {
ac *paladin.Map
dao dao.Dao
}
// New new a service and return.
func New() (s *Service) {
var ac = new(paladin.TOML)
if err := paladin.Watch("application.toml", ac); err != nil {
panic(err)
}
s = &Service{
ac: ac,
dao: dao.New(),
}
return s
}
// Ping ping the resource.
func (s *Service) Ping(ctx context.Context) (err error) {
return s.dao.Ping(ctx)
}
// Close close the resource.
func (s *Service) Close() {
s.dao.Close()
}
//通过用户ID获取用户信息
func (s *Service) GetUserInfoByUserId(ctx context.Context, user_id int64) (res *model.Users, err error) {
if res, err = s.dao.GetUserInfoByUserId(ctx, user_id); err != nil {
err = errors.WithStack(err)
}
return
}
//获取用户列表
func (s *Service) GetUserList(ctx context.Context) (res []*model.Users, err error) {
//调用DB方法,获取会员数据
res, _ = s.dao.GetUserList(ctx)
fmt.Println(res)
return
}
\ No newline at end of file
File mode changed
[2019/11/20 13:33:35.764] [INFO] [F:/golang/src/caipu-server/cmd/main.go:26] caipu-server start
[2019/11/20 13:33:35.774] [INFO] [F:/golang/src/github.com/bilibili/kratos/pkg/net/http/blademaster/server.go:98] blademaster: start http listen addr: 0.0.0.0:8000
[2019/11/20 13:42:14.484] [INFO] [F:/golang/src/caipu-server/cmd/main.go:33] get a signal interrupt
[2019/11/20 13:42:14.484] [INFO] [F:/golang/src/caipu-server/cmd/main.go:40] caipu-server exit
[2019/11/20 13:42:14.484] [INFO] [F:/golang/src/github.com/bilibili/kratos/pkg/net/http/blademaster/server.go:106] blademaster: server closed
[2019/11/20 18:43:38.619] [INFO] [F:/golang/src/caipu-server/cmd/main.go:22] caipu-server start
[2019/11/20 18:43:38.628] [INFO] [F:/golang/src/github.com/bilibili/kratos/pkg/net/http/blademaster/server.go:98] blademaster: start http listen addr: 0.0.0.0:8000
[2019/11/20 18:49:14.342] [INFO] [F:/golang/src/caipu-server/cmd/a.go:22] caipu-server start
[2019/11/20 18:49:14.352] [INFO] [F:/golang/src/github.com/bilibili/kratos/pkg/net/http/blademaster/server.go:98] blademaster: start http listen addr: 0.0.0.0:8000
[2019/11/20 18:49:48.413] [INFO] [F:/golang/src/caipu-server/cmd/a.go:22] caipu-server start
[2019/11/20 18:49:48.424] [INFO] [F:/golang/src/github.com/bilibili/kratos/pkg/net/http/blademaster/server.go:98] blademaster: start http listen addr: 0.0.0.0:8000
[2019/11/20 18:50:14.699] [INFO] [F:/golang/src/caipu-server/cmd/a.go:22] caipu-server start
[2019/11/20 18:50:14.711] [INFO] [F:/golang/src/github.com/bilibili/kratos/pkg/net/http/blademaster/server.go:98] blademaster: start http listen addr: 0.0.0.0:8000
[2019/11/20 18:50:29.051] [INFO] [F:/golang/src/caipu-server/cmd/a.go:22] caipu-server start
[2019/11/20 18:50:29.074] [INFO] [F:/golang/src/github.com/bilibili/kratos/pkg/net/http/blademaster/server.go:98] blademaster: start http listen addr: 0.0.0.0:8000
[2019/11/20 20:00:18.062] [INFO] [F:/golang/src/caipu-server/cmd/a.go:22] caipu-server start
[2019/11/20 20:01:50.681] [INFO] [F:/golang/src/caipu-server/cmd/a.go:22] caipu-server start
[2019/11/20 20:02:51.936] [INFO] [F:/golang/src/caipu-server/cmd/a.go:22] caipu-server start
[2019/11/20 20:04:42.327] [INFO] [F:/golang/src/caipu-server/cmd/a.go:22] caipu-server start
[2019/11/20 20:07:19.511] [INFO] [F:/golang/src/caipu-server/cmd/a.go:22] caipu-server start
[2019/11/20 20:09:57.209] [INFO] [F:/golang/src/caipu-server/cmd/a.go:22] caipu-server start
[2019/11/20 20:10:47.784] [INFO] [F:/golang/src/caipu-server/cmd/a.go:22] caipu-server start
[2019/11/20 20:39:22.845] [INFO] [F:/golang/src/caipu-server/cmd/main.go:21] caipu-server start
[2019/11/20 20:40:10.580] [INFO] [F:/golang/src/caipu-server/cmd/main.go:21] caipu-server start
[2019/11/21 10:15:12.824] [INFO] [D:/gocode/caipu-server/cmd/main.go:24] caipu-server start
[2019/11/21 10:34:31.591] [INFO] [D:/gocode/caipu-server/cmd/main.go:24] caipu-server start
[2019/11/21 10:37:59.963] [INFO] [D:/gocode/kaopu-server/cmd/main.go:24] caipu-server start
File mode changed
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment