https://github.com/quan-nh/clj-grpc
https://github.com/quan-nh/clj-grpc
clojure grpc protobuf
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/quan-nh/clj-grpc
- Owner: quan-nh
- Created: 2022-03-25T06:25:12.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-05-14T14:45:24.000Z (about 4 years ago)
- Last Synced: 2025-02-22T21:26:19.507Z (over 1 year ago)
- Topics: clojure, grpc, protobuf
- Language: Java
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# clj-grpc
Clojure gRPC
## Guide
- Create Clojure app
```sh
lein new app clj-grpc
```
- Update `project.clj` to separate java & clojure source:
```clj
:source-paths ["src/clj"]
:java-source-paths ["src/java"]
```
- Add sample proto file [src/proto/helloworld.proto](src/proto/helloworld.proto)
- Generating the code using Protocol Buffer Compiler
- Download latest protoc from https://github.com/protocolbuffers/protobuf/releases
- Obtain the [gRPC Java Codegen Plugin](https://github.com/grpc/grpc-java/tree/master/compiler)
- `protoc --plugin=protoc-gen-grpc-java=$PATH_TO_PLUGIN -I=$SRC_DIR
--java_out=$DST_DIR --grpc-java_out=$DST_DIR $SRC_DIR/helloworld.proto`
- Using lein plugin
- https://github.com/bsima/lein-protoc
```clj
:plugins [[lein-protoc "0.4.0"]]
; lein-protoc config
:proto-version ~proto-version
:protoc-grpc {:version ~grpc-version}
:proto-source-paths ["src/proto"] ; default
:proto-target-path "src/java"
```
- `lein protoc`
- Add grpc deps
```clj
;; ref https://github.com/grpc/grpc-java
[io.grpc/grpc-netty-shaded ~grpc-version]
[io.grpc/grpc-protobuf ~grpc-version]
[io.grpc/grpc-stub ~grpc-version]
[org.apache.tomcat/annotations-api "6.0.53"] ; necessary for Java 9+
```
- Creating the server
- impl service
```clj
(defn greeter-service []
(proxy [GreeterGrpc$GreeterImplBase] []
(sayHello [^HelloRequest request response]
(let [name (.getName request)
builder (-> (HelloReply/newBuilder)
(.setMessage (str "Hello " name)))]
(.onNext response (.build builder))
(.onCompleted response)))))
```
- start server
```clj
(let [server (-> (ServerBuilder/forPort 9090)
(.addService (greeter-service))
(.build))]
(.start server)
(println "Server started, listening on 9090")
(.awaitTermination server))
```
- Creating the client
```clj
(let [channel (-> (ManagedChannelBuilder/forAddress "localhost" 9090)
(.usePlaintext)
(.build))
stub (GreeterGrpc/newBlockingStub channel)
req (-> (HelloRequest/newBuilder)
(.setName "test")
(.build))
resp (.sayHello stub req)]
(prn resp)
(.shutdown channel))
```
- AppsFlyer/pronto
+ https://github.com/AppsFlyer/pronto/
+ https://medium.com/appsflyerengineering/appsflyers-open-source-libraries-for-clojure-and-protocol-buffers-da9a87583c41