We're introducing a communication layer between our microservices based on JSON-RPC (just because it's cool and fast). Here's an initial implementation of a Groovy-based framework for doing JSON-RPC calls:
class JsonRpcClient extends HTTPBuilder {
JsonRpcClient(String uri) {
super(uri)
}
def methodMissing(String name, args) {
def result
request(POST, JSON) { req ->
body = [
"jsonrpc" : "2.0",
"method" : name,
"params" : args, "id" : 1 ]
response.success = { resp, json -> result = json }
}
return result
}
}
I means sure it's not complete but using it is very much possible!
def http = new JsonRpcClient('http://localhost:8080/example/api/hello')
println http.sayHello("John")
I mean how cool is that, ha? Some meta programming with an existing framework (HTTPBuilder) and you get a micro implementation of JSON-RPC protocol in a few lines of code!
Happy coding! Groovy Rulez!
No comments:
Post a Comment