Spring Alibaba Nacos替换dubbo zookeeper

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/GoSaint/article/details/85039317

阿里巴巴不得不说在开源方面是国内做的最好的,没有之一吧。先是druid数据库连接池。笔者有幸在学习之处就使用到它,它是在2012年开源的,但是大规模的使用却是在2015年。后来又推出了dubbo RPC框架。这个在2012年开源的,但是后续没有人维护,再到后来携程pull了一个分支自己去维护,也就是dubbox。直到2017年dubbo开始复活,并且每个月发布新的版本。在国内dubbo的使用还是比较多的。然后就是接触到了RocketMQ。有个消息中间件,和dubbo一样,同样是apache基金会顶级项目。这三款开源产品现在是比较流行的。今年Spring Cloud默认的注册中心Eureka现在是闭源啦,然后就是Hytrix这个服务隔离也闭源啦。因此阿里巴巴开源团队以及国内的一些开源大神开始用nacos准备替换Eureka,还有一个功能是可以作为配置中心,之前携程的Apollo也是作为配置中心的。好了,话不多说,下面的实例工程是我使用nacos作为zookeeper的替代者作为注册中心。需要说明的是nacos现在的版本是0.6.1。因此它暂时还是不能用在生产上,但是预计2019年会正式的开源使用。

先下载nacos服务器,笔者在windows下操作的,当然linux或者是mac的具体安装可以见官网:https://nacos.io 。

上图是下载后的nacos的目录结构。点击bin进去之后直接运行startup.cmd即可启动nacos服务。下面表示启动成功。

                                              

上图是我构建的dubbo服务的工程结构。API是一个公共接口。Provider是服务的提供者,Consumer是服务的消费者。很简单的入门demo.

API接口代码如下:

/**
 * @author gosaint
 */
public interface DemoService {
    String sayHello(String name);
}

      然后我们搭建Provider,服务的提供者。首先要加nacos的jar包。还有要说明一点,必须加上log4j-core这个jar包。否则会报错。这是nacos的一个bug。我还有点小小的激动,刚才我问了下nacos开源组织大神小马哥,小马哥说这确实是一个bug,然我解决,我希望可以成为我nacos的一个commiter。下面的依赖涉及Spring、Dubbo的依赖我不展示了,只展示nacos的依赖以及log4j-core。就是一个单纯的dubbo工程,只不过加上这两个jar即可。

       <!-- Dubbo Nacos registry dependency -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo-registry-nacos</artifactId>
            <version>0.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.10.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.10.0</version>
        </dependency>

然后是dubbo的配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!--应用名称-->
    <dubbo:application name="demo-dubbo"/>
    <!--注册中心 这里使用Zookeeper-->
   <!-- <dubbo:registry address="zookeeper://127.0.0.1:2182"/>-->
    <!--使用nacos作为注册中心-->
    <dubbo:registry address="nacos://127.0.0.1:8848" />
    <!--用dubbo协议在20880端口暴露服务-->
    <dubbo:protocol name="dubbo" port="20885"/>
    <!--接口以及实现类-->
    <dubbo:service interface="com.cmos.interfaces.DemoService" ref="demoService" />
    <bean id="demoService" class="com.cmos.service.DemoServiceImpl"/>
</beans>

    需要说明的是nacos的默认端口是8848.然后我们写接口的实现以及Dubbo的启动类:

/**
 * @author gosaint 
 */
public class DemoServiceImpl implements DemoService {
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

下面是dubbo的启动类:

/**
 * @author gosaint
 */
public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] {"META-INF/spring/dubbo-demo-provider.xml"});
        context.start();
        System.out.println("服务启动!");
        System.in.read();
    }
}

运行方法,启动成功!

下面继续搭建Consumer。pom依赖基本一致,我就不贴代码啦。然后就是配置文件和服务dubbo消费者的启动。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!--应用名称-->
    <dubbo:application name="demo-dubbo"/>
    <!--注册中心 这里使用Zookeeper-->
   <!-- <dubbo:registry address="zookeeper://127.0.0.1:2182"/>-->
    <dubbo:registry address="nacos://127.0.0.1:8848"/>
    <dubbo:reference id="demoService" interface="com.cmos.interfaces.DemoService"/>

</beans>

启动类如下:

/**
 * @author gosaint
 */
public class Consumer {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext
                (new String[] {"META-INF/spring/dubbo-demo-consumer.xml"});
        context.start();
        // 获取远程服务代理
        DemoService demoService = (DemoService)context.getBean("demoService");
        // 执行远程方法
        String hello = demoService.sayHello("world");
        // 显示调用结果
        System.out.println( hello );
        System.in.read();
    }
}

最后的运行结果:Hello World。是不是和zookeeper基本一致呢。关于nacos的基本实现我还没有了解过,因此无从比较优劣。最后nacos提供了一个管理页面,可以直接访问http://127.0.0.1:8848/nacos/index.html 。如下是我的管理界面:

猜你喜欢

转载自blog.csdn.net/GoSaint/article/details/85039317
今日推荐