Spring Boot 整合 Dubbo和Zookeeper

2021-07-12 16:06

阅读:685

标签:keep   spring   sam   格式   1.5   ret   reporting   jar   self   

Spring Boot 整合 Dubbo和Zookeeper

 

  • Spring Boot 整合 Dubbo和Zookeeper
    • 环境介绍
    • Zookeeper 安装
      • 启动
    • Dubbo admin 搭建
    • 创建主maven项目
      • 创建子spring boot项目

 

环境介绍

  • zookeeper 安装
  • dubbo-admin 查看管理注册中心服务提供者和消费者

Zookeeper 安装

http://zookeeper.apache.org/releases.html

下载最新安装包,文件格式是tar.gz,可见Apache还是推荐在Linux上使用,但也提供了在windows上使用的脚本。 
解压之后,修改数据和日志配置,注意在3.5.3版本上,conf文件夹里不在直接提供zoo.cfg,而是提供了一个example,zoo_sample.cfg。复制重命名为zoo.cfg,然后修改data位置新增log位置。注意,windows目录要用//分割。

dataDir=D:\\software\\zookeeper-3.5.3-beta\\zookeeper\\data
dataLogDir=D:\\software\\zookeeper-3.5.3-beta\\zookeeper\\log
  • 修改zkEnv.cmd 
    防止win10 闪退,注意双引号的位置
if not exist "%JAVA_HOME%\bin\Java.exe"

set JAVA="%JAVA_HOME%\bin\java"

 

启动

使用zkServer.cmd启动Zookeeper即可。注意Zookeeper是运行在8080端口,服务接口是2181

Dubbo admin 搭建

https://github.com/alibaba/dubbo

https://github.com/dangdangdotcom/dubbox

下载dubbo,执行

mvn install -D maven.test.skip=true

 

然后在dubbo-admin下执行

mvn package -D maven.skip.test=true

打war包,发布到tomcat下,注意修改tomcat端口8081

创建主maven项目

技术分享图片

pom.xml

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    modelVersion>4.0.0modelVersion>

    groupId>com.rencc.dubbogroupId>
    artifactId>dubbo-spring-studyartifactId>
    version>0.0.1-SNAPSHOTversion>
    packaging>pompackaging>

    modules>
        module>dubbo-providermodule>
        module>dubbo-consumermodule>
    modules>

    properties>
        project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        java.version>1.8java.version>
    properties>

project>

 

创建子spring boot项目

  • dubbo-provider 服务提供者

在application.properties 里设置端口,目前8080和8081已被使用

server.port=8090

 

添加dubbo的配置文件

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="dubbo-provider" owner="dubbo-provider" />

    
    dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" client="zkclient" />

    
    dubbo:protocol name="dubbo" port="-1" />

    
    dubbo:service interface="com.rencc.dubbo.provider.service.DemoService"
        ref="demoService" timeout="10000" />

    
    bean id="demoService" class="com.rencc.dubbo.provider.serviceImpl.DemoServiceImpl" />

beans>

 

DemoService 接口和实现类

package com.rencc.dubbo.provider.service;

public interface DemoService {
    String sayHello(String name);
}





package com.rencc.dubbo.provider.serviceImpl;

import com.rencc.dubbo.provider.service.DemoService;

public class DemoServiceImpl implements DemoService {

    public String sayHello(String name) {
        return "Welcome to Dubbo, Hello " + name;
    }

}

 

pom.xml

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    modelVersion>4.0.0modelVersion>

    groupId>com.rencc.dubbo.providergroupId>
    artifactId>dubbo-providerartifactId>
    packaging>jarpackaging>

    name>dubbo-providername>
    description>Demo project for Spring Bootdescription>

    parent>  
        groupId>com.rencc.dubbogroupId>
        artifactId>dubbo-spring-studyartifactId>
        version>0.0.1-SNAPSHOTversion>
    parent>

    properties>
        project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        java.version>1.8java.version>
    properties>

    dependencies>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-webartifactId>
            version>1.5.2.RELEASEversion>
        dependency>

        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starterartifactId>
            version>1.5.2.RELEASEversion>
        dependency>

        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-testartifactId>
            scope>testscope>
            version>1.5.2.RELEASEversion>
        dependency>

        dependency>
            groupId>com.alibabagroupId>
            artifactId>dubboartifactId>
            version>2.4.10version>
            exclusions>
                exclusion>
                    artifactId>springartifactId>
                    groupId>org.springframeworkgroupId>
                exclusion>
            exclusions>
        dependency>

        dependency>
            groupId>org.apache.zookeepergroupId>
            artifactId>zookeeperartifactId>
            version>3.4.6version>
            exclusions>
                exclusion>
                    artifactId>slf4j-log4j12artifactId>
                    groupId>org.slf4jgroupId>
                exclusion>
            exclusions>
        dependency>

        dependency>
            groupId>com.github.sgroschupfgroupId>
            artifactId>zkclientartifactId>
            version>0.1version>
        dependency>

    dependencies>

    build>
        plugins>
            plugin>
                groupId>org.springframework.bootgroupId>
                artifactId>spring-boot-maven-pluginartifactId>
                version>1.5.2.RELEASEversion>
            plugin>
        plugins>
    build>


project>
  • dubbo-consumer 服务消费者 
    在此仅贴出dubbo代码配置。
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="dubbo-consumer" owner="dubbo-consumer" />

    
    dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" client="zkclient" />

    
    dubbo:reference id="demoService" interface="com.rencc.dubbo.provider.service.DemoService" />

beans>

Spring Boot 整合 Dubbo和Zookeeper

标签:keep   spring   sam   格式   1.5   ret   reporting   jar   self   

原文地址:https://www.cnblogs.com/thinheader/p/9545910.html


评论


亲,登录后才可以留言!