Skip to content

xumingming/clj-dubbo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

clj-dubbo

call Dubbo service in clojure.

Dubbo is a distributed service framework created by Alibaba. This library makes it possible to call existing java service exposed by Dubbo using clojure AND in clojure way. With this library, you can call a remote service method just like call a normal clojure function , and your code doesn't need to depend on any java library.

Usage

Suppose we have the following service:

package com.alibaba.dubbo.demo;

public interface DemoService {
    String sayHello(String name);
    
    int add(int a, int b);
    
    Person findPerson(String name);
    
    String   savePerson(Person p);
}

And the implementation is:

package com.alibaba.dubbo.demo.provider;

import com.alibaba.dubbo.demo.DemoService;
import com.alibaba.dubbo.demo.Person;
 
public class DemoServiceImpl implements DemoService {
    public String sayHello(String name) {
        return "Hello " + name;
    }

    @Override
    public int add(int a, int b) {
        return a + b;
    }
 
    public Person findPerson(String name) {
        Person ret = new Person();
        ret.setAge(20);
        ret.setName(name);
        
        return ret;
    }

    @Override
    public String savePerson(Person p) {
        return "From server: name:" + p.getName() + ", age: " + p.getAge();
    }
}
  • First, we need to tell clojure code where the registry is.
(set-registry! "127.0.0.1:9090")
  • Then we define a remote service method:
(def-service-method "com.alibaba.dubbo.demo.DemoService" "1.0.0" "sayHello" ["java.lang.String"] ["name"])

The params are: service name, service version, method name, param types, param names. This will define a function named sayHello which accepts a single param named name , when you call this function, it will delegate the call to the remote service method: com.alibaba.dubbo.demo.DemoService#sayHello

  • You can also define multiple methods in one form:
(def-service "com.alibaba.dubbo.demo.DemoService" "1.0.0" [
    ["sayHello" ["java.lang.String"] ["name"]]
    ["add" ["int" "int"] ["a" "b"]]
    ["findPerson" ["java.lang.String"] ["name"]]
    ["savePerson" ["com.alibaba.dubbo.demo.Person"] ["p"]]
  ])
  • After define the remote service method, we can call the function just like a normal function
(sayHello "james")
;; "Hello james"
  • Also supports service method which has multiple params:
;; Just treat it as a function with mutiple params.
(add 1 2)
;; 3
  • Also supports service method whose param is a POJO.
;; For service method whose param is a POJO, you just need to treat the param 
;; as a map in clojure.
(savePerson {:name "james" :age (int 12)})
;; "From server: name:james, age: 12"
  • Also supports service method whose return value is a POJO.
;; the POJO return value will be converted to a clojure map, and the property name 
;; whill be converted to keyword type.
(findPerson "james")
;; {:name "james", :age 20, :class "com.alibaba.dubbo.demo.Person"}
  • Destory the service.
;; close one service
(close-service "com.alibaba.dubbo.demo.DemoService")

;; close all services
(close-all-services )

License

Copyright (C) 2012 xumingming

Distributed under the Eclipse Public License.

About

Call dubbo service in clojure

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published