Browse Source

加入包

yjp19871013@126.com 3 years ago
parent
commit
9d0aafa758
3 changed files with 110 additions and 0 deletions
  1. 76 0
      dapr_client/client.go
  2. 30 0
      dapr_client/dapr.go
  3. 4 0
      go.mod

+ 76 - 0
dapr_client/client.go

@@ -0,0 +1,76 @@
+package dapr_client
+
+import (
+	"context"
+	dapr "github.com/dapr/go-sdk/client"
+	"time"
+)
+
+type Client struct {
+	client     dapr.Client
+	timeoutSec int64
+}
+
+func newClient(timeoutSec int64) (*Client, error) {
+	client, err := dapr.NewClient()
+	if err != nil {
+		return nil, err
+	}
+
+	c := new(Client)
+	c.client = client
+	c.timeoutSec = timeoutSec
+
+	return c, nil
+}
+
+func destroyClient(client *Client) {
+	if client == nil {
+		return
+	}
+
+	client.client.Close()
+	client.client = nil
+	client = nil
+}
+
+func (client *Client) InvokeGetMethod(appID string, methodName string) ([]byte, error) {
+	return client.invokeMethod(appID, methodName, "get")
+}
+
+func (client *Client) InvokePostMethodJson(appID string, methodName string, jsonData []byte) ([]byte, error) {
+	return client.invokeMethodJson(appID, methodName, "post", jsonData)
+}
+
+func (client *Client) InvokePutMethodJson(appID string, methodName string, jsonData []byte) ([]byte, error) {
+	return client.invokeMethodJson(appID, methodName, "put", jsonData)
+}
+
+func (client *Client) InvokeDeleteMethod(appID string, methodName string) ([]byte, error) {
+	return client.invokeMethod(appID, methodName, "delete")
+}
+
+func (client *Client) Publish(pubsubName string, topic string, content []byte) error {
+	ctx, cancel := context.WithTimeout(context.Background(), time.Duration(client.timeoutSec)*time.Second)
+	defer cancel()
+
+	return client.client.PublishEvent(ctx, pubsubName, topic, content)
+}
+
+func (client *Client) invokeMethod(appID string, methodName string, verb string) ([]byte, error) {
+	ctx, cancel := context.WithTimeout(context.Background(), time.Duration(client.timeoutSec)*time.Second)
+	defer cancel()
+
+	return client.client.InvokeMethod(ctx, appID, methodName, verb)
+}
+
+func (client *Client) invokeMethodJson(appID string, methodName string, verb string, jsonData []byte) ([]byte, error) {
+	ctx, cancel := context.WithTimeout(context.Background(), time.Duration(client.timeoutSec)*time.Second)
+	defer cancel()
+
+	return client.client.InvokeMethodWithContent(ctx, appID, methodName, verb,
+		&dapr.DataContent{
+			Data:        jsonData,
+			ContentType: "application/json",
+		})
+}

+ 30 - 0
dapr_client/dapr.go

@@ -0,0 +1,30 @@
+package dapr_client
+
+import "sync"
+
+var clientInstance *Client
+var initOnce sync.Once
+var clientTimeoutSec int64
+
+func InitDAPR(timeoutSec int64) {
+	clientTimeoutSec = timeoutSec
+}
+
+func DestroyDAPR() {
+	if clientInstance != nil {
+		destroyClient(clientInstance)
+	}
+}
+
+func GetInstance() *Client {
+	initOnce.Do(func() {
+		client, err := newClient(clientTimeoutSec)
+		if err != nil {
+			panic(err)
+		}
+
+		clientInstance = client
+	})
+
+	return clientInstance
+}

+ 4 - 0
go.mod

@@ -0,0 +1,4 @@
+module gogs.sxidc.com/yjp/dapr_client
+
+go 1.16
+