1. 负载均衡
import (
"context"
"fmt"
"/micro/go-micro/client"
"/micro/go-micro/client/selector"
"/micro/go-micro/registry"
"/micro/go-micro/registry/etcd"
myhttp "/micro/go-plugins/client/http"
"/micro/go-plugins/registry/consul"
Models "go-micro-study/bak/models"
"io/ioutil"
"log"
"net/http"
)
func callApi2(s ) {
myClient := ((s),
("application/json"),
)
//请求注册中心里面的服务
request := ("prodservice", "/v1/prods", {Size: 1})
var resp
err := ((), request, &resp)
if err != nil {
(err)
}
()
return
}
func main() {
etcdReg := (("127.0.0.1:2379"))
mySeletor := ( //负载均衡插件
(etcdReg ),
(), //随机
)
callApi2(mySeletor)
}
参数检查:
第三方validator库使用
package main
import (
"/go-playground/validator.v9"
"log"
"micro-tools/AppLib"
"testing"
)
type Users struct {
Username string `validate:"required,min=6,max=20" vmsg:"用户名必须大于6位以上"`
Userpwd string `validate:"required,min=6,max=18" vmsg:"用户密码必须大于6位以上"`
//TestName string `validate:"required,username" vmsg:"用户名不正确"`
Usertags []string `validate:"required,min=1,max=5,unique,dive,usertag" vmsg:"用户标签不合法"`
}
func TestStruct(t *) {
userTags := []string{"aa", "bb","cc","dd","ee"}
valid := ()
_ = ("usertag", "^[\u4e00-\u9fa5a-zA-Z0-9]{2,4}$", valid)
//_ = AppLib