Skip to content

Commit 95fdd1f

Browse files
authored
Add token examples of python (#25)
* Add oauth2 examples of python Signed-off-by: xiaolong.ran <rxl@apache.org> * Add token examples of nodejs Signed-off-by: xiaolong.ran <rxl@apache.org> * ignore package-lock.json file Signed-off-by: xiaolong.ran <rxl@apache.org> * Fix a little Signed-off-by: xiaolong.ran <rxl@apache.org> * fix comments Signed-off-by: xiaolong.ran <rxl@apache.org> * Add pulsar-client and pulsar-perf Signed-off-by: xiaolong.ran <rxl@apache.org> * update readme Signed-off-by: xiaolong.ran <rxl@apache.org> * Improve pulsarctl example Signed-off-by: xiaolong.ran <rxl@apache.org> * fix comments Signed-off-by: xiaolong.ran <rxl@apache.org> * fix pulsarctl code example Signed-off-by: xiaolong.ran <rxl@apache.org> * fix comments Signed-off-by: xiaolong.ran <rxl@apache.org>
1 parent 57634fa commit 95fdd1f

File tree

25 files changed

+972
-4
lines changed

25 files changed

+972
-4
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
*.tar.gz
2020
*.rar
2121

22+
node_modules
23+
package-lock.json
24+
2225
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2326
hs_err_pid*
2427

cloud/README.md

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,87 @@
22

33
## Programming Languages
44

5-
This directory includes examples of Apache Pulsar client applications, showcasing producers and consumers, written in various programming languages. The README for each language walks through the necessary steps to run each example. When each client establishes a connection with the Pulsar cluster through OAuth2, it needs to obtain the specified options from the Pulsar cluster and OAuth2 services. [How to get OAuth2 options](#How to get OAuth2 options) explains how you get these options.
5+
This directory includes examples of Apache Pulsar client applications, showcasing producers and consumers, written in various programming languages. The README for each language walks through the necessary steps to run each example. When each client establishes a connection with the Pulsar cluster through **OAuth2** or **Token**.
6+
7+
For the OAuth2, it needs to obtain the specified options from the Pulsar cluster and OAuth2 services. [How to get OAuth2 options](#How to get OAuth2 options) explains how you get these options.
68

79
Currently, we support the following three languages to connect through OAuth2:
810

911
- Java
1012
- Go
1113
- CPP
1214

13-
For clients in other languages, you can connect through token, reference to [here](https://pulsar.apache.org/docs/en/security-tls-transport/#client-configuration).
15+
The following clients and Pulsar CLI tools are supported to connect to cluster through the Token. And [How to get Token options](#How to get Token options) explains how you get these options.
16+
17+
- Java
18+
- Go
19+
- CPP
20+
- Python
21+
- CSharp(TODO)
22+
- NodeJS
23+
- pulsarctl
24+
- pulsar-admin
25+
- pulsar-client
26+
- pulsar-perf
27+
28+
## How to get token options
29+
30+
When you use Token to connect to Pulsar cluster, you need to provide the following options:
31+
32+
- `SERVICE_URL`
33+
- `WEB_SERVICE_URL`
34+
- `AUTH_PARAMS`
35+
36+
For the `SERVICE_URL` field, you can get the **hostname** through the following command:
37+
38+
```shell script
39+
$ snctl get pulsarclusters [CLUSTER_NAME] -n [NAMESPACE] -o json | jq '.spec.serviceEndpoints[0].dnsName'
40+
```
41+
42+
Output:
43+
44+
```text
45+
api.test.cloud.xxx.streamnative.dev
46+
```
47+
48+
A `SERVICE_URL` is a combination of protocol, hostname and port, so an example of a complete `SERVICE_URL` is as follows:
49+
50+
51+
```text
52+
pulsar://api.test.cloud.xxx.streamnative.dev:6650
53+
54+
# For tls
55+
pulsar+ssl://api.test.cloud.xxx.streamnative.dev:6651
56+
```
57+
58+
For the `WEB_SERVICE_URL` field, you can get the **hostname** through the following command:
59+
60+
```shell script
61+
$ snctl get pulsarclusters [CLUSTER_NAME] -n [NAMESPACE] -o json | jq '.spec.serviceEndpoints[0].dnsName'
62+
```
63+
64+
Output:
65+
66+
```text
67+
api.test.cloud.xxx.streamnative.dev
68+
```
69+
70+
A `WEB_SERVICE_URL` is a combination of protocol, hostname and port, so an example of a complete `WEB_SERVICE_URL` is as follows:
71+
72+
```text
73+
http://api.test.cloud.xxx.streamnative.dev:8080
74+
75+
# For tls
76+
https://api.test.cloud.xxx.streamnative.dev:8443
77+
```
78+
79+
For the `AUTH_PARAMS` field, you can get it through the following command:
80+
81+
```shell script
82+
$ snctl auth get-token [INSTANCE] [flags]
83+
```
84+
85+
> Tips: In code implementation, for safety and convenience, you can consider setting `AUTH_PARAMS` as an environment variable.
1486
1587
## How to get OAuth2 options
1688

cloud/cpp/ConnectByOAuth2.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
/**
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
#include <iostream>
21+
#include <pulsar/Client.h>
22+
23+
using namespace pulsar;
24+
25+
int main() {
26+
ClientConfiguration config;
27+
std::string params = R"({
28+
"issuer_url": "https://dev-kt-aa9ne.us.auth0.com/oauth/token",
29+
"private_key": "../../pulsar-broker/src/test/resources/authentication/token/cpp_credentials_file.json",
30+
"audience": "https://dev-kt-aa9ne.us.auth0.com/api/v2/"})";
31+
32+
config.setAuth(pulsar::AuthOauth2::create(params));
33+
34+
Client client("pulsar+ssl://cluster.test.us-east4.streamnative.test.g.sn2.dev:6651", config);
35+
client.close();
36+
}

cloud/cpp/ConnectByToken.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
/**
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
#include <iostream>
21+
#include <pulsar/Client.h>
22+
23+
using namespace pulsar;
24+
25+
int main() {
26+
ClientConfiguration config;
27+
config.setAuth(AuthToken::createWithToken("AUTH_PARAMS"));
28+
Client client(SERVICE_URL, config);
29+
30+
client.close();
31+
}

cloud/cpp/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ Produce message to and consume message from a Pulsar cluster using [Apache pulsa
44

55
# Prerequisites
66

7+
## Linux
8+
9+
Since 2.1.0 release, Pulsar ships pre-built RPM and Debian packages. You can download and install those packages directly.
10+
11+
For more information, refer to [here](https://pulsar.apache.org/docs/en/client-libraries-cpp/#supported-platforms).
12+
13+
## MacOS
14+
715
Pulsar releases are available in the Homebrew core repository. You can install the C++ client library with the following command. The package is installed with the library and headers.
816

917
```shell script
@@ -16,6 +24,8 @@ In this example, the producer will publish data to the `topic-1` in your Pulsar
1624
The content of each message payload is `content`.
1725
The consumer will receive the message from the `topic-1` and `ack` the receipt of each message received.
1826

27+
> Tips: The following code example uses the OAuth2 connection method. If you want to connect to the Pulsar cluster using Token, please refer to the implementation of **connectByToken.cc**.
28+
1929
1. Run the consumer, and start to receiving the message from `topic-1`:
2030

2131
```bash

cloud/csharp/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Overview
2+
3+
This document describes how to produce message to and consume message from a Pulsar cluster using [Apache pulsar-dotpulsar](https://github.com/apache/pulsar-dotpulsar).

cloud/go/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ In this example, the producer publishes data to the `topic-1` in your Pulsar clu
1717
The content of each message payload is a combination of `hello-` and a digital (0-9) (e.g: `hello-0`).
1818
The consumer receives the message from the `topic-1` and `acknowledges` each received message.
1919

20+
> Tips: The following code example uses the OAuth2 connection method. If you want to connect to the Pulsar cluster using Token, please refer to the implementation of **connectByToken.go**.
21+
2022
1. Run the consumer, and start to receiving the message from `topic-1`:
2123

2224
```bash

cloud/go/connectByToken.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package main
19+
20+
import (
21+
"github.com/apache/pulsar-client-go/pulsar"
22+
"log"
23+
)
24+
25+
func main() {
26+
client, err := pulsar.NewClient(pulsar.ClientOptions{
27+
URL: SERVICE_URL,
28+
Authentication: pulsar.NewAuthenticationToken(AUTH_PARAMS),
29+
})
30+
if err != nil {
31+
log.Fatal(err)
32+
}
33+
defer client.Close()
34+
}

cloud/java/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ This document describes how to produce messages to and consume messages from a A
77
- Java: 1.8+
88
- Pulsar broker: 2.7.0-742fc5c9b+
99

10-
> You can get this tarball from [bintray](https://bintray.com/streamnative/maven/org.apache.pulsar/2.7.0-742fc5c9b). When the 2.6.1 release is complete, you can also use the official 2.6.1 version.
10+
> You can get this tarball from [bintray](https://bintray.com/streamnative/maven/org.apache.pulsar/2.7.0-742fc5c9b). When Pulsar 2.6.1 is released, you can also use the official 2.6.1 version.
1111
1212
# Example
1313

1414
In this example, the producer publishes messages to the `topic-1` in your Pulsar cluster.
1515
The content of each message payload is a combination of `my-message-` and a digital (0-9) (e.g: `my-message-0`).
1616
The consumer receives the message from the `topic-1` and `acknowledges` each received message.
1717

18+
> Tips: The following code example uses the OAuth2 connection method. If you want to connect to the Pulsar cluster using Token, please refer to the implementation of **connectByToken.java**.
19+
1820
1. Run the consumer.
1921

2022
```shell script

cloud/java/src/main/java/io/streamnative/examples/oauth2/ConnectByOauth2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.apache.pulsar.client.impl.auth.oauth2.AuthenticationFactoryOAuth2;
2323

2424
public class ConnectByOauth2 {
25-
public static void main(String[] args) throws Exception{
25+
public static void main(String[] args) throws Exception {
2626
String issuerUrl = "https://dev-kt-aa9ne.us.auth0.com/oauth/token";
2727
String credentialsUrl = "file:///path/to/KeyFile.json";
2828
String audience = "https://dev-kt-aa9ne.us.auth0.com/api/v2/";

0 commit comments

Comments
 (0)