联犀 联犀
首页
使用指南
开发指南
技术分享
  • 中台接口 (opens new window)
  • 物联网接口 (opens new window)
在线体验 (opens new window)
  • gitee (opens new window)
  • github (opens new window)
  • 边缘网关-RHILEX (opens new window)
  • 边缘网关-opengw (opens new window)
首页
使用指南
开发指南
技术分享
  • 中台接口 (opens new window)
  • 物联网接口 (opens new window)
在线体验 (opens new window)
  • gitee (opens new window)
  • github (opens new window)
  • 边缘网关-RHILEX (opens new window)
  • 边缘网关-opengw (opens new window)
  • 应用开发

  • 后端开发

    • 服务开发

      • 服务初始化
      • 连接联犀服务
        • 配置初始化
        • 服务初始化
        • 使用
      • 中间件使用
      • 授权定义
      • swagger使用
      • 数据库使用
      • 第三方接入
      • 插槽使用
      • 数据分析模块使用
      • 消息队列
      • 分布式缓存
      • 定时任务
      • 分布式存储使用
    • 物联网

  • 开发指南
  • 后端开发
  • 服务开发
godLei6
2024-10-30
目录

连接联犀服务

当我们开发自己的服务的时候调用联犀的rpc便会是我们必须要做的一件事情,在本文中带大家一起实现rpc调用.
ps: 请先运行起联犀的服务,docker和代码直接运行都可以教程

# 配置初始化

我们以获取控制设备开灯为例进行测试

  1. 在服务的 internal/config/config.go 的Config结构体中添加以下配置
	DmRpc      conf.RpcClientConf `json:",optional"`
1
  1. 在 etc/xxx.yaml 服务配置文件中添加对应的配置文件
  • 如果是etcd模式,key可以参考dmsvr的 etcd注册key
DmRpc:
  Conf:
    Timeout: 1000000
    Etcd:
      Hosts:
        - localhost:2379
      Key: dm.rpc
1
2
3
4
5
6
7
  • 如果是直连模式则直接填写以下格式配置 具体的ip和端口参考dmsvr 监听的ip和端口
DmRpc:
  Conf:
    Timeout: 1000000
    Endpoints:
      - localhost:9081
1
2
3
4
5

# 服务初始化

在gozero中所有的依赖都定义在 internal/svc/serviceContext.go 中,我们的服务初始化也定义在这里.
我们这里是需要进行设备控制,首先我们需要查看dmsvr的proto,来找到对应的接口 things/service/dmsvr/proto/dm.proto:269 可以在对应的位置上看到我们需要调用的接口定义: 其中service为DeviceInteract,我们打开things/service/dmsvr/client/deviceinteract/deviceInteract.go client这个文件夹下是代码自动生成的proto实例,业务直接引入对应的包就可以使用
引入代码示例:

package svc

import (
	"gitee.com/unitedrhino/share/ctxs"
	"gitee.com/unitedrhino/share/eventBus"
	"gitee.com/unitedrhino/share/utils"
	"gitee.com/unitedrhino/things/service/dmsvr/client/deviceinteract"
	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/core/stores/kv"
	"github.com/zeromicro/go-zero/rest"
	"github.com/zeromicro/go-zero/zrpc"
	"lightsvr/internal/config"
)

type ServiceContext struct {
	Config         config.Config
	InitCtxsWare   rest.Middleware
	DeviceInteract deviceinteract.DeviceInteract
	FastEvent      *eventBus.FastEvent
	Store          kv.Store
	NodeID         int64
}

func NewServiceContext(c config.Config) *ServiceContext {
	var (
		deviceInteract deviceinteract.DeviceInteract
	)
	deviceInteract = deviceinteract.NewDeviceInteract(zrpc.MustNewClient(c.DmRpc.Conf))

	nodeID := utils.GetNodeID(c.CacheRedis, c.Name)

	fastEvent, err := eventBus.NewFastEvent(c.Event, c.Name, nodeID)
	logx.Must(err)
	return &ServiceContext{
		Config:         c,
		InitCtxsWare:   ctxs.InitMiddleware,
		FastEvent:      fastEvent,
		DeviceInteract: deviceInteract,
		Store:          kv.NewStore(c.CacheRedis),
	}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

# 使用

直接调用即可,示例如下

ret, err := svcCtx.DeviceInteract.PropertyControlSend(ctx, &dm.PropertyControlSendReq{
			ProductID:  stu.Device.ProductID,
			DeviceName: stu.Device.DeviceName,
			Data:       fmt.Sprintf(`{"load":%v}`, stu.Param),
		})
1
2
3
4
5
上次更新: 2024/11/12, 13:18:12
服务初始化
中间件使用

← 服务初始化 中间件使用→

Theme by Vdoing | Copyright © 2022-2025 昆明云物通科技有限公司|GNU | 滇ICP备2024043132号-1 |
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式