Springboot整合Dubbo和zookeeper-----XML版,即是服务消费者又是服务提供者

笔者个人更喜欢SpringCloud,也更喜欢它的一系列的解决方案。但是技术不在喜不喜欢,而是要符合市场需求。dubbo也是一个成熟的分布式服务的解决方案。所以笔者现在分享Xml方式SpringBoot整合Dubbo和Zookeeper。

背景原因,笔者也尝试过使用无xml的方式使用starter的方式整合过Dubbo,但是很遗憾,可能是笔者能力有限,在配置多个注册中心时笔者一直不成功,无赖的情况下,笔者只能选择xml方式。

首先看看项目结构:

其中dubbo-api中是定义接口和实体类的模块,dubbo1和dubbo2两者既是服务消费者,又是服务提供者。

dubbo1和dubbo2都要maven依赖于dubbo-api。

dubbo-api模块就不再多说,我们具体说说dubbo1和dubbo2

dubbo1:

首先是dubbo1的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wangye</groupId>
    <artifactId>dubbo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo1</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- _________________________________________________dubbo开始_______________________________________ -->

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.0</version>
        </dependency>


        <!-- Zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.14</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.1.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.1.0</version>
        </dependency>
        <!-- _________________________________________________dubbo结束_______________________________________ -->

        <!-- 依赖公共接口 -->
        <dependency>
            <groupId>com.wangye</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

重点依赖是下面这些:

<!-- _________________________________________________dubbo开始_______________________________________ -->

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.0</version>
        </dependency>


        <!-- Zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.14</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.1.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.1.0</version>
        </dependency>
        <!-- _________________________________________________dubbo结束_______________________________________ -->

上面主要是dubbo依赖和zk。

那么看看我们dubbo1的application.properties

server.port=8081

#dubbo配置
# zk注册中心url
registryUrl=zookeeper1.XXXXX.org:16001,zookeeper2.XXXXX.org:16002,zookeeper3.XXXXX.org:16003
#dubbo1提供服务的版本号
dubbo1Version=dubbo1-DEV
#dubbo2提供服务的版本号
dubbo2Version=dubbo2-DEV
#dubbo服务的端口号
dubboPort=20002

注意上面的配置了多个注册中心

然后看看dubbo1的项目结构:

既然我们有实现的类,那么就看看:

HelloServiceImpl.java:

import com.wangye.dubboapi.api.HelloService;
import org.springframework.stereotype.Service;

@Service("helloService")
public class HelloServiceImpl implements HelloService {

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

注意这里使用的service的注解是Spring的注解,接下来只要在provider.xml提供服务的标签中加上ref关联这个服务类就好

下面看看dubbo-config.xml:

<?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="dubbo1"  />
    
    <dubbo:registry protocol="zookeeper" address="${registryUrl}" client="curator"/>
 
    <dubbo:consumer loadbalance="leastactive" check="false" init="false" layer="service"/>
    
    <dubbo:protocol name="dubbo" port="${dubboPort}" />

 	<dubbo:provider version="${dubbo1Version}" protocol="dubbo"   loadbalance="leastactive" cluster="failfast" timeout="60000" layer="service"/>
 	
 	<import resource="classpath:rmi/consumer.xml" /> 
 	
 	<import resource="classpath:rmi/provider.xml" /> 
</beans>

这里配置了dubbo应用的名字,注册中心协议、url、以及使用的客户端,配置了dubbo消费端免检查,提供服务的版本。以及import消费者和提供者文件配置。

接下来是provider.xml的配置:

<?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:service interface="com.wangye.dubboapi.api.HelloService" ref="helloService" version="${dubbo1Version}"/>
 </beans>

上面要注意,ref属性的值一定要和@Service(”helloService“) 里的helloService是一样的。

consumer.xml的配置:

<?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:reference interface="com.wangye.dubboapi.api.ByeService" id="byeService" version="${dubbo2Version}"/>
</beans>

最后看一下controller:

import com.wangye.dubboapi.api.ByeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ByeController {

    //这里调用了dubbo2模块提供的服务
    @Autowired
    private ByeService byeService;

    @RequestMapping("/sayBye/{name}")
    public String sayBye(@PathVariable String name){
        return byeService.sayBye(name);
    }

}

dubbo2配置:

dubbo2的配置和dubbo1的配置几乎以模一样,就是提供的服务和消费的服务不一样。

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wangye</groupId>
    <artifactId>dubbo2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo2</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- _________________________________________________dubbo开始_______________________________________ -->

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.0</version>
        </dependency>


        <!-- Zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.14</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.1.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.1.0</version>
        </dependency>
        <!-- _________________________________________________dubbo结束_______________________________________ -->

        <dependency>
            <groupId>com.wangye</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.properties:

server.port=8082

#dubbo配置
# zk注册中心url
registryUrl=zookeeper1.XXXXX.org:16001,zookeeper2.XXXXX.org:16002,zookeeper3.XXXXX.org:16003
#dubbo1提供服务的版本号
dubbo1Version=dubbo1-DEV
#dubbo2提供服务的版本号
dubbo2Version=dubbo2-DEV
#dubbo服务的端口号
dubboPort=20002

service接口实现类:

@Service("byeService")
public class ByeServiceImpl implements ByeService {

    @Override
    public String sayBye(String name) {
        return "Bye " + name;
    }

}

dubbo-config.xml:

<?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="dubbo2"  />
    
    <dubbo:registry protocol="zookeeper" address="${registryUrl}" client="curator"/>
 
    <dubbo:consumer loadbalance="leastactive" check="false" init="false" layer="service"/>
    
    <dubbo:protocol name="dubbo" port="${dubboPort}" />

 	<dubbo:provider version="${dubbo2Version}" protocol="dubbo" loadbalance="leastactive" cluster="failfast" timeout="60000" layer="service"/>
 	
 	<import resource="classpath:rmi/consumer.xml" /> 
 	
 	<import resource="classpath:rmi/provider.xml" /> 
</beans>

provider.xml:

<?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:service interface="com.wangye.dubboapi.api.ByeService" ref="byeService" version="${dubbo2Version}"/>

 </beans>

consumer.xml:

<?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:reference interface="com.wangye.dubboapi.api.HelloService" id="helloService" version="${dubbo1Version}"/>

</beans>

controller:

@RestController
public class HelloController {

    // 这里调用的是dubbo1提供的服务
    @Autowired
    private HelloService helloService;

    @RequestMapping("/sayHello/{name}")
    public String sayBye(@PathVariable String name){
        return helloService.sayHello(name);
    }

}

然后各自启动SpringBoot项目测试controller接口就好了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值