【dubbo系列】dubbo快速啓動

zookeeper

引入jar

 <dependencies>
        <!-- Dubbo dependency -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.5</version>
        </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo-registry-nacos</artifactId>
        <version>2.6.7</version>
    </dependency>
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.5.0-alpha</version>
    </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.11.1</version>
        </dependency>
    </dependencies>

接口及實現

public interface IUserService {

    public String getUserInfo(String aa);
}
public class UserService implements IUserService {
    public String getUserInfo(String aa) {
        System.out.println("這裏是服務器:aa:"+aa);
        return aa;
    }
}

服務端啓動


    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        // 服務實現
        IUserService xxxService = new UserService();

		// 當前應用配置
        ApplicationConfig application = new ApplicationConfig();
        application.setName("xxx");

		// 連接註冊中心配置
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("zookeeper://127.0.0.1:2181");
        registry.setClient("curator");
        registry.setUsername("nacos");
        registry.setPassword("nacos");

		// 服務提供者協議配置
        ProtocolConfig protocol = new ProtocolConfig();
        protocol.setName("dubbo");
        protocol.setPort(12345);
        protocol.setThreads(200);

		// 注意:ServiceConfig爲重對象,內部封裝了與註冊中心的連接,以及開啓服務端口

		// 服務提供者暴露服務配置
        ServiceConfig<IUserService> service = new ServiceConfig<IUserService>(); // 此實例很重,封裝了與註冊中心的連接,請自行緩存,否則可能造成內存和連接泄漏
        service.setApplication(application);
        service.setRegistry(registry); // 多個註冊中心可以用setRegistries()
        service.setProtocol(protocol); // 多個協議可以用setProtocols()
        service.setInterface(IUserService.class);
        service.setRef(xxxService);
        service.setVersion("1.0.0");

        // 暴露及註冊服務
        service.export();
        countDownLatch.await();
    }

客戶端消費

 public static void main(String[] args) {
        // 當前應用配置
        ApplicationConfig application = new ApplicationConfig();
        application.setName("yyy");

		// 連接註冊中心配置
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("zookeeper://127.0.0.1:2181");
        registry.setUsername("aaa");
        registry.setPassword("bbb");

		// 注意:ReferenceConfig爲重對象,內部封裝了與註冊中心的連接,以及與服務提供方的連接

		// 引用遠程服務
        ReferenceConfig<IUserService> reference = new ReferenceConfig<IUserService>(); // 此實例很重,封裝了與註冊中心的連接以及與提供者的連接,請自行緩存,否則可能造成內存和連接泄漏
        reference.setApplication(application);
        reference.setRegistry(registry); // 多個註冊中心可以用setRegistries()
        reference.setInterface(IUserService.class);
        reference.setVersion("1.0.0");

		// 和本地bean一樣使用xxxService
        IUserService iUserService = reference.get();
        iUserService.getUserInfo("111");
    }

Nacos

註冊中心啓動

從git上下載源碼

我下載的是1.3.0版本的。

Windows

啓動命令:

cmd startup.cmd

或者雙擊startup.cmd運行文件。

linux啓動配置:https://nacos.io/zh-cn/docs/quick-start.html

啓動之後如下
在這裏插入圖片描述
啓動成功後

進入紅框的地址進行登錄註冊中心

賬號密碼:nacos/nacos
在這裏插入圖片描述
註冊中心啓動成功。

dubbo配置

pom引入

    <dependencies>
        <!-- Dubbo dependency -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.5</version>
        </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo-registry-nacos</artifactId>
        <version>2.6.7</version>
    </dependency>

    <!-- Keep latest Nacos client version -->
    <dependency>
        <groupId>com.alibaba.nacos</groupId>
        <artifactId>nacos-client</artifactId>
        <version>1.2.1</version>
    </dependency>
        <!-- Alibaba Spring Context extension -->
        <dependency>
            <groupId>com.alibaba.spring</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>1.0.2</version>
        </dependency>
 
    </dependencies>

服務端啓動


    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        // 服務實現
        IUserService xxxService = new UserService();

		// 當前應用配置
        ApplicationConfig application = new ApplicationConfig();
        application.setName("xxx");

		// 連接註冊中心配置
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("nacos://127.0.0.1:8848");
//        registry.setClient("curator");
        registry.setUsername("nacos");
        registry.setPassword("nacos");

		// 服務提供者協議配置
        ProtocolConfig protocol = new ProtocolConfig();
        protocol.setName("dubbo");
        protocol.setPort(12345);
        protocol.setThreads(200);

		// 注意:ServiceConfig爲重對象,內部封裝了與註冊中心的連接,以及開啓服務端口

		// 服務提供者暴露服務配置
        ServiceConfig<IUserService> service = new ServiceConfig<IUserService>(); // 此實例很重,封裝了與註冊中心的連接,請自行緩存,否則可能造成內存和連接泄漏
        service.setApplication(application);
        service.setRegistry(registry); // 多個註冊中心可以用setRegistries()
        service.setProtocol(protocol); // 多個協議可以用setProtocols()
        service.setInterface(IUserService.class);
        service.setRef(xxxService);
        service.setVersion("1.0.0");

        // 暴露及註冊服務
        service.export();
        countDownLatch.await();
    }

啓動成功之後,註冊中心會有數據顯示出來。
在這裏插入圖片描述
客戶端消費

public static void main(String[] args) {
        // 當前應用配置
        ApplicationConfig application = new ApplicationConfig();
        application.setName("yyy");

		// 連接註冊中心配置
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("nacos://127.0.0.1:8848");
        registry.setUsername("aaa");
        registry.setPassword("bbb");

		// 注意:ReferenceConfig爲重對象,內部封裝了與註冊中心的連接,以及與服務提供方的連接

		// 引用遠程服務
        ReferenceConfig<IUserService> reference = new ReferenceConfig<IUserService>(); // 此實例很重,封裝了與註冊中心的連接以及與提供者的連接,請自行緩存,否則可能造成內存和連接泄漏
        reference.setApplication(application);
        reference.setRegistry(registry); // 多個註冊中心可以用setRegistries()
        reference.setInterface(IUserService.class);
        reference.setVersion("1.0.0");

		// 和本地bean一樣使用xxxService
        IUserService iUserService = reference.get();
        iUserService.getUserInfo("111");
    }

y);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章