commit
3279839221
@ -0,0 +1,10 @@ |
||||
# Run the example |
||||
|
||||
1、Compile and execute the server code: |
||||
```shell |
||||
$ go run server/main.go |
||||
``` |
||||
2、From a different terminal, compile and execute the client code: |
||||
```shell |
||||
$ go run client/main.go |
||||
``` |
@ -0,0 +1,64 @@ |
||||
|
||||
-----BEGIN CERTIFICATE----- |
||||
MIIFZTCCA02gAwIBAgIBATANBgkqhkiG9w0BAQUFADB8MQswCQYDVQQGEwJDTjEL |
||||
MAkGA1UECAwCU0gxCzAJBgNVBAcMAlNIMQ8wDQYDVQQKDAZrcmF0b3MxCzAJBgNV |
||||
BAsMAklUMRMwEQYDVQQDDAprcmF0b3MuY29tMSAwHgYJKoZIhvcNAQkBFhFrcmF0 |
||||
b3NAa3JhdG9zLmNvbTAeFw0yMTA3MjcxNTM1MjJaFw0yMjA3MjcxNTM1MjJaME0x |
||||
CzAJBgNVBAYTAkNOMQswCQYDVQQIDAJTSDELMAkGA1UEBwwCU0gxDzANBgNVBAoM |
||||
BmtyYXRvczETMBEGA1UEAwwKa3JhdG9zLmNvbTCCAiIwDQYJKoZIhvcNAQEBBQAD |
||||
ggIPADCCAgoCggIBALfEWAuPqUHTwTiOT8dtiCM3aEm1D7I6K/PY2mSDMMOI4f5z |
||||
TYi7LsKLLItMQR44cEDein/kl0U0QNYJRHqVCr/3IXA4ds0maiq+npY/S2KABDiI |
||||
Z38TZ1PZ2bcD3Jb0o1gw0GcSokOGzVtuNKSiASc3D711AepGerChD5UrbixrZdg2 |
||||
wAeWZj39Tl93+zCcldVrCHMSM7LmDluHJ5KZ8T6auuK6ypQPVhqLz8VseHhB/IRw |
||||
s9/o9OlJ3kv4wB/CWlFIvU6ZGVnshZAGOk3Brq25kw600RRDr2MpxNhFY1Xvrt7D |
||||
tAJ7fK3/3VaV+I2C4OzzmztK7T1WlL8XqZj6I3t1ZCZryMvDIUPzC4mgiRqEJiF7 |
||||
VzuVm1sjyTKkD4oX6ZjJYXJ/6pbvd8+AexuwVAsQ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11opfl5R86GpXXbz+vNL9fWP+8cIvoGZK8RAC872bcMPEoJPjPIwAbI= |
||||
-----END CERTIFICATE----- |
||||
-----BEGIN CERTIFICATE----- |
||||
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 |
||||
djovWE7E6ic= |
||||
-----END CERTIFICATE----- |
@ -0,0 +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pfTsRFUHsJVCzB3aXffCmX0L |
||||
+yqEMoX3B+R+EdTv3SF79K5a3bAA6VQtpnwHQ83PTK7MTTDepSdM+ft+6D++iGYS |
||||
GU5+HGahjSg+iZti9feaOUIT5OfcXjoYUT3B6+rhgTQO2OMpq3QtSJhK7ZnP6HnZ |
||||
URRaawDhyJSqOO8Z5Z/bdaMSJHWcR1huaHF7yGoPHR06xBzaX82tsHiZbZx5pg8= |
||||
-----END RSA PRIVATE KEY----- |
@ -0,0 +1,63 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"context" |
||||
"crypto/tls" |
||||
"crypto/x509" |
||||
"io/ioutil" |
||||
"log" |
||||
|
||||
pb "github.com/go-kratos/kratos/examples/helloworld/helloworld" |
||||
"github.com/go-kratos/kratos/v2/transport/grpc" |
||||
"github.com/go-kratos/kratos/v2/transport/http" |
||||
) |
||||
|
||||
func main() { |
||||
b, err := ioutil.ReadFile("../cert/server.crt") |
||||
if err != nil { |
||||
panic(err) |
||||
} |
||||
cp := x509.NewCertPool() |
||||
if !cp.AppendCertsFromPEM(b) { |
||||
panic(err) |
||||
} |
||||
tlsConf := &tls.Config{ServerName: "www.kratos.com", RootCAs: cp} |
||||
callHTTP(tlsConf) |
||||
callGRPC(tlsConf) |
||||
} |
||||
|
||||
func callHTTP(tlsConf *tls.Config) { |
||||
conn, err := http.NewClient( |
||||
context.Background(), |
||||
http.WithEndpoint("https://127.0.0.1:8000"), |
||||
http.WithTLSConfig(tlsConf), |
||||
) |
||||
if err != nil { |
||||
panic(err) |
||||
} |
||||
defer conn.Close() |
||||
client := pb.NewGreeterHTTPClient(conn) |
||||
reply, err := client.SayHello(context.Background(), &pb.HelloRequest{Name: "kratos"}) |
||||
if err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
log.Printf("[http] SayHello %s\n", reply.Message) |
||||
} |
||||
|
||||
func callGRPC(tlsConf *tls.Config) { |
||||
conn, err := grpc.Dial( |
||||
context.Background(), |
||||
grpc.WithEndpoint("127.0.0.1:9000"), |
||||
grpc.WithTLSConfig(tlsConf), |
||||
) |
||||
if err != nil { |
||||
panic(err) |
||||
} |
||||
defer conn.Close() |
||||
client := pb.NewGreeterClient(conn) |
||||
reply, err := client.SayHello(context.Background(), &pb.HelloRequest{Name: "kratos"}) |
||||
if err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
log.Printf("[grpc] SayHello %+v\n", reply) |
||||
} |
@ -0,0 +1,63 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"context" |
||||
"crypto/tls" |
||||
"fmt" |
||||
"log" |
||||
|
||||
"github.com/go-kratos/kratos/examples/helloworld/helloworld" |
||||
"github.com/go-kratos/kratos/v2" |
||||
"github.com/go-kratos/kratos/v2/transport/grpc" |
||||
"github.com/go-kratos/kratos/v2/transport/http" |
||||
) |
||||
|
||||
// go build -ldflags "-X main.Version=x.y.z"
|
||||
var ( |
||||
// Name is the name of the compiled software.
|
||||
Name = "helloworld" |
||||
// Version is the version of the compiled software.
|
||||
Version = "v1.0.0" |
||||
) |
||||
|
||||
// server is used to implement helloworld.GreeterServer.
|
||||
type server struct { |
||||
helloworld.UnimplementedGreeterServer |
||||
} |
||||
|
||||
// SayHello implements helloworld.GreeterServer
|
||||
func (s *server) SayHello(ctx context.Context, in *helloworld.HelloRequest) (*helloworld.HelloReply, error) { |
||||
return &helloworld.HelloReply{Message: fmt.Sprintf("Hello %+v", in.Name)}, nil |
||||
} |
||||
|
||||
func main() { |
||||
cert, err := tls.LoadX509KeyPair("../cert/server.crt", "../cert/server.key") |
||||
if err != nil { |
||||
panic(err) |
||||
} |
||||
tlsConf := &tls.Config{Certificates: []tls.Certificate{cert}} |
||||
|
||||
s := &server{} |
||||
httpSrv := http.NewServer( |
||||
http.Address(":8000"), |
||||
http.TLSConfig(tlsConf), |
||||
) |
||||
grpcSrv := grpc.NewServer( |
||||
grpc.Address(":9000"), |
||||
grpc.TLSConfig(tlsConf), |
||||
) |
||||
helloworld.RegisterGreeterServer(grpcSrv, s) |
||||
helloworld.RegisterGreeterHTTPServer(httpSrv, s) |
||||
|
||||
app := kratos.New( |
||||
kratos.Name(Name), |
||||
kratos.Server( |
||||
httpSrv, |
||||
grpcSrv, |
||||
), |
||||
) |
||||
|
||||
if err := app.Run(); err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
} |
@ -0,0 +1,29 @@ |
||||
package http |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func TestParseTarget(t *testing.T) { |
||||
target, err := parseTarget("localhost:8000", false) |
||||
assert.Nil(t, err) |
||||
assert.Equal(t, &Target{Scheme: "http", Authority: "localhost:8000"}, target) |
||||
|
||||
target, err = parseTarget("discovery:///demo", false) |
||||
assert.Nil(t, err) |
||||
assert.Equal(t, &Target{Scheme: "discovery", Authority: "", Endpoint: "demo"}, target) |
||||
|
||||
target, err = parseTarget("127.0.0.1:8000", false) |
||||
assert.Nil(t, err) |
||||
assert.Equal(t, &Target{Scheme: "http", Authority: "127.0.0.1:8000"}, target) |
||||
|
||||
target, err = parseTarget("https://127.0.0.1:8000", false) |
||||
assert.Nil(t, err) |
||||
assert.Equal(t, &Target{Scheme: "https", Authority: "127.0.0.1:8000"}, target) |
||||
|
||||
target, err = parseTarget("127.0.0.1:8000", true) |
||||
assert.Nil(t, err) |
||||
assert.Equal(t, &Target{Scheme: "https", Authority: "127.0.0.1:8000"}, target) |
||||
} |
Loading…
Reference in new issue