This repository has been archived by the owner on Sep 15, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
185 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
run-client:export EGO_DEBUG=true | ||
run-client:export EGO_LOG_EXTRA_KEYS=X-Ego-Uid | ||
run-client: | ||
go run client/main.go --config=client/config.toml | ||
|
||
|
||
run-server:export EGO_DEBUG=true | ||
run-server:export EGO_LOG_EXTRA_KEYS=X-Ego-Uid | ||
run-server: | ||
go run server/main.go --config=server/config.toml | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[trace.jaeger] | ||
|
||
[grpc.test] | ||
debug = true # 开启后并加上export EGO_DEBUG=true,可以看到每次grpc请求,配置名、地址、耗时、请求数据、响应数据 | ||
addr = "127.0.0.1:9002" | ||
enableAccessInterceptor = true | ||
enableAccessInterceptorReq = true | ||
enableAccessInterceptorRes = true | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/davecgh/go-spew/spew" | ||
"github.com/gotomicro/ego/core/transport" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/metadata" | ||
|
||
"github.com/gotomicro/ego" | ||
"github.com/gotomicro/ego/client/egrpc" | ||
"github.com/gotomicro/ego/core/elog" | ||
"github.com/gotomicro/ego/examples/helloworld" | ||
) | ||
|
||
func main() { | ||
if err := ego.New().Invoker( | ||
invokerGrpc, | ||
callGrpc, | ||
).Run(); err != nil { | ||
elog.Error("startup", elog.FieldErr(err)) | ||
} | ||
} | ||
|
||
var grpcComp helloworld.GreeterClient | ||
|
||
func invokerGrpc() error { | ||
grpcConn := egrpc.Load("grpc.test").Build() | ||
grpcComp = helloworld.NewGreeterClient(grpcConn.ClientConn) | ||
return nil | ||
} | ||
|
||
func callGrpc() error { | ||
req := http.Request{} | ||
parentContext := transport.WithValue(req.Context(), "X-Ego-Uid", 9527) | ||
var headers metadata.MD | ||
var trailers metadata.MD | ||
_, err := grpcComp.SayHello(parentContext, &helloworld.HelloRequest{ | ||
Name: "i am client", | ||
}, grpc.Header(&headers), grpc.Trailer(&trailers)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
spew.Dump(headers) | ||
spew.Dump(trailers) | ||
_, err = grpcComp.SayHello(parentContext, &helloworld.HelloRequest{ | ||
Name: "error", | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[mysql.test] | ||
# debug = true # ego重写gorm debug,打开后可以看到,配置名、地址、耗时、请求数据、响应数据 | ||
# rawDebug = true 原生gorm debug | ||
dsn = "root:root@tcp(127.0.0.1:3306)/test?charset=utf8&parseTime=True&loc=Local&readTimeout=1s&timeout=1s&writeTimeout=3s" | ||
onFail = "panic" # 失败后,直接fail fast,panic操作 | ||
# connMaxLifetime = "300s" | ||
# maxIdleConns = 50 | ||
# maxOpenConns = 100 | ||
# disableTrace = false | ||
# disableMetric = false | ||
enableDetailSQL = true | ||
enableAccessInterceptor = true | ||
enableAccessInterceptorReq = true | ||
enableAccessInterceptorRes = true | ||
[server.grpc] | ||
port = 9002 | ||
enableAccessInterceptorReq = true | ||
enableAccessInterceptorRes = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/gotomicro/ego" | ||
"github.com/gotomicro/ego-component/egorm" | ||
"github.com/gotomicro/ego/core/elog" | ||
"github.com/gotomicro/ego/examples/helloworld" | ||
"github.com/gotomicro/ego/server" | ||
"github.com/gotomicro/ego/server/egrpc" | ||
"gorm.io/gorm" | ||
) | ||
|
||
var db *gorm.DB | ||
|
||
type User struct { | ||
Id int `gorm:"not null" json:"id"` | ||
Nickname string `gorm:"not null" json:"name"` | ||
} | ||
|
||
func (User) TableName() string { | ||
return "user" | ||
} | ||
|
||
// export EGO_DEBUG=true && go run main.go --config=config.toml | ||
func main() { | ||
if err := ego.New().Invoker(func() error { | ||
db = egorm.Load("mysql.test").Build() | ||
return nil | ||
}).Serve(func() server.Server { | ||
server := egrpc.Load("server.grpc").Build() | ||
helloworld.RegisterGreeterServer(server.Server, &Greeter{server: server}) | ||
return server | ||
}()).Run(); err != nil { | ||
elog.Panic("startup", elog.FieldErr(err)) | ||
} | ||
} | ||
|
||
// Greeter ... | ||
type Greeter struct { | ||
server *egrpc.Component | ||
helloworld.UnimplementedGreeterServer | ||
} | ||
|
||
// SayHello ... | ||
func (g Greeter) SayHello(ctx context.Context, request *helloworld.HelloRequest) (*helloworld.HelloReply, error) { | ||
var user User | ||
err := db.WithContext(ctx).Where("id = ?", 100).First(&user).Error | ||
if err != nil { | ||
return nil, fmt.Errorf("sql err: %w", err) | ||
} | ||
return &helloworld.HelloReply{ | ||
Message: "Hello EGO, I'm " + g.server.Address(), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters