dubbo+zookeeper+springmvc实现

2019-07-05  本文已影响0人  simperLv

这几天给公司新项目搭一下dubbo,过程简直无语。记录一下吧

zookeeper下载,启动。直接网上找个教程,分分钟搞定就行。这里不详细说了。
然后就是下一个dubbo-admin,可视化管理嘛。然后我看网上都是什么进入dubbo-admin里面去打包,于是我直接去github下了最新版本的dubbo。发现下下来没有dubbo-admin,emmm,然后直接在外面执行命令。果然打包完后target下面没有出现war包,后面看dubbo版本介绍的时候,发现在2.6.1就已经将dubbo-admin, dubbo-monitor等OPS模块剥离到一个单独的工程仓库中。懒得看新版本怎么弄,就下了2.5.10版本,顺利打包。找个tomcat把webapp下面除了ROOT文件夹全部删掉,据说是防止一些bug。成功启动进入管理页。


dubbo.png

然后就是建一个dubbo-api用于提供接口,下面是项目截图,很随意。


dubbo-api.png
然后将dubbo-api install一下。
到服务提供者工程的pom文件里面引入一下jar包。然后实现,再去配置文件中引入。
<dependency>
            <groupId>{your groupid}</groupId>
            <artifactId>{your artifactId }</artifactId>
            <version>{your versoin}</version>
</dependency>
@Service
public class TestServiceImpl implements DfrcService {

    @Override
    public String sayHello(String name) {
        return "hello"+name;
    }
}

<?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"
       xmlns:context="http://www.springframework.org/schema/context"
    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="dfrc_provider" />

    <dubbo:protocol name="dubbo" port="20880" />
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" client="curator"  />
    <dubbo:service interface="com.service.DfrcService" ref="dfrcService"/>
    <bean id="dfrcService" class="{实现类路径}...service.impl.TestServiceImpl"/>
</beans>

弄完直接启动,就可以在dubbo-admin管理页面看到了


dubbo-admin-provider.png

然后消费者工程同样是pom文件引入jar包,然后配置,再使用提供者提供的接口实现

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="dfrc_consumer" />

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 声明需要使用的服务接口,即作为消费者,只需要知道接口即可,不依赖具体实现 -->
    <dubbo:reference interface="com.service.DfrcService" id="dfrcService" check="false"/>

    <dubbo:consumer check="false" />

    <dubbo:registry check="false" />
</beans>
@Controller
@RequestMapping("/test/testController")
public class TestController {

    @Autowired
    private DfrcService dfrcService;


    @RequestMapping({"index.do"})
    public void hello() {
        String str = dfrcService.sayHello("lv");
        System.out.println(str);
    }

启动消费者工程,再看dubbo-admin页面。


dubbo-admin-consumer.png

运行一下,OK!


result.png

记录一下自己比较蠢的地方

1、一开始看网上的博客,没仔细阅读,忘记还有打jar包这一回事。以为需要在消费者工程建相同的目录结构去调用,后来发现好像不对。
2、最开始是把服务提供者打包了,消费者引用后,我才想到这样和dubbo,zookeeper这些没啥关系...运行起来也是两个提供者。
3、最坑的地方,就是服务提供者引入开始创建的dubbo-api包后,启动工程一直报错,java.lang.ClassNotFoundException:com.service.DfrcService is not found。找了各种原因,最后在一位大佬的博客上找到了灵感。因为dubbo-api里面的接口没有加注解,Spring扫描的时候没有把它注册成bean。而dubbo必须引用已存在的service对象。

上一篇下一篇

猜你喜欢

热点阅读