You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
2.5 KiB
90 lines
2.5 KiB
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()
|
|
}
|