Dubbo+Zookeeper+SpringBoot家族实现多模块分布式项目

项目搭建

搭建父工程

选择maven项目

Dubbo+Zookeeper+SpringBoot家族实现多模块分布式项目_第1张图片

填写GroupId和ArtifactId

Dubbo+Zookeeper+SpringBoot家族实现多模块分布式项目_第2张图片

此时项目结构如下

Dubbo+Zookeeper+SpringBoot家族实现多模块分布式项目_第3张图片

修改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.bloggroupId>
    <artifactId>blogartifactId>
    <version>1.0-SNAPSHOTversion>

    
    <packaging>pompackaging>

project>

搭建子模块

在blog项目的基础上,搭建接口模块api

Dubbo+Zookeeper+SpringBoot家族实现多模块分布式项目_第4张图片

Dubbo+Zookeeper+SpringBoot家族实现多模块分布式项目_第5张图片

依次建立consumer、provider,此时项目结构如下

Dubbo+Zookeeper+SpringBoot家族实现多模块分布式项目_第6张图片

修改每个module的pom文件

根项目的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.bloggroupId>
    <artifactId>blogartifactId>
    <version>1.0-SNAPSHOTversion>

    
    <modules>
        <module>apimodule>
        <module>consumermodule>
        <module>providermodule>
    modules>

    
    <packaging>pompackaging>

    
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.2.5.RELEASEversion>
    parent>

    
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <java.version>1.8java.version>
        <dubbo.version>2.5.5dubbo.version>
        <zkclient.version>0.10zkclient.version>
        <lombok.version>1.16.18lombok.version>
        <spring-boot.version>1.5.7.RELEASEspring-boot.version>
    properties>


    
    <dependencyManagement>
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starterartifactId>
                <version>${spring-boot.version}version>
            dependency>

            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
                <version>${spring-boot.version}version>
            dependency>

            
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <version>${lombok.version}version>
                <scope>providedscope>
            dependency>

            
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>dubboartifactId>
                <version>${dubbo.version}version>
            dependency>

            
            <dependency>
                <groupId>com.101tecgroupId>
                <artifactId>zkclientartifactId>
                <version>${zkclient.version}version>
            dependency>
        dependencies>
    dependencyManagement>

project>

api的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">
    <parent>
        <artifactId>blogartifactId>
        <groupId>com.bloggroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>apiartifactId>

    <dependencies>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <scope>providedscope>
        dependency>
    dependencies>
project>

provider的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">
    <parent>
        <artifactId>blogartifactId>
        <groupId>com.bloggroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>providerartifactId>

    <dependencies>
        <dependency>
            <groupId>com.bloggroupId>
            <artifactId>blogartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
            <version>2.2.5.RELEASEversion>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>dubboartifactId>
        dependency>
        <dependency>
            <groupId>com.101tecgroupId>
            <artifactId>zkclientartifactId>
        dependency>
        <dependency>
            <groupId>com.bloggroupId>
            <artifactId>apiartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
    dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>


project>

consumer的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">
    <parent>
        <artifactId>blogartifactId>
        <groupId>com.bloggroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>consumerartifactId>

    <dependencies>
        <dependency>
            <groupId>com.bloggroupId>
            <artifactId>blogartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
            <version>2.2.5.RELEASEversion>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>dubboartifactId>
        dependency>
        <dependency>
            <groupId>com.101tecgroupId>
            <artifactId>zkclientartifactId>
        dependency>
        <dependency>
            <groupId>com.bloggroupId>
            <artifactId>apiartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
    dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

项目配置

测试接口

在api中创建以下两个文件

Dubbo+Zookeeper+SpringBoot家族实现多模块分布式项目_第7张图片

Article:

package com.boot.model;


import lombok.Data;

import java.util.Date;

@Data
public class Article implements Serializable{
     

    private String name;

    private String type;

    private String author;

    private Date publishDate;

    private Integer score;
}


TestService:

package com.boot.service;

import com.boot.model.Article;

public interface TestService {
     

    String descArticle();

    Article find();
}


在provider中创建文件结构及文件

Dubbo+Zookeeper+SpringBoot家族实现多模块分布式项目_第8张图片

TestServiceImpl:

package com.boot.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.boot.model.Article;
import com.boot.service.TestService;

import java.util.Date;

@Service
public class TestServiceImpl implements TestService {
     


    @Override
    public String descArticle() {
     
        return "this is kbq's Article";
    }

    @Override
    public Article find() {
     
        Article article = new Article();
        article.setAuthor("kbq");
        article.setName("Dubbo");
        article.setPublishDate(new Date());
        article.setScore(100);
        article.setType("program");
        return article;
    }
}

ProviderApplication:

package com.boot;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

import java.io.IOException;

@SpringBootApplication
@ImportResource({
     "classpath:config/spring-dubbo.xml"})
public class ProviderApplication {
     

    public static void main(String[] args) {
     
        SpringApplication.run(ProviderApplication.class, args);
        try {
     
            System.in.read();
        } catch (IOException e) {
     
            e.printStackTrace();
        }
    }
}

spring-dubbo.xml:


<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="provider"/>
    
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    
    <dubbo:annotation package="com.boot.service.impl"/>
beans>

application.yml:

# 在这里编写springboot的配置信息
server:
  port: 8091

在consumer中创建文件结构以及文件

Dubbo+Zookeeper+SpringBoot家族实现多模块分布式项目_第9张图片

TestController:

package com.boot.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.boot.model.Article;
import com.boot.service.TestService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class TestController {
     

    @Reference(version = "1.0.0")
    private TestService testService;

    @GetMapping("hello")
    public String hello() {
     
        return testService.descArticle();
    }

    @GetMapping("user")
    public Article user() {
     
        return testService.find();
    }
}

ConsumerApplication:

package com.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource({
     "classpath:config/spring-dubbo.xml"})
public class ConsumerApplication {
     

    public static void main(String[] args) {
     
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

spring-dubbo.xml:


<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="consumer"/>
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <dubbo:annotation package="com.boot.controller"/>
beans>

application.yml:

# 在这里编写springboot的配置信息
server:
  port: 8090

以上就是项目的初始搭建过程,后面配置模块只会以各自的文件出现,只需要在上述的基础上增加即可

整合MongoDB

准备MongoDB的环境,建立名为blog的库

1、打开命令窗口,切换到mongodb安装目录下的“bin”目录中

2、启动服务。输入命令:”mongod --dbpath E:\mongodb\data

推荐使用Robot工具连接MongoDB,建好如下图所示:

Dubbo+Zookeeper+SpringBoot家族实现多模块分布式项目_第10张图片

添加依赖

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-data-mongodbartifactId>
dependency>

添加配置

spring:
  data:
    mongodb:
      uri: mongodb://localhost:27017/blog

Entity:

package com.boot.service.entity;


import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.Date;

@Document(collection = "article")
@Data
public class ArticleDO {
     

    private String title;

    private String author;

    private Date createdTime;
}

Mapper:

package com.boot.service.mapper;

import com.boot.service.entity.ArticleDO;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;


@Repository
public interface ArticleMapper extends MongoRepository<ArticleDO, String> {
     
}

ServiceImpl:

package com.boot.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.boot.model.Article;
import com.boot.service.TestService;
import com.boot.service.entity.ArticleDO;
import com.boot.service.mapper.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service(version = "1.0.0")
public class TestServiceImpl implements TestService {
     


    @Autowired
    private ArticleMapper articleMapper;


    @Override
    public String descArticle() {
     
        List<ArticleDO> all = articleMapper.findAll();
        return JSON.toJSONString(all);
    }

    @Override
    public Article find() {
     
        Article article = new Article();
        article.setAuthor("kbq");
        article.setName("Dubbo");
        article.setPublishDate(new Date());
        article.setScore(100);
        article.setType("program");
        return article;
    }

    @Override
    public void add() {
     
        ArticleDO articleDO = new ArticleDO();
        articleDO.setAuthor("kangbinqiang");
        articleDO.setTitle("Dubbo");
        articleDO.setCreatedTime(new Date());
        articleMapper.insert(articleDO);
    }
}

Controller:

package com.boot.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.boot.model.Article;
import com.boot.service.TestService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class TestController {
     

    @Reference(version = "1.0.0")
    private TestService testService;

    @GetMapping("hello")
    public String hello() {
     
        return testService.descArticle();
    }

    @GetMapping("user")
    public Article user() {
     
        return testService.find();
    }

    @GetMapping("add")
    public void add() {
     
        testService.add();
    }
}

结果如图:

add()

Dubbo+Zookeeper+SpringBoot家族实现多模块分布式项目_第11张图片

hello()

Dubbo+Zookeeper+SpringBoot家族实现多模块分布式项目_第12张图片

你可能感兴趣的:(Dubbo,分布式,maven,spring,java,mongodb)