• Stars
    star
    30
  • Rank 811,030 (Top 17 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created about 5 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Owner: Core/Support WG

go-mod-messaging

Build Status Code Coverage Go Report Card GitHub Latest Dev Tag) GitHub Latest Stable Tag) GitHub License GitHub go.mod Go version GitHub Pull Requests GitHub Contributors GitHub Committers GitHub Commit Activity

Messaging client library for use by Go implementation of EdgeX micro services. This project contains the abstract Message Bus interface and an implementation for Redis Pub/Sub, MQTT and NATS. These interface functions connect, publish, subscribe and disconnect to/from the Message Bus. For more information see the MessageBus documentation.

What is this repository for?

  • Create new MessageClient
  • Connect to the Message Bus
  • Public messages to the Message Bus
  • Subscribe to and receives messages from the Messsage Bus
  • Disconnect from the Message Bus

Installation

  • Make sure you have modules enabled, i.e. have an initialized go.mod file
  • If your code is in your GOPATH then make sure GO111MODULE=on is set
  • Run go get github.com/edgexfoundry/go-mod-messaging/v3
    • This will add the go-mod-messaging to the go.mod file and download it into the module cache

How to Use

This library is used by Go programs for interacting with the Message Bus (i.e. redis).

The Message Bus connection information as well as which implementation to use is stored in the service's toml configuration as:

MessageBus:
  Protocol: redis
  Host: localhost
  Port: 6379
  Type: redis

Additional Configuration

Individual client abstractions allow additional configuration properties which can be provided via configuration file:

MessageBus:
  Protocol: tcp
  Host: localhost
  Port: 1883
  Type: mqtt
  Topic: events
  Optional:
    ClientId: MyClient
    Username: MyUsername
    ...

Or programmatically in the Optional field of the MessageBusConfig struct. For example,

types.MessageBusConfig{
				Broker: types.HostInfo{Host: "example.com", Port: 9090, Protocol: "tcp"},
				Optional: map[string]string{
					"ClientId":          "MyClientID",
					"Username":          "MyUser",
					"Password":          "MyPassword",
					...
				}}

NOTE
For complete details on configuration options see the MessageBus documentation

Usage

The following code snippets demonstrate how a service uses this messaging module to create a connection, send messages, and receive messages.

This code snippet shows how to connect to the abstract message bus.

var messageBus messaging.MessageClient

var err error
messageBus, err = msgFactory.NewMessageClient(types.MessageBusConfig{
    Broker:   types.HostInfo{
    Host:     Configuration.MessageBus.Host,
    Port:     Configuration.MessageBus.Port,
    Protocol: Configuration.MessageBus.Protocol,
  },
  Type: Configuration.MessageBus.Type,})

if err != nil {
  LoggingClient.Error("failed to create messaging client: " + err.Error())
}

err = messsageBus.Connect()

if err != nil {
  LoggingClient.Error("failed to connect to message bus: " + err.Error())
}

This code snippet shows how to publish a message to the abstract message bus.

...
payload, err := json.Marshal(evt)
...
msgEnvelope := types.MessageEnvelope{
  CorrelationID: evt.CorrelationId,
  Payload:       payload,
  ContentType:   clients.ContentJson,
}

err = messageBus.Publish(msgEnvelope, Configuration.MessageBus.Topic)

This code snippet shows how to subscribe to the abstract message bus.

messageBus, err := factory.NewMessageClient(types.MessageBusConfig{
    Broker:   types.HostInfo{
    Host:     Configuration.MessageBus.Host,
    Port:     Configuration.MessageBus.Port,
    Protocol: Configuration.MessageBus.Protocol,
  },
  Type: Configuration.MessageBus.Type,
})

if err != nil {
  LoggingClient.Error("failed to create messaging client: " + err.Error())
  return
}

if err := messageBus.Connect(); err != nil {
  LoggingClient.Error("failed to connect to message bus: " + err.Error())
  return
}

topics := []types.TopicChannel{
    {
      Topic:    Configuration.MessageBus.Topic,
      Messages: messages,
    },
}

err = messageBus.Subscribe(topics, messageErrors)
if err != nil {
  LoggingClient.Error("failed to subscribe for event messages: " + err.Error())
  return
}

This code snippet shows how to receive data on the message channel after you have subscribed to the bus.

...

for {
select {
  case e := <-errors:
  // handle errors
  ...
  
  case msgEnvelope := <-messages:
    LoggingClient.Info(fmt.Sprintf("Event received on message queue. Topic: %s, Correlation-id: %s ", Configuration.MessageBus.Topic, msgEnvelope.CorrelationID))
    if msgEnvelope.ContentType != clients.ContentJson {
      LoggingClient.Error(fmt.Sprintf("Incorrect content type for event message. Received: %s, Expected: %s", msgEnvelope.ContentType, clients.ContentJson))
      continue
    }
    str := string(msgEnvelope.Payload)
    event := parseEvent(str)
    if event == nil {
      continue
    }
}
...

More Repositories

1

edgex-go

EdgeX Golang Services Monorepo | Owner: Core/Support WG
Go
1,274
star
2

developer-scripts

Shell
93
star
3

edgex-ui-go

Owner: Core/Support WG
TypeScript
89
star
4

device-sdk-go

Owner: Device WG
Go
86
star
5

edgex-compose

EdgeX Foundry Docker Compose release compose files and tools for building EdgeX compose files
Makefile
73
star
6

device-modbus-go

Owner: Device WG
Go
72
star
7

edgex-examples

Go
50
star
8

edgex-docs

HTML
41
star
9

app-functions-sdk-go

Owner: Applications WG
Go
39
star
10

device-sdk-c

Owner: Device WG
C
39
star
11

device-mqtt-go

Owner: Device WG
Go
39
star
12

device-camera-go

Owner: Device WG
Go
32
star
13

device-onvif-camera

Owner: Device WG
Go
26
star
14

go-mod-core-contracts

Owner: Core/Support WG
Go
24
star
15

device-virtual-go

Owner: Device WG
Go
22
star
16

edgex-global-pipelines

Owner: DevOps WG
Groovy
21
star
17

support-rulesengine

Owner: Core/Support WG
Java
19
star
18

edgex-cli

Go
19
star
19

app-service-configurable

Owner: Applications WG
Makefile
16
star
20

device-rest-go

Owner: Device WG
Go
16
star
21

go-mod-bootstrap

go-mod-bootstrap
Go
13
star
22

device-mqtt

Java
13
star
23

core-data

Java
12
star
24

device-bacnet-c

Owner: Device WG
C
12
star
25

edgex-taf

RobotFramework
11
star
26

device-usb-camera

Owner: Device WG
Go
11
star
27

device-rfid-llrp-go

repository for new RFID device service being created/contributed by Intel | Owner: Device Service WG Chair, github ID: iain-anderson
Go
10
star
28

security-api-gateway

Owner: Security WG
Go
10
star
29

device-random

Owner: Device WG
Go
10
star
30

ci-management

Control repo for Jenkins job and build minion definitions | Owner: DevOps WG
Shell
9
star
31

app-rfid-llrp-inventory

Go
9
star
32

go-mod-secrets

Owner: Security WG
Go
8
star
33

device-gpio

device-gpio
Go
8
star
34

device-snmp-go

Owner: Device WG
Go
8
star
35

device-modbus

Java
8
star
36

go-mod-registry

Owner: Core/Support WG
Go
7
star
37

device-sdk

Java
7
star
38

device-bacnet

Java
6
star
39

git-semver

Owner: DevOps WG
Python
6
star
40

device-virtual

Java
6
star
41

sample-service

Owner: DevOps WG
Go
6
star
42

go-mod-configuration

Go module providing abstract configuration provider with Consul implementation | Owner: Core Working Group
Go
6
star
43

core-config-seed

Java
5
star
44

cd-management

Owner: DevOps WG
5
star
45

device-grove-c

Owner: Device WG
C
5
star
46

export-go

Go
5
star
47

jenkins_pipeline_presentation

4
star
48

device-bluetooth

Java
4
star
49

device-sdk-tools

Java
4
star
50

blackbox-testing

Integration test suite | Owner: QA-Testing WG
Shell
4
star
51

core-clients-go

Go
4
star
52

docker-edgex-volume

Dockerfile
3
star
53

core-data-go

Go
3
star
54

core-metadata-go

Go
3
star
55

security-secret-store

Owner: Security WG
Go
3
star
56

docker-edgex-mongo

Owner: Core/Support WG
Go
3
star
57

core-command-go

Go
3
star
58

edgex-taf-common

Owner: QA-Testing WG
Python
3
star
59

core-command

Java
3
star
60

core-config-seed-go

Go
3
star
61

support-scheduler

Java
3
star
62

device-coap-c

EdgeX device service for CoAP-based REST protocol
C
3
star
63

core-domain

Java
3
star
64

core-domain-go

Go
3
star
65

export-distro

Java
2
star
66

edgex-dev-badge

Python
2
star
67

demo-grove-pi

2
star
68

device-uart

device-uart
Go
2
star
69

device-snmp

Java
1
star
70

docker-edgex-consul

Owner: Core/Support WG
Go
1
star
71

core-metadata

Java
1
star
72

core-command-client

Java
1
star
73

community

1
star
74

core-config-watcher

Java
1
star
75

support-notifications

Java
1
star
76

support-logging-client-go

Go
1
star
77

docker-export-distro

1
star
78

edgex-ui-clojure

Owner: Core/Support WG
Clojure
1
star
79

edgex-helm

Owner: Core WG
Mustache
1
star
80

consul-client-go

Go
1
star
81

edgex-taf-pipelines

to implement Jenkins pipeline for edgex-taf | Owner: QA WG
1
star