parent
5721f0619d
commit
d9f68d55f7
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/goshop.iml" filepath="$PROJECT_DIR$/.idea/goshop.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$/product-service" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/product-service.iml" filepath="$PROJECT_DIR$/.idea/product-service.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true">
|
||||
<buildTags>
|
||||
<option name="goVersion" value="go1.17" />
|
||||
</buildTags>
|
||||
</component>
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectTasksOptions">
|
||||
<enabled-global>
|
||||
<option value="go fmt" />
|
||||
</enabled-global>
|
||||
</component>
|
||||
</project>
|
@ -0,0 +1,89 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
consul "github.com/asim/go-micro/plugins/registry/consul/v4"
|
||||
opentracing2 "github.com/go-micro/plugins/v4/wrapper/trace/opentracing"
|
||||
"github.com/opentracing/opentracing-go"
|
||||
"go-micro.dev/v4/web"
|
||||
"goproduct/common"
|
||||
"goproduct/proto"
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go-micro.dev/v4"
|
||||
"go-micro.dev/v4/registry"
|
||||
)
|
||||
|
||||
// 获取远程服务的客户端
|
||||
func main() {
|
||||
router := gin.Default()
|
||||
|
||||
//注册到consul
|
||||
consulReg := consul.NewRegistry(func(options *registry.Options) {
|
||||
options.Addrs = []string{"192.168.100.131:8500"}
|
||||
})
|
||||
//初始化链路追踪的jaeper
|
||||
t, io, err := common.NewTracer("shop-cart-client", "192.168.100.131:6831")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
defer io.Close()
|
||||
opentracing.SetGlobalTracer(t)
|
||||
|
||||
rpcServer := micro.NewService(
|
||||
//micro.Name("shop-product-client"),
|
||||
micro.Registry(consulReg), //服务发现
|
||||
micro.WrapClient(opentracing2.NewClientWrapper(opentracing.GlobalTracer())),
|
||||
)
|
||||
client := proto.NewAddCartService("shop-cart", rpcServer.Client())
|
||||
clientA := proto.NewShowProductDetailService("shop-product", rpcServer.Client())
|
||||
//分页查询商品列表
|
||||
router.GET("/increase", func(c *gin.Context) {
|
||||
// "number": 0,
|
||||
// "productId": 0,
|
||||
// "productSkuId": 0
|
||||
number, _ := strconv.Atoi(c.Request.FormValue("number"))
|
||||
productId, _ := strconv.Atoi(c.Request.FormValue("productId"))
|
||||
productSkuId, _ := strconv.Atoi(c.Request.FormValue("productSkuId"))
|
||||
|
||||
//拼接请求信息
|
||||
req := &proto.AddCartReq{
|
||||
Number: int32(number),
|
||||
ProductId: int32(productId),
|
||||
ProductSkuId: int32(productSkuId),
|
||||
}
|
||||
//商品详情
|
||||
reqDetail := &proto.ProductDetailReq{
|
||||
Id: int32(productId),
|
||||
}
|
||||
respDetail, err := clientA.ShowProductDetail(context.TODO(), reqDetail)
|
||||
req.ProductName = respDetail.ProductDetail[0].Name
|
||||
req.ProductMainPicture = respDetail.ProductDetail[0].MainPicture
|
||||
log.Println(" /ProductDetailReq get :", req)
|
||||
//SKU
|
||||
//添加购物车 远程调用服务log.Println(" /ProductDetailReq get :", req)
|
||||
// //SKU
|
||||
// //添加购物车 远程调用服务
|
||||
resp, err := client.AddCart(context.TODO(), req)
|
||||
log.Println(" /increase :", resp)
|
||||
//根据响应做输出
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
common.RespFail(c.Writer, resp, "请求失败")
|
||||
return
|
||||
}
|
||||
////writer data message row total field
|
||||
common.RespOK(c.Writer, resp, "请求成功")
|
||||
})
|
||||
|
||||
service := web.NewService(
|
||||
web.Address(":6668"),
|
||||
web.Name("shop-cart-client"),
|
||||
web.Registry(consulReg),
|
||||
web.Handler(router),
|
||||
)
|
||||
//启动服务
|
||||
service.Run()
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
@Auth:ShenZ
|
||||
@Description:
|
||||
*/
|
@ -0,0 +1,60 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
_ "github.com/spf13/viper/remote"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
func GetConsulConfig(url string, fileKey string) (*viper.Viper, error) {
|
||||
conf := viper.New()
|
||||
conf.AddRemoteProvider("consul", url, fileKey)
|
||||
conf.SetConfigType("json")
|
||||
err := conf.ReadRemoteConfig()
|
||||
if err != nil {
|
||||
log.Println("viper conf err :", err)
|
||||
}
|
||||
return conf, nil
|
||||
}
|
||||
|
||||
/**
|
||||
{
|
||||
"host": "192.168.137.131",
|
||||
"port": "3306",
|
||||
"user": "root",
|
||||
"pwd": "mashibing123",
|
||||
"database": "user_center"
|
||||
}
|
||||
**/
|
||||
|
||||
// type MySQLConfig struct {
|
||||
// Host string `json:"host"`
|
||||
// Post string `json:"port"`
|
||||
// User string `json:"user"`
|
||||
// Pwd string `json:"pwd"`
|
||||
// Database string `json:"database"`
|
||||
// }
|
||||
|
||||
func GetMysqlFromConsul(vip *viper.Viper) (db *gorm.DB, err error) {
|
||||
newLogger := logger.New(
|
||||
log.New(os.Stdout, "\r\n", log.LstdFlags),
|
||||
logger.Config{
|
||||
SlowThreshold: time.Second,
|
||||
LogLevel: logger.Info,
|
||||
Colorful: true,
|
||||
},
|
||||
)
|
||||
str := vip.GetString("user") + ":" + vip.GetString("pwd") + "@tcp(" + vip.GetString("host") + ":" + vip.GetString("port") + ")/" + vip.GetString("database") + "?charset=utf8mb4&parseTime=True&loc=Local"
|
||||
db, errr := gorm.Open(mysql.Open(str), &gorm.Config{Logger: newLogger}) //"root:mashibing123@tcp(8.142.25.43:3306)/user_center?charset=utf8mb4&parseTime=True&loc=Local"), &gorm.Config{Logger: newLogger})
|
||||
if errr != nil {
|
||||
log.Println("db err :", errr)
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"github.com/opentracing/opentracing-go"
|
||||
jaeger "github.com/uber/jaeger-client-go"
|
||||
"github.com/uber/jaeger-client-go/config"
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
|
||||
/*
|
||||
@Auth:ShenZ
|
||||
@Description: Jaeger的实列化方法
|
||||
*/
|
||||
func NewTracer(serviceName string, addr string) (opentracing.Tracer, io.Closer, error) {
|
||||
cfg := &config.Configuration{
|
||||
ServiceName: serviceName,
|
||||
Sampler: &config.SamplerConfig{
|
||||
Type: jaeger.SamplerTypeConst,
|
||||
Param: 1,
|
||||
},
|
||||
Reporter: &config.ReporterConfig{
|
||||
BufferFlushInterval: 1 * time.Second,
|
||||
LogSpans: true,
|
||||
LocalAgentHostPort: addr,
|
||||
},
|
||||
}
|
||||
return cfg.NewTracer()
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
/**
|
||||
"{\"ID\":33,\"Name\":\"NeSugar小巢糖创意桌面无叶风扇usb台式风扇办公室喷雾风扇冷风机\",\"ProductType\":1,\"CategoryId\":52,\"StartingPrice\":92,\"TotalStock\":0,\"MainPicture\":\"https://msb-edu-dev.oss-cn-beijing.aliyuncs.com/mall-product/productO1CN01nsp1Wk1FIYRXXUXL8_!!4023510464-0-cib.jpg\",\"RemoteAreaPostage\":10,\"SingleBuyLimit\":0,\"IsEnable\":1,\"Remark\":\"NeSugar小巢糖创意桌面无叶风扇usb台式风扇办公室喷雾风扇冷风机\",\"CreateUser\":1,\"CreateTime\":\"2022-04-30T16:55:00+08:00\",\"Up"
|
||||
|
||||
**/
|
||||
//通过json tag 进行结构体赋值
|
||||
func SwapToStruct(req, target interface{}) (err error) {
|
||||
dataByte, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = json.Unmarshal(dataByte, target)
|
||||
return
|
||||
}
|
||||
|
||||
type H struct {
|
||||
Code string
|
||||
Message string
|
||||
TraceId string
|
||||
Data interface{}
|
||||
Rows interface{}
|
||||
Total interface{}
|
||||
SkyWalkingDynamicField string
|
||||
}
|
||||
|
||||
func Resp(w http.ResponseWriter, code string, data interface{}, message string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
h := H{
|
||||
Code: code,
|
||||
Data: data,
|
||||
Message: message,
|
||||
}
|
||||
ret, err := json.Marshal(h)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
w.Write(ret)
|
||||
}
|
||||
|
||||
func RespList(w http.ResponseWriter, code string, data interface{}, message string, rows interface{}, total interface{}, skyWalkingDynamicField string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
h := H{
|
||||
Code: code,
|
||||
Data: data,
|
||||
Message: message,
|
||||
Rows: rows,
|
||||
Total: total,
|
||||
SkyWalkingDynamicField: skyWalkingDynamicField,
|
||||
}
|
||||
ret, err := json.Marshal(h)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
w.Write(ret)
|
||||
}
|
||||
|
||||
/**
|
||||
200 OKLoginSuccessVO
|
||||
201 Created
|
||||
401 Unauthorized
|
||||
403 Forbidden
|
||||
404 Not Found
|
||||
**/
|
||||
func RespOK(w http.ResponseWriter, data interface{}, message string) {
|
||||
Resp(w, "SUCCESS", data, message)
|
||||
}
|
||||
func RespFail(w http.ResponseWriter, data interface{}, message string) {
|
||||
Resp(w, "TOKEN_FAIL", data, message)
|
||||
}
|
||||
|
||||
//writer data message row total field
|
||||
func RespListOK(w http.ResponseWriter, data interface{}, message string, rows interface{}, total interface{}, skyWalkingDynamicField string) {
|
||||
RespList(w, "SUCCESS", data, message, rows, total, skyWalkingDynamicField)
|
||||
}
|
||||
func RespListFail(w http.ResponseWriter, data interface{}, message string, rows interface{}, total interface{}, skyWalkingDynamicField string) {
|
||||
RespList(w, "TOKEN_FAIL", data, message, rows, total, skyWalkingDynamicField)
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
type ShoppingCart struct {
|
||||
//gorm.Model
|
||||
ID int32 `json:"id"`
|
||||
UserId int32 `gorm:"default:1" json:"userId"`
|
||||
ProductId int32 `gorm:"product_id" json:"productId"`
|
||||
ProductSkuId int32 `gorm:"product_sku_id" json:"productSkuId"`
|
||||
ProductName string `json:"productName"`
|
||||
ProductMainPicture string `gorm:"product_main_picture" json:"productMainPicture"`
|
||||
Number int32 `gorm:"default:1" json:"shoppingCartNumber"`
|
||||
CreateUser int32 `gorm:"default:1" json:"createUser"`
|
||||
CreateTime time.Time `json:"createTime"`
|
||||
UpdateUser int32 `json:"updateUser"`
|
||||
UpdateTime time.Time `json:"updateTime"`
|
||||
IsDeleted bool `json:"isDeleted"`
|
||||
}
|
||||
|
||||
func (table *ShoppingCart) TableName() string {
|
||||
return "shopping_cart"
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"goproduct/domain/model"
|
||||
"goproduct/proto"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
/**
|
||||
int32 clientId = 1;
|
||||
string phone = 2;
|
||||
int32 systemId = 3;
|
||||
string verificationCode = 4;
|
||||
**/
|
||||
//接口
|
||||
type ICartRepository interface {
|
||||
AddCart(*proto.AddCartReq) (*model.ShoppingCart, error)
|
||||
}
|
||||
|
||||
// 创建实例
|
||||
func NewCartRepository(db *gorm.DB) ICartRepository {
|
||||
return &CartRepository{mysqlDB: db}
|
||||
}
|
||||
|
||||
// 数据DB
|
||||
type CartRepository struct {
|
||||
mysqlDB *gorm.DB
|
||||
}
|
||||
|
||||
func (u *CartRepository) AddCart(req *proto.AddCartReq) (obj *model.ShoppingCart, err error) {
|
||||
cart := model.ShoppingCart{
|
||||
Number: req.Number,
|
||||
ProductId: req.ProductId,
|
||||
ProductSkuId: req.ProductSkuId,
|
||||
ProductName: req.ProductName,
|
||||
}
|
||||
cart.CreateTime = time.Now() //
|
||||
u.mysqlDB.Create(&cart)
|
||||
|
||||
fmt.Println("repository AddCart >>>> ", cart)
|
||||
return &cart, nil
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"goproduct/domain/model"
|
||||
"goproduct/domain/repository"
|
||||
"goproduct/proto"
|
||||
)
|
||||
|
||||
type ICartService interface {
|
||||
AddCart(*proto.AddCartReq) (obj *model.ShoppingCart, err error)
|
||||
}
|
||||
type CartService struct {
|
||||
cartRepository repository.ICartRepository
|
||||
}
|
||||
|
||||
func NewCartService(cartRepository repository.ICartRepository) ICartService {
|
||||
return &CartService{cartRepository: cartRepository}
|
||||
}
|
||||
|
||||
// "number": 0,
|
||||
//
|
||||
// "productId": 0,
|
||||
// "productSkuId": 0
|
||||
//
|
||||
// 重写接口方法
|
||||
func (u *CartService) AddCart(req *proto.AddCartReq) (obj *model.ShoppingCart, err error) {
|
||||
return u.cartRepository.AddCart(req)
|
||||
}
|
@ -0,0 +1,134 @@
|
||||
module goproduct
|
||||
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
go-micro.dev/v4 v4.8.0
|
||||
google.golang.org/protobuf v1.28.0
|
||||
gorm.io/gorm v1.23.8
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/google/uuid v1.3.0 // indirect
|
||||
github.com/miekg/dns v1.1.50 // indirect
|
||||
golang.org/x/net v0.0.0-20220708220712-1185a9018129 // indirect
|
||||
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
|
||||
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e // indirect
|
||||
golang.org/x/tools v0.1.11 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/asim/go-micro/plugins/registry/consul/v4 v4.7.0
|
||||
github.com/gin-gonic/gin v1.8.1
|
||||
github.com/go-micro/plugins/v4/wrapper/trace/opentracing v1.1.0
|
||||
github.com/opentracing/opentracing-go v1.2.0
|
||||
github.com/spf13/viper v1.12.0
|
||||
github.com/uber/jaeger-client-go v2.30.0+incompatible
|
||||
google.golang.org/grpc v1.49.0
|
||||
gorm.io/driver/mysql v1.3.5
|
||||
)
|
||||
|
||||
require (
|
||||
cloud.google.com/go v0.100.2 // indirect
|
||||
cloud.google.com/go/compute v1.6.1 // indirect
|
||||
cloud.google.com/go/firestore v1.6.1 // indirect
|
||||
github.com/HdrHistogram/hdrhistogram-go v1.1.2 // indirect
|
||||
github.com/Microsoft/go-winio v0.5.0 // indirect
|
||||
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
|
||||
github.com/acomagu/bufpipe v1.0.3 // indirect
|
||||
github.com/armon/go-metrics v0.3.10 // indirect
|
||||
github.com/bitly/go-simplejson v0.5.0 // indirect
|
||||
github.com/coreos/go-semver v0.3.0 // indirect
|
||||
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
|
||||
github.com/emirpasic/gods v1.12.0 // indirect
|
||||
github.com/evanphx/json-patch/v5 v5.5.0 // indirect
|
||||
github.com/fatih/color v1.13.0 // indirect
|
||||
github.com/felixge/httpsnoop v1.0.1 // indirect
|
||||
github.com/fsnotify/fsnotify v1.5.4 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-acme/lego/v4 v4.4.0 // indirect
|
||||
github.com/go-git/gcfg v1.5.0 // indirect
|
||||
github.com/go-git/go-billy/v5 v5.3.1 // indirect
|
||||
github.com/go-git/go-git/v5 v5.4.2 // indirect
|
||||
github.com/go-playground/locales v0.14.0 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.0 // indirect
|
||||
github.com/go-playground/validator/v10 v10.10.0 // indirect
|
||||
github.com/go-sql-driver/mysql v1.6.0 // indirect
|
||||
github.com/gobwas/httphead v0.1.0 // indirect
|
||||
github.com/gobwas/pool v0.2.1 // indirect
|
||||
github.com/gobwas/ws v1.0.4 // indirect
|
||||
github.com/goccy/go-json v0.9.7 // indirect
|
||||
github.com/gogo/protobuf v1.3.2 // indirect
|
||||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
||||
github.com/golang/protobuf v1.5.2 // indirect
|
||||
github.com/google/go-cmp v0.5.8 // indirect
|
||||
github.com/googleapis/gax-go/v2 v2.4.0 // indirect
|
||||
github.com/gorilla/handlers v1.5.1 // indirect
|
||||
github.com/hashicorp/consul/api v1.12.0 // indirect
|
||||
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
|
||||
github.com/hashicorp/go-hclog v1.2.0 // indirect
|
||||
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
|
||||
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
|
||||
github.com/hashicorp/golang-lru v0.5.4 // indirect
|
||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||
github.com/hashicorp/serf v0.9.7 // indirect
|
||||
github.com/imdario/mergo v0.3.12 // indirect
|
||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
|
||||
github.com/leodido/go-urn v1.2.1 // indirect
|
||||
github.com/magiconair/properties v1.8.6 // indirect
|
||||
github.com/mattn/go-colorable v0.1.12 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||
github.com/mitchellh/hashstructure v1.1.0 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/nxadm/tail v1.4.8 // indirect
|
||||
github.com/opentracing-contrib/go-observer v0.0.0-20170622124052-a52f23424492 // indirect
|
||||
github.com/openzipkin-contrib/zipkin-go-opentracing v0.4.5 // indirect
|
||||
github.com/openzipkin/zipkin-go v0.4.0 // indirect
|
||||
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
|
||||
github.com/pelletier/go-toml v1.9.5 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.0.1 // indirect
|
||||
github.com/sagikazarmark/crypt v0.6.0 // indirect
|
||||
github.com/sergi/go-diff v1.1.0 // indirect
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
|
||||
github.com/spf13/afero v1.8.2 // indirect
|
||||
github.com/spf13/cast v1.5.0 // indirect
|
||||
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/subosito/gotenv v1.3.0 // indirect
|
||||
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
|
||||
github.com/ugorji/go/codec v1.2.7 // indirect
|
||||
github.com/urfave/cli/v2 v2.3.0 // indirect
|
||||
github.com/xanzy/ssh-agent v0.3.0 // indirect
|
||||
go.etcd.io/etcd/api/v3 v3.5.4 // indirect
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.5.4 // indirect
|
||||
go.etcd.io/etcd/client/v2 v2.305.4 // indirect
|
||||
go.etcd.io/etcd/client/v3 v3.5.4 // indirect
|
||||
go.opencensus.io v0.23.0 // indirect
|
||||
go.uber.org/atomic v1.7.0 // indirect
|
||||
go.uber.org/multierr v1.6.0 // indirect
|
||||
go.uber.org/zap v1.17.0 // indirect
|
||||
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
|
||||
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df // indirect
|
||||
google.golang.org/api v0.81.0 // indirect
|
||||
google.golang.org/appengine v1.6.7 // indirect
|
||||
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd // indirect
|
||||
gopkg.in/ini.v1 v1.66.4 // indirect
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
|
||||
gopkg.in/warnings.v0 v0.1.2 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
)
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,33 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"goproduct/domain/service"
|
||||
"goproduct/proto"
|
||||
)
|
||||
|
||||
type CartHandler struct {
|
||||
CartService service.ICartService
|
||||
}
|
||||
|
||||
// 查询商品列表
|
||||
func (u *CartHandler) AddCart(ctx context.Context, req *proto.AddCartReq, resp *proto.AddCartResp) error {
|
||||
// "number": 0,
|
||||
// "productId": 0,
|
||||
// "productSkuId": 0
|
||||
//obj, err := u.CartService.AddCart(req.GetNumber(), req.GetProductId(), req.GetProductSkuId())
|
||||
obj, err := u.CartService.AddCart(req)
|
||||
if err != nil {
|
||||
println(" AddCart err :", err)
|
||||
}
|
||||
//count = u.ProductDataService.CountNum()
|
||||
//fmt.Println(">>>>>>>>>>>>> page product success :", obj)
|
||||
//cart := &proto.ShoppingCart{}
|
||||
//err1 := common.SwapToStruct(obj, resp)
|
||||
resp.CanSetShoppingCartNumber = int64(obj.Number)
|
||||
resp.ShoppingCartNumber = int64(obj.Number)
|
||||
resp.IsBeyondMaxLimit = false // 查询sku
|
||||
fmt.Println(" increase handler >>>>>> ", resp)
|
||||
return nil
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
consul "github.com/asim/go-micro/plugins/registry/consul/v4"
|
||||
opentracing2 "github.com/go-micro/plugins/v4/wrapper/trace/opentracing"
|
||||
"github.com/opentracing/opentracing-go"
|
||||
"go-micro.dev/v4"
|
||||
"go-micro.dev/v4/registry"
|
||||
"goproduct/common"
|
||||
"goproduct/domain/repository"
|
||||
"goproduct/domain/service"
|
||||
"goproduct/handler"
|
||||
"goproduct/proto"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
consulStr = "http://192.168.100.131:8500"
|
||||
consulReistStr = "192.168.100.131:8500"
|
||||
fileKey = "mysql-product"
|
||||
)
|
||||
|
||||
func main() {
|
||||
//0 配置中心
|
||||
consulConfig, err := common.GetConsulConfig(consulStr, fileKey)
|
||||
if err != nil {
|
||||
log.Println("consulConfig err :", err)
|
||||
}
|
||||
// 1.consul注册中心
|
||||
consulReist := consul.NewRegistry(func(options *registry.Options) {
|
||||
options.Addrs = []string{consulReistStr}
|
||||
})
|
||||
//链路追踪实列化 注意addr是 jaeper地址 端口号6831
|
||||
t, io, err := common.NewTracer("shop-cart", "192.168.100.131:6831")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer io.Close()
|
||||
//设置全局的Tracing
|
||||
opentracing.SetGlobalTracer(t)
|
||||
|
||||
repcService := micro.NewService(
|
||||
micro.RegisterTTL(time.Second*30),
|
||||
micro.RegisterInterval(time.Second*30),
|
||||
micro.Name("shop-cart"),
|
||||
micro.Address(":8084"),
|
||||
micro.Version("v1"),
|
||||
micro.Registry(consulReist),
|
||||
//引包
|
||||
micro.WrapHandler(opentracing2.NewHandlerWrapper(opentracing.GlobalTracer())),
|
||||
)
|
||||
//2.初始化db
|
||||
db, _ := common.GetMysqlFromConsul(consulConfig)
|
||||
//3.创建服务实例
|
||||
cartService := service.NewCartService(repository.NewCartRepository(db))
|
||||
//4.注册handler
|
||||
proto.RegisterAddCartHandler(repcService.Server(), &handler.CartHandler{cartService})
|
||||
|
||||
//5.启动服务
|
||||
if err := repcService.Run(); err != nil {
|
||||
log.Println("start cart service err :", err)
|
||||
}
|
||||
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,272 @@
|
||||
// Code generated by protoc-gen-micro. DO NOT EDIT.
|
||||
// source: cart.proto
|
||||
|
||||
package proto
|
||||
|
||||
import (
|
||||
fmt "fmt"
|
||||
proto "google.golang.org/protobuf/proto"
|
||||
math "math"
|
||||
)
|
||||
|
||||
import (
|
||||
context "context"
|
||||
api "go-micro.dev/v4/api"
|
||||
client "go-micro.dev/v4/client"
|
||||
server "go-micro.dev/v4/server"
|
||||
)
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ api.Endpoint
|
||||
var _ context.Context
|
||||
var _ client.Option
|
||||
var _ server.Option
|
||||
|
||||
// Api Endpoints for AddCart service
|
||||
|
||||
func NewAddCartEndpoints() []*api.Endpoint {
|
||||
return []*api.Endpoint{}
|
||||
}
|
||||
|
||||
// Client API for AddCart service
|
||||
|
||||
type AddCartService interface {
|
||||
//rpc 服务
|
||||
AddCart(ctx context.Context, in *AddCartReq, opts ...client.CallOption) (*AddCartResp, error)
|
||||
}
|
||||
|
||||
type addCartService struct {
|
||||
c client.Client
|
||||
name string
|
||||
}
|
||||
|
||||
func NewAddCartService(name string, c client.Client) AddCartService {
|
||||
return &addCartService{
|
||||
c: c,
|
||||
name: name,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *addCartService) AddCart(ctx context.Context, in *AddCartReq, opts ...client.CallOption) (*AddCartResp, error) {
|
||||
req := c.c.NewRequest(c.name, "AddCart.AddCart", in)
|
||||
out := new(AddCartResp)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Server API for AddCart service
|
||||
|
||||
type AddCartHandler interface {
|
||||
//rpc 服务
|
||||
AddCart(context.Context, *AddCartReq, *AddCartResp) error
|
||||
}
|
||||
|
||||
func RegisterAddCartHandler(s server.Server, hdlr AddCartHandler, opts ...server.HandlerOption) error {
|
||||
type addCart interface {
|
||||
AddCart(ctx context.Context, in *AddCartReq, out *AddCartResp) error
|
||||
}
|
||||
type AddCart struct {
|
||||
addCart
|
||||
}
|
||||
h := &addCartHandler{hdlr}
|
||||
return s.Handle(s.NewHandler(&AddCart{h}, opts...))
|
||||
}
|
||||
|
||||
type addCartHandler struct {
|
||||
AddCartHandler
|
||||
}
|
||||
|
||||
func (h *addCartHandler) AddCart(ctx context.Context, in *AddCartReq, out *AddCartResp) error {
|
||||
return h.AddCartHandler.AddCart(ctx, in, out)
|
||||
}
|
||||
|
||||
// Api Endpoints for Page service
|
||||
|
||||
func NewPageEndpoints() []*api.Endpoint {
|
||||
return []*api.Endpoint{}
|
||||
}
|
||||
|
||||
// Client API for Page service
|
||||
|
||||
type PageService interface {
|
||||
//rpc 服务
|
||||
Page(ctx context.Context, in *PageReq, opts ...client.CallOption) (*PageResp, error)
|
||||
}
|
||||
|
||||
type pageService struct {
|
||||
c client.Client
|
||||
name string
|
||||
}
|
||||
|
||||
func NewPageService(name string, c client.Client) PageService {
|
||||
return &pageService{
|
||||
c: c,
|
||||
name: name,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *pageService) Page(ctx context.Context, in *PageReq, opts ...client.CallOption) (*PageResp, error) {
|
||||
req := c.c.NewRequest(c.name, "Page.Page", in)
|
||||
out := new(PageResp)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Server API for Page service
|
||||
|
||||
type PageHandler interface {
|
||||
//rpc 服务
|
||||
Page(context.Context, *PageReq, *PageResp) error
|
||||
}
|
||||
|
||||
func RegisterPageHandler(s server.Server, hdlr PageHandler, opts ...server.HandlerOption) error {
|
||||
type page interface {
|
||||
Page(ctx context.Context, in *PageReq, out *PageResp) error
|
||||
}
|
||||
type Page struct {
|
||||
page
|
||||
}
|
||||
h := &pageHandler{hdlr}
|
||||
return s.Handle(s.NewHandler(&Page{h}, opts...))
|
||||
}
|
||||
|
||||
type pageHandler struct {
|
||||
PageHandler
|
||||
}
|
||||
|
||||
func (h *pageHandler) Page(ctx context.Context, in *PageReq, out *PageResp) error {
|
||||
return h.PageHandler.Page(ctx, in, out)
|
||||
}
|
||||
|
||||
// Api Endpoints for ShowProductDetail service
|
||||
|
||||
func NewShowProductDetailEndpoints() []*api.Endpoint {
|
||||
return []*api.Endpoint{}
|
||||
}
|
||||
|
||||
// Client API for ShowProductDetail service
|
||||
|
||||
type ShowProductDetailService interface {
|
||||
//rpc 服务
|
||||
ShowProductDetail(ctx context.Context, in *ProductDetailReq, opts ...client.CallOption) (*ProductDetailResp, error)
|
||||
}
|
||||
|
||||
type showProductDetailService struct {
|
||||
c client.Client
|
||||
name string
|
||||
}
|
||||
|
||||
func NewShowProductDetailService(name string, c client.Client) ShowProductDetailService {
|
||||
return &showProductDetailService{
|
||||
c: c,
|
||||
name: name,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *showProductDetailService) ShowProductDetail(ctx context.Context, in *ProductDetailReq, opts ...client.CallOption) (*ProductDetailResp, error) {
|
||||
req := c.c.NewRequest(c.name, "ShowProductDetail.ShowProductDetail", in)
|
||||
out := new(ProductDetailResp)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Server API for ShowProductDetail service
|
||||
|
||||
type ShowProductDetailHandler interface {
|
||||
//rpc 服务
|
||||
ShowProductDetail(context.Context, *ProductDetailReq, *ProductDetailResp) error
|
||||
}
|
||||
|
||||
func RegisterShowProductDetailHandler(s server.Server, hdlr ShowProductDetailHandler, opts ...server.HandlerOption) error {
|
||||
type showProductDetail interface {
|
||||
ShowProductDetail(ctx context.Context, in *ProductDetailReq, out *ProductDetailResp) error
|
||||
}
|
||||
type ShowProductDetail struct {
|
||||
showProductDetail
|
||||
}
|
||||
h := &showProductDetailHandler{hdlr}
|
||||
return s.Handle(s.NewHandler(&ShowProductDetail{h}, opts...))
|
||||
}
|
||||
|
||||
type showProductDetailHandler struct {
|
||||
ShowProductDetailHandler
|
||||
}
|
||||
|
||||
func (h *showProductDetailHandler) ShowProductDetail(ctx context.Context, in *ProductDetailReq, out *ProductDetailResp) error {
|
||||
return h.ShowProductDetailHandler.ShowProductDetail(ctx, in, out)
|
||||
}
|
||||
|
||||
// Api Endpoints for ShowProductSku service
|
||||
|
||||
func NewShowProductSkuEndpoints() []*api.Endpoint {
|
||||
return []*api.Endpoint{}
|
||||
}
|
||||
|
||||
// Client API for ShowProductSku service
|
||||
|
||||
type ShowProductSkuService interface {
|
||||
//rpc 服务
|
||||
ShowProductSku(ctx context.Context, in *ProductSkuReq, opts ...client.CallOption) (*ProductSkuResp, error)
|
||||
}
|
||||
|
||||
type showProductSkuService struct {
|
||||
c client.Client
|
||||
name string
|
||||
}
|
||||
|
||||
func NewShowProductSkuService(name string, c client.Client) ShowProductSkuService {
|
||||
return &showProductSkuService{
|
||||
c: c,
|
||||
name: name,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *showProductSkuService) ShowProductSku(ctx context.Context, in *ProductSkuReq, opts ...client.CallOption) (*ProductSkuResp, error) {
|
||||
req := c.c.NewRequest(c.name, "ShowProductSku.ShowProductSku", in)
|
||||
out := new(ProductSkuResp)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Server API for ShowProductSku service
|
||||
|
||||
type ShowProductSkuHandler interface {
|
||||
//rpc 服务
|
||||
ShowProductSku(context.Context, *ProductSkuReq, *ProductSkuResp) error
|
||||
}
|
||||
|
||||
func RegisterShowProductSkuHandler(s server.Server, hdlr ShowProductSkuHandler, opts ...server.HandlerOption) error {
|
||||
type showProductSku interface {
|
||||
ShowProductSku(ctx context.Context, in *ProductSkuReq, out *ProductSkuResp) error
|
||||
}
|
||||
type ShowProductSku struct {
|
||||
showProductSku
|
||||
}
|
||||
h := &showProductSkuHandler{hdlr}
|
||||
return s.Handle(s.NewHandler(&ShowProductSku{h}, opts...))
|
||||
}
|
||||
|
||||
type showProductSkuHandler struct {
|
||||
ShowProductSkuHandler
|
||||
}
|
||||
|
||||
func (h *showProductSkuHandler) ShowProductSku(ctx context.Context, in *ProductSkuReq, out *ProductSkuResp) error {
|
||||
return h.ShowProductSkuHandler.ShowProductSku(ctx, in, out)
|
||||
}
|
Loading…
Reference in new issue