dubbo学习记录(一)
注意:本地要下载zookeeperAPI接口
public interface GreetingsService {
String sayHi(String name);
}
API实现
import com.api.GreetingsService;
public class GreetingsServiceImpl implements GreetingsService {
@Override
public String sayHi(String name) {
return "hi, " + name;
}
}
注册dubbo服务
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();
}
}
客户端
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);
}
}
支持!!!!!!!!
页:
[1]