天天看點

一、Dubbo+SpringBoot+zookeerper整合(XML方式)Git位址:https://github.com/boorZ/demo-dubbo-xml注意:Git位址上的項目與本文章有差同,請參照文章與Git。項目結構(這是個父子項目)

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

注意:Git位址上的項目與本文章有差同,請參照文章與Git。

項目結構(這是個父子項目)

一、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 inter ref="demoOne1ServiceImpl"/>

    <bean id="demoOne2ServiceImpl" class="com.example.dubboprovider.impl.DemoOne2ServiceImpl"/>
    <dubbo:service inter 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" inter/>
    <dubbo:reference id="DemoOne2Service" inter/>

</beans>      
application.properties
server.port=9091      
Application 就是SpringBoot的啟動類 添加注解讓SpringBoot啟動時讀取Dubbo配置檔案
@ImportResource("classpath:dubbo-consumer.xml")      

繼續閱讀