快速入门
<dependencies>
<dependency>
<groupId>com.github.applesline</groupId>
<artifactId>mini-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
package org.applesline.api;
public interface GreetingService {
String sayHello(String name);
}
package org.applesline;
import org.applesline.api.GreetingService;
public class GreetingServiceImpl implements GreetingService {
public String sayHello(String name) {
return "hi " + name;
}
}
package org.applesline;
import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
import org.applesline.api.GreetingService;
public class ServerConfig extends AbstractModule {
public void configure() {
bind(GreetingService.class).to(GreetingServiceImpl.class).in(Singleton.class);
}
}
package org.applesline;
import org.applesline.mini.dubbo.context.RpcContext;
import org.applesline.mini.dubbo.protocol.Protocol;
public class ServiceProvider {
public static void main(String[] args) {
// 将服务注册到guice中,默认使用本地zookeeper作为注册中心(127.0.0.1:2181),需要确保本地安装并启动了zk
RpcContext.setConfiguration(new ServerConfig());
Protocol protocol = RpcContext.getBean(Protocol.class);
protocol.export();
}
}
package org.applesline;
import org.applesline.api.GreetingService;
import org.applesline.mini.dubbo.context.RpcContext;
import org.applesline.mini.dubbo.proxy.ProxyFactory;
public class ServiceConsumer {
public static void main(String[] args) {
// 默认使用本地zookeeper作为注册中心(127.0.0.1:2181),需要确保本地安装并启动了zk
RpcContext.initContext();
GreetingService greetingService = ProxyFactory.getProxyObject(GreetingService.class);
System.out.println(greetingService.sayHello("mini-dubbo"));
}
}
联系作者