HK仅輝 发表于 2022-9-9 16:57

dubbo学习记录(一)

注意:本地要下载zookeeper
API接口
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);

    }

}

小木hwy 发表于 2022-9-9 17:18

支持!!!!!!!!

xiadongming 发表于 2022-9-10 11:50

页: [1]
查看完整版本: dubbo学习记录(一)