目录
**统一Web服务框架讲解: **
web-client&server
- 概要
- 一键生成神器1. 安装webcli120工具
go install git.ichub.com/general/webcli120/cmd/webcli120
go get git.ichub.com/general/webcli120/cmd/webcli120
- 一键生成WEB工程
- 创建一个工程,有go.mod
go.mod添加replace google.golang.org/grpc => google.golang.org/grpc v1.26.0
- 创建config文件
go get git.ichub.com/general/webcli120/cmd/webcli120
Webcli120 install config
- 创建整个工程文件
Webcli120 install all
(func installAll(cmd *cobra.Command, args []string) {
installConfig(cmd, args)
installWebServer(cmd, args)
installCmd(cmd, args)
installDocker(cmd, args)
installDocx(cmd, args)
installTest(cmd, args)
}
- 执行:
Go mod tidy
Goinstall.bat
- 验证
awesomeProject stat:查询性能统计
- **配置文件 **1. 切换环境:
Env: ${ICHUB_ENV:dev} 代表使用ichub-dev.yml配置
支持多个配置文件切换,
支持环境变量:
hosturl: ${ETCD_HOSTURL:192.168.27.27:2379}
Docker 容器配置ETCD_HOSTURL 则可修改配置
支持加密
password: ${ES_PASSWORD:enc(8CfGuLQ79iK6e6+UU/yXiA==)}
webcli120 enc 123456
8CfGuLQ79iK6e6+UU/yXiA== -》enc(8CfGuLQ79iK6e6+UU/yXiA==)
****支持指定服务地址: ****
client:
name: www.general.website.com
testUrl: ${ICHUB_TESTURL:http://localhost:100}
指向配置的 url
testUrl: #${ICHUB_TESTURL:http://localhost:100}
testUrl通过etcd注册中心查询www.general.website.com服务地址。
- 配置文件 ichub-dev.yml
etcd: 环境切换只要改etcd,
server:
hosturl: ${ETCD_HOSTURL:192.168.27.27:2379}
web: --配置
server: 本服务server 配置
name: ${WEB_SERVER_HOST:www.general.website.com}
port: 100
client: --本服务CLIENT配置
name: ${WEB_CLIENT_HOST:www.general.website.com}
testUrl: ${ICHUB_TESTURL:http://localhost:100}
webTimeout: 30
swagger: 本服务swagger配置
enable: ${SWAGGER_ENABLE:true}
host: ${SWAGGER_HOST:localhost:100}
basePath: /
version: v1.0
title: ${SWAGGER_TITLE:网站管理服务-WebsiteServer}
- 连接其它服务配置客户端
esclient: 连接esserver
name: web.esserver.com
testUrl: ${WEB_ESCLIENT_TESTURL:http://localhost:88}
webTimeout: 30
ruleclient: 连接规则引擎
name: web.ruleserver.com
testUrl: ${WEB_RULECLIENT_TESTURL:http://localhost:89}
webTimeout: 30
- 前端服务路由配置:继承原来
general_website:
name: www.general.website.com
address: 0.0.0.0
port: 100
运行模式(debug:调试,test:测试,release:正式)
runMode: debug
- godi依赖****注入1. 编写****代码:单例 basedto.BaseEntitySingle
type ****SingleEntity ****struct {
basedto.BaseEntitySingle
MultiBindNew *multi.MultiEntity godi:"bind"
Id int
Name string
}
func NewSingleEntity() *SingleEntity {
var s = &SingleEntity{
Name: "single",
}
s.InitProxy(s)
return s
}
- 编写****代码:多实例 basedto.BaseEntity
type ****MultiEntity ****struct {
basedto.BaseEntity
Id int
Name string
myeentity *MyEntity `godi:"auto"`
}
func NewMultiEntity() *MultiEntity {
var s = &MultiEntity{
Name: "multi",
}
s.InitProxy(s)
return s
}
- 执行生成goid注入****代码:
Webcli120 godi all
使用测试:多实例
func Test001_FindMulti(t *testing.T) { //var bean1 = FindBeanMultiEntity()
var bean2 = FindBeanMultiEntity()
var bean3 = FindBeanMultiEntity()
bean2.Name = "123"
logrus.Info(bean2, bean3)
assert.Equal(t, bean3.Name != bean2.Name, true)
}
单实例
func Test002_FindSingle(t *testing.T) {
var singleEntity = FindBeanSingleEntity()
singleEntity.Name = "aaa"
logrus.Info(singleEntity.ToPrettyString())
var sa = FindBeanSingleEntity()
var sb = FindBeanSingleEntity()
sb.Name = "bbb"
logrus.Info(singleEntity, sa, sb)
assert.Equal(t, sa.Name == singleEntity.Name, true)
assert.Equal(t, sb.Name == singleEntity.Name, true)
}
- 根据struct自动生成测试套
webcli120 suite ConfigAllFuncService
- 编写WEB REST代码
Server/service目录创建一个目录如website
创建一个接口文件,一个接口一个struct
配置路由func (this *WebSiteFuncService) init() *WebSiteFuncService {
this.WebGroupPath = "/raymond"
this.InitRoute(this, http.MethodGet, "/website")
return this
}
编写swagger 注释
// @Summary website
// @Tags 网站管理
// @Produce json
// @Success 200 {object} basedto.IchubResult "成功"
// @Failure 400 {object} string "请求错误"
// @Failure 500 {object} string "内部错误"
// @Router /website [get]
func (this *WebSiteFuncService) Execute(c *gin.Context) {
c.IndentedJSON(http.StatusOK, (&basedto.IchubResult{}).SuccessData("ok"))
}
注册
func Register() {
NewWebSiteFuncService().Register()
}
router目录下总注册
func Register() *webserver.WebHandlers {
file.Register()
config.Register()
gocenter.Register()
website.Register()
return funchandler.Build()
}
- 生成****swagger
进入server目录执行swagini.bat生成
- 运行服务
Go mod tidy
Goinstall.bat
- 执行Swagger
- 测试代码 :
- Webclient: WEB客户端
func TestWebSuites(t *testing.T) {
suite.Run(t, new(TestWebSuite))
}
// 每个测试运行前,会执行
func (suite *TestWebSuite) SetupTest() {
ichublog.InitLogrus()
fileutils.FindRootDir()
logrus.Info("SetupTest")
var config = ichubconfig.Default()
clientDto := config.ReadIchubWebClient()
suite.webclient = webclient.New(clientDto)
suite.statwebcli = webclient.DefaultStat()
}
func (suite *TestWebSuite) Test0001_WebClient() {
logrus.Info(suite.webclient)
var result = suite.webclient.Get("")
logrus.Info(result)
}
- ESWebClient: ES客户端
go get -u gitlab.ichub.com/general/esserver
Goinstall.bat
通用查询
func (this *TestWebQuerySuite) Test003_ShouldQueryDept() {
var queryRequest = pagereq.NewQueryRequest()
queryRequest.PageSize = 2
queryRequest.IndexName = "ichub_sys_dept"
queryRequest.EsTerms("dept_name", "若依科技", "kl")
queryRequest.EsTerms("dept_name", "若依科技", "kl")
queryRequest.Source = "dept_id,dept_name,order_nm,create_time"
queryRequest.OrderByDesc("create_time")
var result = this.EsWebClient.Should(queryRequest).As(&[]*model.SysDept{})
queryRequest.Save2File(queryRequest, result)
logrus.Info(result)
//check
this.Equal(200, result.Code)
}
执行结果
- RuleWebClient : 规则引擎客户端
go get -u gitlab.ichub.com/general/ruleserver
Goinstall.bat
动态执行规则脚本
- git路径1. ES客户端:简化版本
http://git.ichub.com/general/webcli.git
- WEB框架全集:
http://git.ichub.com/general/webcli120.git
- ESSERVER统一搜索服务
http://[email protected]/ichub2/esserver.git
- RULESERVER统一规则引擎服务
http://[email protected]/ichub2/ruleserver.git
- 网站管理独立微服务
http://[email protected]/general/website-management.git
vv
版权归原作者 leijmdas 所有, 如有侵权,请联系我们删除。