主程序:
package main
import (
"context"
"flag"
"net/http"
"os"
"mtcomm/db/mysql"
"mtcomm/db/redis"
logger "mtcomm/log"
stdprometheus "github.com/prometheus/client_golang/prometheus"
"github.com/tjz101/goprop"
kitprometheus "github.com/go-kit/kit/metrics/prometheus"
"github.com/go-kit/kit/tracing/opentracing"
httptransport "github.com/go-kit/kit/transport/http"
stdopentracing "github.com/opentracing/opentracing-go"
zipkin "github.com/openzipkin/zipkin-go-opentracing"
)
var (
namespace string
serviceName string
mysqlClient mysql.MysqlClient
redisClient redis.RedisClient
tracer stdopentracing.Tracer
prop *goprop.Prop
log *logger.Logger
)
func init() {
/* init properties */
propFile := flag.String("prop", "prop.properties", "properties file")
flag.Parse()
prop = goprop.NewProp()
prop.Read(*propFile)
namespace = prop.Get("namespace") //kubernetes namespace
serviceName = prop.Get("serviceName") //kubernetes service name
/* init log */
logger.SetDefaultLogLevel(logger.LevelDebug)
logger.With("serviceName", serviceName)
log = logger.GetDefaultLogger()
/* init mysql */
mysqlClient = mysql.NewMysqlClient(&mysql.MysqlInfo{
UserName: prop.Get("mysql_username"),
Password: prop.Get("mysql_password"),
IP: prop.Get("mysql_ip"),
Port: prop.Get("mysql_port"),
DatabaseName: prop.Get("mysql_database"),
Logger: logger.GetDefaultLogger(),
})
/* init redis */
redisClient = redis.NewRedisClient(&redis.RedisServerInfo{
Ctx: context.TODO(),
Logger: logger.GetDefaultLogger(),
RedisHost: prop.Get("redis_host"),
})
}
func main() {
// init
zipkinAddr := prop.Get("zipkinAddr")
listenPort := prop.Get("listenPort")
options := []httptransport.ServerOption{
httptransport.ServerErrorLogger(*logger.GetDefaultLogger().GetDefaultKitLogger()),
}
// init tracing domain.
{
if zipkinAddr != "" {
logger.Info("tracer", "Zipkin", "zipkinAddr", zipkinAddr)
collector, err := zipkin.NewHTTPCollector(zipkinAddr, zipkin.HTTPBatchSize(1))
if err != nil {
logger.Error("tracer", "Zipkin", "err", err)
os.Exit(1)
}
tracer, err = zipkin.NewTracer(
zipkin.NewRecorder(collector, false, listenPort, serviceName),
)
if err != nil {
logger.Error("tracer", "Zipkin", "err", err)
os.Exit(1)
}
} else {
logger.Info("tracer", "none")
tracer = stdopentracing.GlobalTracer() // no-op
}
}
fieldKeys := []string{"method", "error"}
requestCount := kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{
Namespace: namespace,
Subsystem: serviceName,
Name: "request_count",
Help: "Number of requests received.",
}, fieldKeys)
requestLatency := kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{
Namespace: namespace,
Subsystem: serviceName,
Name: "request_latency_microseconds",
Help: "Total duration of requests in microseconds.",
}, fieldKeys)
/* create service */
var svc UserService
svc = userService{}
svc = loggingMiddleware{svc}
svc = instrumentingMiddleware{requestCount, requestLatency, svc}
// 保存请求
saveUserHandler := httptransport.NewServer(
opentracing.TraceServer(tracer, "SaveUser")(makeSaveUserEndpoint(svc)),
decodeSaveUserRequest,
encodeResponse,
append(options, httptransport.ServerBefore(opentracing.HTTPToContext(tracer, "SaveUser", *logger.GetDefaultLogger().GetDefaultKitLogger())))...,
)
// 删除请求
delUserHandler := httptransport.NewServer(
opentracing.TraceServer(tracer, "DelUser")(makeDelUserEndpoint(svc)),
decodeDelUserRequest,
encodeResponse,
append(options, httptransport.ServerBefore(opentracing.HTTPToContext(tracer, "DelUser", *logger.GetDefaultLogger().GetDefaultKitLogger())))...,
)
// 修改请求
updateUserHandler := httptransport.NewServer(
opentracing.TraceServer(tracer, "UpdateUser")(makeUpdateUserEndpoint(svc)),
decodeUpdateUserRequest,
encodeResponse,
append(options, httptransport.ServerBefore(opentracing.HTTPToContext(tracer, "UpdateUser", *logger.GetDefaultLogger().GetDefaultKitLogger())))...,
)
// 查询请求
selectAllUserHandler := httptransport.NewServer(
opentracing.TraceServer(tracer, "SelectAllUser")(makeSelectAllUserEndpoint(svc)),
decodeSelectAllUserRequest,
encodeResponse,
append(options, httptransport.ServerBefore(opentracing.HTTPToContext(tracer, "SelectAllUser", *logger.GetDefaultLogger().GetDefaultKitLogger())))...,
)
http.Handle("/saveUser", saveUserHandler)
http.Handle("/delUser", delUserHandler)
http.Handle("/updateUser", updateUserHandler)
http.Handle("/selectAllUser", selectAllUserHandler)
http.Handle("/metrics", stdprometheus.Handler())
logger.Info("msg", "HTTP", "addr", listenPort)
logger.Info("err", http.ListenAndServe(listenPort, nil))
}
错误信息:
GOROOT=C:\Go #gosetup
GOPATH=C:\gowork #gosetup
C:\Go\bin\go.exe build -i -o C:\Users\123\AppData\Local\Temp\___go_build_main_go.exe C:/gowork/src/user/main.go #gosetup
# command-line-arguments
user\main.go:112:10: undefined: UserService
user\main.go:113:8: undefined: userService
user\main.go:114:8: undefined: loggingMiddleware
user\main.go:115:8: undefined: instrumentingMiddleware
user\main.go:119:47: undefined: makeSaveUserEndpoint
user\main.go:120:3: undefined: decodeSaveUserRequest
user\main.go:121:3: undefined: encodeResponse
user\main.go:127:46: undefined: makeDelUserEndpoint
user\main.go:128:3: undefined: decodeDelUserRequest
user\main.go:129:3: undefined: encodeResponse
user\main.go:129:3: too many errors
求解:go程序能在liteide中运行,但再GoLand中运行就会出项如下错误command-line-arguments user\main.go:112:10: undefined: UserService
Choxsan · · 2364 次点击更多评论
<a href="/user/tk103331" title="@tk103331">@tk103331</a> 真的是这个问题,谢谢了!但是程序还是在GoLang上运行不起,又是读不到数据库,郁闷了
#2