注意:本地要下载zookeeper
API接口
[Java] 纯文本查看 复制代码 public interface GreetingsService {
String sayHi(String name);
}
API实现
[Java] 纯文本查看 复制代码 import com.api.GreetingsService;
public class GreetingsServiceImpl implements GreetingsService {
@Override
public String sayHi(String name) {
return "hi, " + name;
}
}
注册dubbo服务
[Java] 纯文本查看 复制代码 import com.api.GreetingsService;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import java.util.concurrent.CountDownLatch;
public class Application {
private static String zookeeperHost = System.getProperty("zookeeper.address","127.0.0.1");
public static void main(String[] args) throws InterruptedException {
ServiceConfig<GreetingsService> service = new ServiceConfig<>();
service.setApplication(new ApplicationConfig("first-dubbo-provider"));
service.setRegistry(new RegistryConfig("zookeeper://"+zookeeperHost+":2181"));
service.setInterface(GreetingsService.class);
service.setRef(new GreetingsServiceImpl());
service.export();
System.out.println("dubbo service start");
new CountDownLatch(1).await();
}
}
客户端
[Java] 纯文本查看 复制代码 import com.api.GreetingsService;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
public class Application {
private static String zookeeperHost = System.getProperty("zookeeper.address","127.0.0.1");
public static void main(String[] args) {
ReferenceConfig<GreetingsService> reference = new ReferenceConfig<>();
reference.setApplication(new ApplicationConfig("first-dubbo-consumer"));
reference.setRegistry(new RegistryConfig("zookeeper://" + zookeeperHost + ":2181"));
reference.setInterface(GreetingsService.class);
GreetingsService service = reference.get();
String message = service.sayHi("dubbo-Hi");
System.out.println(message);
}
}
|