1
0
mirror of https://github.com/chai2010/advanced-go-programming-book.git synced 2025-05-25 05:02:23 +00:00

Merge branch 'master' of github.com:chai2010/advanced-go-programming-book

This commit is contained in:
Xargin 2018-08-07 11:32:21 +08:00
commit 3d4dd73ffe
5 changed files with 16 additions and 9 deletions

View File

@ -16,6 +16,9 @@
- 深入CGO编程: https://github.com/chai2010/gopherchina2018-cgo-talk - 深入CGO编程: https://github.com/chai2010/gopherchina2018-cgo-talk
## 关注微信公众号(golang-china)
![](weixin-golang-china.jpg)
## 版权声明 ## 版权声明

View File

@ -262,11 +262,11 @@ func main() {
```protobuf ```protobuf
service PubsubService { service PubsubService {
rpc Publish (String) returns (String); rpc Publish (String) returns (String);
rpc SubscribeTopic (String) returns (stream String); rpc Subscribe (String) returns (stream String);
} }
``` ```
其中Publish是普通的RPC方法SubscribeTopic则是一个单向的流服务。然后grpc插件会为服务端和客户端生成对应的接口 其中Publish是普通的RPC方法Subscribe则是一个单向的流服务。然后grpc插件会为服务端和客户端生成对应的接口
```go ```go
type PubsubServiceServer interface { type PubsubServiceServer interface {
@ -286,7 +286,7 @@ type HelloService_SubscribeServer interface {
} }
``` ```
因为SubscribeTopic是服务端的单向流因此生成的HelloService_SubscribeServer接口中只有Send方法。 因为Subscribe是服务端的单向流因此生成的HelloService_SubscribeServer接口中只有Send方法。
然后就可以实现发布和订阅服务了: 然后就可以实现发布和订阅服务了:
@ -315,9 +315,9 @@ func (p *PubsubService) Publish(
func (p *PubsubService) Subscribe( func (p *PubsubService) Subscribe(
arg *String, stream PubsubService_SubscribeServer, arg *String, stream PubsubService_SubscribeServer,
) error { ) error {
ch := p.SubscribeTopic(func(v interface{}) bool { ch := p.pub.SubscribeTopic(func(v interface{}) bool {
if key, ok := v.(string); ok { if key, ok := v.(string); ok {
if strings.Hasprefix(arg.GetValue()) { if strings.HasPrefix(key,arg.GetValue()) {
return true return true
} }
} }
@ -346,11 +346,11 @@ func main() {
client := NewPubsubServiceClient(conn) client := NewPubsubServiceClient(conn)
reply, err := client.Publish(context.Background(), &String{Value: "golang: hello Go"}) _, err = client.Publish(context.Background(), &String{Value: "golang: hello Go"})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
reply, err := client.Publish(context.Background(), &String{Value: "docker: hello Docker"}) _, err = client.Publish(context.Background(), &String{Value: "docker: hello Docker"})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -368,7 +368,7 @@ func main() {
defer conn.Close() defer conn.Close()
client := NewPubsubServiceClient(conn) client := NewPubsubServiceClient(conn)
stream, err := client.Channel(context.Background(), &String{Value: "golang:"}) stream, err := client.SubscribeTopic(context.Background(), &String{Value: "golang:"})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -4,7 +4,7 @@
## 4.5.1 证书认证 ## 4.5.1 证书认证
GRPC建立在HTTP/2协议之上对TLS提供了很好的支持。我们前面章节中GRPC的服务都没有提供证书支持因此客户端在链接服务器中通过`grpc.WithInsecure()`选项跳过了对服务器证书的验证。没有启用证书的GRPC服务在和客户端进行的是明文通讯信息面临被任何第三方监听的风险。为了保障GRPC通信不被第三方监听改或伪造我们可以对服务器启动TLS加密特性。 GRPC建立在HTTP/2协议之上对TLS提供了很好的支持。我们前面章节中GRPC的服务都没有提供证书支持因此客户端在链接服务器中通过`grpc.WithInsecure()`选项跳过了对服务器证书的验证。没有启用证书的GRPC服务在和客户端进行的是明文通讯信息面临被任何第三方监听的风险。为了保障GRPC通信不被第三方监听改或伪造我们可以对服务器启动TLS加密特性。
可以用以下命令为服务器和客户端分别生成私钥和证书: 可以用以下命令为服务器和客户端分别生成私钥和证书:

View File

@ -11,6 +11,10 @@
- https://www.gitbook.com/book/chai2010/advanced-go-programming-book/ - https://www.gitbook.com/book/chai2010/advanced-go-programming-book/
## 关注微信公众号(golang-china)
![](weixin-golang-china.jpg)
## 版权声明 ## 版权声明
[Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-nc-sa/4.0/)。 [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-nc-sa/4.0/)。

BIN
weixin-golang-china.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB