This is an automated email from the ASF dual-hosted git repository.
fuyou001 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-clients.git
The following commit(s) were added to refs/heads/master by this push:
new fc2b54d7 fix(golang): read example endpoint and credentials from
environment variables (#1488)
fc2b54d7 is described below
commit fc2b54d7e2762f948a8896b7936e5287e0b9d101
Author: fuyou001 <[email protected]>
AuthorDate: Tue Nov 10 15:25:27 2026 +0800
fix(golang): read example endpoint and credentials from environment
variables (#1488)
* fix(golang): read example credentials from environment variables
* fix(golang): read example endpoints from environment variables
---
golang/README.md | 23 ++++++++++++++++++++++
golang/example/consumer/lite_push_consumer/main.go | 11 +++++------
.../example/consumer/lite_simple_consumer/main.go | 11 +++++------
golang/example/consumer/push_consumer/main.go | 11 +++++------
golang/example/consumer/simple_consumer/main.go | 11 +++++------
golang/example/producer/async/main.go | 15 ++++++--------
golang/example/producer/delay/main.go | 15 ++++++--------
golang/example/producer/delay_recall/main.go | 17 +++++++---------
golang/example/producer/fifo/main.go | 15 ++++++--------
golang/example/producer/lite/main.go | 15 ++++++--------
golang/example/producer/normal/main.go | 15 ++++++--------
golang/example/producer/priority/main.go | 17 +++++++---------
golang/example/producer/transaction/main.go | 15 ++++++--------
13 files changed, 93 insertions(+), 98 deletions(-)
diff --git a/golang/README.md b/golang/README.md
index 41df54a6..cd4c9739 100644
--- a/golang/README.md
+++ b/golang/README.md
@@ -24,6 +24,29 @@ Otherwise, to install the `golang` package, run the
following command:
go get -u github.com/apache/rocketmq-clients/golang/v5
```
+### Running Examples
+
+The producer and consumer examples read their endpoint and authentication
credentials from environment variables:
+
+| Variable | Description |
+| --- | --- |
+| `ROCKETMQ_ENDPOINT` | RocketMQ endpoint in `host:port` format (required). |
+| `ROCKETMQ_ACCESS_KEY` | Access key for broker authentication. |
+| `ROCKETMQ_SECRET_KEY` | Secret key for broker authentication. |
+
+Set `ROCKETMQ_ENDPOINT` before running an example. There is no default
endpoint; an unset or empty value causes client creation to fail.
+
+When broker authentication is enabled, also set both `ROCKETMQ_ACCESS_KEY` and
`ROCKETMQ_SECRET_KEY` in your runtime environment. Keep real credentials out of
source files and version control.
+
+Unset credential variables are read as empty strings. For brokers that do not
require authentication, these variables can remain unset.
+
+Configure the topic and consumer group in the example as needed, then run it
from the `golang` directory. For example, to connect to a local endpoint:
+
+```sh
+export ROCKETMQ_ENDPOINT='localhost:8081'
+go run ./example/producer/normal
+```
+
## Development
### Protocol Buffers Generation
diff --git a/golang/example/consumer/lite_push_consumer/main.go
b/golang/example/consumer/lite_push_consumer/main.go
index 29741c58..6f4a25c3 100644
--- a/golang/example/consumer/lite_push_consumer/main.go
+++ b/golang/example/consumer/lite_push_consumer/main.go
@@ -30,9 +30,6 @@ import (
const (
ConsumerGroup = "victory_lite_group001"
Topic = "victory_lite_topic001"
- Endpoint = "11.167.131.190:8081"
- AccessKey = "xxxxxx"
- SecretKey = "xxxxxx"
NameSpace = ""
)
@@ -42,17 +39,19 @@ var (
)
func main() {
+ endpoint := os.Getenv("ROCKETMQ_ENDPOINT")
+
// log to console
os.Setenv("mq.consoleAppender.enabled", "true")
rmq_client.ResetLogger()
rmq_client.EnableSsl = false
// In most case, you don't need to create many consumers, singleton
pattern is more recommended.
pushConsumer, err := rmq_client.NewLitePushConsumer(&rmq_client.Config{
- Endpoint: Endpoint,
+ Endpoint: endpoint,
ConsumerGroup: ConsumerGroup,
Credentials: &credentials.SessionCredentials{
- AccessKey: AccessKey,
- AccessSecret: SecretKey,
+ AccessKey: os.Getenv("ROCKETMQ_ACCESS_KEY"),
+ AccessSecret: os.Getenv("ROCKETMQ_SECRET_KEY"),
},
NameSpace: NameSpace,
},
diff --git a/golang/example/consumer/lite_simple_consumer/main.go
b/golang/example/consumer/lite_simple_consumer/main.go
index e2a5044a..c71528f0 100644
--- a/golang/example/consumer/lite_simple_consumer/main.go
+++ b/golang/example/consumer/lite_simple_consumer/main.go
@@ -32,9 +32,6 @@ const (
ConsumerGroup = "xxxxxx"
BindTopic = "xxxxxx"
LiteTopic = "xxxxxx"
- Endpoint = "xxxxxx"
- AccessKey = "xxxxxx"
- SecretKey = "xxxxxx"
NameSpace = ""
)
@@ -48,16 +45,18 @@ var (
)
func main() {
+ endpoint := os.Getenv("ROCKETMQ_ENDPOINT")
+
// log to console
os.Setenv("mq.consoleAppender.enabled", "true")
rmq_client.ResetLogger()
// In most case, you don't need to create many consumers, singleton
pattern is more recommended.
liteSimpleConsumer, err :=
rmq_client.NewLiteSimpleConsumer(&rmq_client.Config{
- Endpoint: Endpoint,
+ Endpoint: endpoint,
ConsumerGroup: ConsumerGroup,
Credentials: &credentials.SessionCredentials{
- AccessKey: AccessKey,
- AccessSecret: SecretKey,
+ AccessKey: os.Getenv("ROCKETMQ_ACCESS_KEY"),
+ AccessSecret: os.Getenv("ROCKETMQ_SECRET_KEY"),
},
NameSpace: NameSpace,
},
diff --git a/golang/example/consumer/push_consumer/main.go
b/golang/example/consumer/push_consumer/main.go
index 78206455..bfcb3674 100644
--- a/golang/example/consumer/push_consumer/main.go
+++ b/golang/example/consumer/push_consumer/main.go
@@ -30,9 +30,6 @@ import (
const (
ConsumerGroup = "victory_lite_group001"
Topic = "victory_lite_topic001"
- Endpoint = "11.167.131.190:8081"
- AccessKey = "xxxxxx"
- SecretKey = "xxxxxx"
NameSpace = "xxxxxx"
)
@@ -42,16 +39,18 @@ var (
)
func main() {
+ endpoint := os.Getenv("ROCKETMQ_ENDPOINT")
+
// log to console
os.Setenv("mq.consoleAppender.enabled", "true")
rmq_client.ResetLogger()
// In most case, you don't need to create many consumers, singleton
pattern is more recommended.
pushConsumer, err := rmq_client.NewPushConsumer(&rmq_client.Config{
- Endpoint: Endpoint,
+ Endpoint: endpoint,
ConsumerGroup: ConsumerGroup,
Credentials: &credentials.SessionCredentials{
- AccessKey: AccessKey,
- AccessSecret: SecretKey,
+ AccessKey: os.Getenv("ROCKETMQ_ACCESS_KEY"),
+ AccessSecret: os.Getenv("ROCKETMQ_SECRET_KEY"),
},
NameSpace: NameSpace,
},
diff --git a/golang/example/consumer/simple_consumer/main.go
b/golang/example/consumer/simple_consumer/main.go
index 05da8ff8..1264d4c7 100644
--- a/golang/example/consumer/simple_consumer/main.go
+++ b/golang/example/consumer/simple_consumer/main.go
@@ -31,9 +31,6 @@ import (
const (
Topic = "xxxxxx"
ConsumerGroup = "xxxxxx"
- Endpoint = "xxxxxx"
- AccessKey = "xxxxxx"
- SecretKey = "xxxxxx"
)
var (
@@ -47,16 +44,18 @@ var (
)
func main() {
+ endpoint := os.Getenv("ROCKETMQ_ENDPOINT")
+
// log to console
os.Setenv("mq.consoleAppender.enabled", "true")
rmq_client.ResetLogger()
// In most case, you don't need to create many consumers, singleton
pattern is more recommended.
simpleConsumer, err := rmq_client.NewSimpleConsumer(&rmq_client.Config{
- Endpoint: Endpoint,
+ Endpoint: endpoint,
ConsumerGroup: ConsumerGroup,
Credentials: &credentials.SessionCredentials{
- AccessKey: AccessKey,
- AccessSecret: SecretKey,
+ AccessKey: os.Getenv("ROCKETMQ_ACCESS_KEY"),
+ AccessSecret: os.Getenv("ROCKETMQ_SECRET_KEY"),
},
},
rmq_client.WithSimpleAwaitDuration(awaitDuration),
diff --git a/golang/example/producer/async/main.go
b/golang/example/producer/async/main.go
index fd48eaaf..da58e6c0 100644
--- a/golang/example/producer/async/main.go
+++ b/golang/example/producer/async/main.go
@@ -29,23 +29,20 @@ import (
"github.com/apache/rocketmq-clients/golang/v5/credentials"
)
-const (
- Topic = "xxxxxx"
- Endpoint = "xxxxxx"
- AccessKey = "xxxxxx"
- SecretKey = "xxxxxx"
-)
+const Topic = "xxxxxx"
func main() {
+ endpoint := os.Getenv("ROCKETMQ_ENDPOINT")
+
// log to console
os.Setenv("mq.consoleAppender.enabled", "true")
rmq_client.ResetLogger()
// In most case, you don't need to create many producers, singleton
pattern is more recommended.
producer, err := rmq_client.NewProducer(&rmq_client.Config{
- Endpoint: Endpoint,
+ Endpoint: endpoint,
Credentials: &credentials.SessionCredentials{
- AccessKey: AccessKey,
- AccessSecret: SecretKey,
+ AccessKey: os.Getenv("ROCKETMQ_ACCESS_KEY"),
+ AccessSecret: os.Getenv("ROCKETMQ_SECRET_KEY"),
},
},
rmq_client.WithTopics(Topic),
diff --git a/golang/example/producer/delay/main.go
b/golang/example/producer/delay/main.go
index cdea43d9..d4efa94a 100644
--- a/golang/example/producer/delay/main.go
+++ b/golang/example/producer/delay/main.go
@@ -29,23 +29,20 @@ import (
"github.com/apache/rocketmq-clients/golang/v5/credentials"
)
-const (
- Topic = "xxxxxx"
- Endpoint = "xxxxxx"
- AccessKey = "xxxxxx"
- SecretKey = "xxxxxx"
-)
+const Topic = "xxxxxx"
func main() {
+ endpoint := os.Getenv("ROCKETMQ_ENDPOINT")
+
// log to console
os.Setenv("mq.consoleAppender.enabled", "true")
rmq_client.ResetLogger()
// In most case, you don't need to create many producers, singleton
pattern is more recommended.
producer, err := rmq_client.NewProducer(&rmq_client.Config{
- Endpoint: Endpoint,
+ Endpoint: endpoint,
Credentials: &credentials.SessionCredentials{
- AccessKey: AccessKey,
- AccessSecret: SecretKey,
+ AccessKey: os.Getenv("ROCKETMQ_ACCESS_KEY"),
+ AccessSecret: os.Getenv("ROCKETMQ_SECRET_KEY"),
},
},
rmq_client.WithTopics(Topic),
diff --git a/golang/example/producer/delay_recall/main.go
b/golang/example/producer/delay_recall/main.go
index 8eafc44a..6b0c756f 100644
--- a/golang/example/producer/delay_recall/main.go
+++ b/golang/example/producer/delay_recall/main.go
@@ -28,23 +28,20 @@ import (
"github.com/apache/rocketmq-clients/golang/v5/credentials"
)
-const (
- Topic = "topic-delay-new"
- Endpoint = "127.0.0.1:8080"
- AccessKey = ""
- SecretKey = ""
-)
+const Topic = "topic-delay-new"
func main() {
+ endpoint := os.Getenv("ROCKETMQ_ENDPOINT")
+
os.Setenv("mq.consoleAppender.enabled", "true")
rmq_client.ResetLogger()
// In most case, you don't need to create many producers, singleton
pattern is more recommended.
producer, err := rmq_client.NewProducer(&rmq_client.Config{
- Endpoint: Endpoint,
+ Endpoint: endpoint,
Credentials: &credentials.SessionCredentials{
- AccessKey: AccessKey,
- AccessSecret: SecretKey,
+ AccessKey: os.Getenv("ROCKETMQ_ACCESS_KEY"),
+ AccessSecret: os.Getenv("ROCKETMQ_SECRET_KEY"),
},
},
rmq_client.WithTopics(Topic),
@@ -63,7 +60,7 @@ func main() {
fmt.Println("========== Delay Message Recall Example ==========")
fmt.Printf("Topic: %s\n", Topic)
- fmt.Printf("Endpoint: %s\n\n", Endpoint)
+ fmt.Printf("Endpoint: %s\n\n", endpoint)
// Send delay messages with recall capability
fmt.Println("Sending delay messages that can be recalled...")
diff --git a/golang/example/producer/fifo/main.go
b/golang/example/producer/fifo/main.go
index a92ed591..eb1a76a4 100644
--- a/golang/example/producer/fifo/main.go
+++ b/golang/example/producer/fifo/main.go
@@ -29,23 +29,20 @@ import (
"github.com/apache/rocketmq-clients/golang/v5/credentials"
)
-const (
- Topic = "xxxxxx"
- Endpoint = "xxxxxx"
- AccessKey = "xxxxxx"
- SecretKey = "xxxxxx"
-)
+const Topic = "xxxxxx"
func main() {
+ endpoint := os.Getenv("ROCKETMQ_ENDPOINT")
+
// log to console
os.Setenv("mq.consoleAppender.enabled", "true")
rmq_client.ResetLogger()
// In most case, you don't need to create many producers, singleton
pattern is more recommended.
producer, err := rmq_client.NewProducer(&rmq_client.Config{
- Endpoint: Endpoint,
+ Endpoint: endpoint,
Credentials: &credentials.SessionCredentials{
- AccessKey: AccessKey,
- AccessSecret: SecretKey,
+ AccessKey: os.Getenv("ROCKETMQ_ACCESS_KEY"),
+ AccessSecret: os.Getenv("ROCKETMQ_SECRET_KEY"),
},
},
rmq_client.WithTopics(Topic),
diff --git a/golang/example/producer/lite/main.go
b/golang/example/producer/lite/main.go
index 72f55f2b..930d4f87 100644
--- a/golang/example/producer/lite/main.go
+++ b/golang/example/producer/lite/main.go
@@ -29,23 +29,20 @@ import (
rmq_client "github.com/apache/rocketmq-clients/golang/v5"
)
-const (
- Topic = "victory_lite_topic001"
- Endpoint = "11.167.131.190:8081"
- AccessKey = "xxxxxx"
- SecretKey = "xxxxxx"
-)
+const Topic = "victory_lite_topic001"
func main() {
+ endpoint := os.Getenv("ROCKETMQ_ENDPOINT")
+
os.Setenv("mq.consoleAppender.enabled", "true")
rmq_client.ResetLogger()
//rmq_client.EnableSsl = false
// In most case, you don't need to create many producers, singleton
pattern is more recommended.
producer, err := rmq_client.NewProducer(&rmq_client.Config{
- Endpoint: Endpoint,
+ Endpoint: endpoint,
Credentials: &credentials.SessionCredentials{
- AccessKey: AccessKey,
- AccessSecret: SecretKey,
+ AccessKey: os.Getenv("ROCKETMQ_ACCESS_KEY"),
+ AccessSecret: os.Getenv("ROCKETMQ_SECRET_KEY"),
},
},
rmq_client.WithTopics(Topic),
diff --git a/golang/example/producer/normal/main.go
b/golang/example/producer/normal/main.go
index 4fe7ac2e..a20c2fd5 100644
--- a/golang/example/producer/normal/main.go
+++ b/golang/example/producer/normal/main.go
@@ -29,22 +29,19 @@ import (
"github.com/apache/rocketmq-clients/golang/v5/credentials"
)
-const (
- Topic = "xxxxxx"
- Endpoint = "xxxxxx"
- AccessKey = "xxxxxx"
- SecretKey = "xxxxxx"
-)
+const Topic = "xxxxxx"
func main() {
+ endpoint := os.Getenv("ROCKETMQ_ENDPOINT")
+
os.Setenv("mq.consoleAppender.enabled", "true")
rmq_client.ResetLogger()
// In most case, you don't need to create many producers, singleton
pattern is more recommended.
producer, err := rmq_client.NewProducer(&rmq_client.Config{
- Endpoint: Endpoint,
+ Endpoint: endpoint,
Credentials: &credentials.SessionCredentials{
- AccessKey: AccessKey,
- AccessSecret: SecretKey,
+ AccessKey: os.Getenv("ROCKETMQ_ACCESS_KEY"),
+ AccessSecret: os.Getenv("ROCKETMQ_SECRET_KEY"),
},
},
rmq_client.WithTopics(Topic),
diff --git a/golang/example/producer/priority/main.go
b/golang/example/producer/priority/main.go
index 9e7af326..839fff86 100644
--- a/golang/example/producer/priority/main.go
+++ b/golang/example/producer/priority/main.go
@@ -28,23 +28,20 @@ import (
"github.com/apache/rocketmq-clients/golang/v5/credentials"
)
-const (
- Topic = "topic-priority"
- Endpoint = "127.0.0.1:8080"
- AccessKey = ""
- SecretKey = ""
-)
+const Topic = "topic-priority"
func main() {
+ endpoint := os.Getenv("ROCKETMQ_ENDPOINT")
+
os.Setenv("mq.consoleAppender.enabled", "true")
rmq_client.ResetLogger()
// In most case, you don't need to create many producers, singleton
pattern is more recommended.
producer, err := rmq_client.NewProducer(&rmq_client.Config{
- Endpoint: Endpoint,
+ Endpoint: endpoint,
Credentials: &credentials.SessionCredentials{
- AccessKey: AccessKey,
- AccessSecret: SecretKey,
+ AccessKey: os.Getenv("ROCKETMQ_ACCESS_KEY"),
+ AccessSecret: os.Getenv("ROCKETMQ_SECRET_KEY"),
},
},
rmq_client.WithTopics(Topic),
@@ -63,7 +60,7 @@ func main() {
fmt.Println("========== Priority Message Producer Example ==========")
fmt.Printf("Topic: %s\n", Topic)
- fmt.Printf("Endpoint: %s\n\n", Endpoint)
+ fmt.Printf("Endpoint: %s\n\n", endpoint)
// Send priority messages with different levels
fmt.Println("Sending priority messages...")
diff --git a/golang/example/producer/transaction/main.go
b/golang/example/producer/transaction/main.go
index ef1b3f85..1e06a54f 100644
--- a/golang/example/producer/transaction/main.go
+++ b/golang/example/producer/transaction/main.go
@@ -29,23 +29,20 @@ import (
"github.com/apache/rocketmq-clients/golang/v5/credentials"
)
-const (
- Topic = "xxxxxx"
- Endpoint = "xxxxxx"
- AccessKey = "xxxxxx"
- SecretKey = "xxxxxx"
-)
+const Topic = "xxxxxx"
func main() {
+ endpoint := os.Getenv("ROCKETMQ_ENDPOINT")
+
// log to console
os.Setenv("mq.consoleAppender.enabled", "true")
rmq_client.ResetLogger()
// In most case, you don't need to create many producers, singleton
pattern is more recommended.
producer, err := rmq_client.NewProducer(&rmq_client.Config{
- Endpoint: Endpoint,
+ Endpoint: endpoint,
Credentials: &credentials.SessionCredentials{
- AccessKey: AccessKey,
- AccessSecret: SecretKey,
+ AccessKey: os.Getenv("ROCKETMQ_ACCESS_KEY"),
+ AccessSecret: os.Getenv("ROCKETMQ_SECRET_KEY"),
},
},
rmq_client.WithTransactionChecker(&rmq_client.TransactionChecker{