一、Dubbo+SpringBoot+zookeerper整合(XML方式)

Git地址:https://github.com/boorZ/demo-dubbo-xml

注意:Git地址上的项目与本文章有差同,请参照文章与Git。

 

项目结构(这是个父子项目)

还是解析下:

  • demo-dubbo-xml是父项目
  • dubbo-service 是Service接口(如果您们要问:为什么我要把Service接口与Service实现类分开。对不起,个人喜好)
  • dubbo-provider 是服务提供者
  • dubbo-consumer 是服务消费者

父项目(demo-dubbo-xml)没有什么需要配置的。默认就好。

dubbo-service所需更改:

Demo1Service
public interface DemoOne2Service {
    String test();
}
Demo2Service
public interface DemoOne2Service {
    String test();
}

dubbo-provider所需更改:

pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- dubbo 和 zookeeper 依赖-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.6</version>
</dependency>
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.9</version>
</dependency>
<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.2</version>
</dependency>
<!-- 这是我的Service包 -->
<dependency>
    <groupId>com.example</groupId>
    <artifactId>dubbo-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <scope>compile</scope>
</dependency>

Demo1ServiceImpl

public class DemoOne1ServiceImpl implements DemoOne1Service {

   public String test() {
      return "One-A";
   }
}
Demo2ServiceImpl
public class DemoOne2ServiceImpl implements DemoOne2Service {

   public String test() {
      return "One-B";
   }
}
dubbo-provider.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">


    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="dubbo-provider"/>

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

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- 暴露服务 -->
    <bean id="demoOne1ServiceImpl" class="com.example.dubboprovider.impl.DemoOne1ServiceImpl"/>
    <dubbo:service interface="com.example.dubboservice.DemoOne1Service" ref="demoOne1ServiceImpl"/>

    <bean id="demoOne2ServiceImpl" class="com.example.dubboprovider.impl.DemoOne2ServiceImpl"/>
    <dubbo:service interface="com.example.dubboservice.DemoOne2Service" ref="demoOne2ServiceImpl"/>

</beans>
application.properties
server.port=9000
Application就是SpringBoot的启动类 添加注解让SpringBoot启动时读取Dubbo配置文件
@ImportResource(value = {"classpath:dubbo-provider.xml"})

dubbo-consumer所需更改:

pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- dubbo 和 zookeeper 依赖-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.6</version>
</dependency>
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.9</version>
</dependency>
<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.2</version>
</dependency>

<!-- 这是我的Service包 -->
<dependency>
    <groupId>com.example</groupId>
    <artifactId>dubbo-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <scope>compile</scope>
</dependency>
HelloController
@RestController
public class HelloController {

   @Autowired
   private DemoOne1Service one1Service;

   @Autowired
   private DemoOne2Service one2Service;

   @GetMapping(value = "/hello1")
   public String hello1() {
      return one1Service.test();
   }

   @GetMapping(value = "/hello2")
   public String hello2() {
      return one2Service.test();
   }

}
dubbo-provider.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="demo-consumer"/>

    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!-- 要扫描的包路径 使用注解方式创建服务 -->
    <dubbo:annotation package="com.example.dubboconsumer" />

    <!--
        服务消费者引用服务配置
            id:服务引用BeanId(必填)
            interface:服务接口名(必填)

            version:服务版本,与服务提供者的版本一致
            check:启动时检查提供者是否存在,true报错,false忽略
            url:点对点直连服务提供者地址,将绕过注册中心(就是经常说的:直连)
            详细请看:http://dubbo.apache.org/zh-cn/docs/user/references/xml/dubbo-reference.html
    -->
    <dubbo:reference id="DemoOne1Service" interface="com.example.dubboservice.DemoOne1Service"/>
    <dubbo:reference id="DemoOne2Service" interface="com.example.dubboservice.DemoOne2Service"/>

</beans>
application.properties
server.port=9091
Application就是SpringBoot的启动类 添加注解让SpringBoot启动时读取Dubbo配置文件
@ImportResource("classpath:dubbo-consumer.xml")
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值