【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);

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叁滴水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值