【SpringBoot】SpringBoot+Zookeeper+Dubbo整合

目录

    • 项目结构
    • 父工程依赖
    • webadmin依赖(consumer)
    • provider依赖
    • service依赖
    • mapper依赖
    • pojo依赖
    • common依赖
    • provider的application.yml配置文件
    • consumer的application.yml配置文件
    • provider的启动类
    • consumer的启动类
    • 一些注意事项

项目结构

【SpringBoot】SpringBoot+Zookeeper+Dubbo整合_第1张图片

父工程依赖


<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.milkygroupId>
    <artifactId>*-zartifactId>
    <packaging>pompackaging>
    <version>1.0-SNAPSHOTversion>
    <modules>
        <module>*-commonmodule>
        <module>*-mappermodule>
        <module>*-pojomodule>
        <module>*-servicemodule>
        <module>*-providermodule>
        <module>*-webadminmodule>
    modules>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.7.2version>
        <relativePath/> 
    parent>

    <properties>
        <maven.compiler.source>17maven.compiler.source>
        <maven.compiler.target>17maven.compiler.target>
        <dubbo.version>3.0.9dubbo.version>
        <zookpeeper.version>3.8.0zookpeeper.version>
        <curator.version>5.3.0curator.version>
    properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
                <version>2.7.3version>
            dependency>

            <dependency>
                
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubbo-spring-boot-starterartifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.apache.zookeepergroupId>
                        <artifactId>zookeeperartifactId>
                    exclusion>
                exclusions>
                <version>${dubbo.version}version>
            dependency>
            
            <dependency>
                <groupId>org.apache.zookeepergroupId>
                <artifactId>zookeeperartifactId>
                <version>${zookpeeper.version}version>
            dependency>
            
            <dependency>
                <groupId>org.apache.curatorgroupId>
                <artifactId>curator-x-discoveryartifactId>
                <version>${curator.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.curatorgroupId>
                <artifactId>curator-recipesartifactId>
                <version>${curator.version}version>
            dependency>
        dependencies>
    dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-loggingartifactId>
                exclusion>
            exclusions>
        dependency>
    dependencies>

project>

webadmin依赖(consumer)


<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>*-zartifactId>
        <groupId>com.milkygroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>*-webadminartifactId>

    <properties>
        <maven.compiler.source>17maven.compiler.source>
        <maven.compiler.target>17maven.compiler.target>
    properties>

    <dependencies>
        <dependency>
            <groupId>com.milkygroupId>
            <artifactId>*-providerartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-spring-boot-starterartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.curatorgroupId>
            <artifactId>curator-x-discoveryartifactId>
        dependency>
    dependencies>

project>

provider依赖


<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>*-zartifactId>
        <groupId>com.milkygroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>*-providerartifactId>

    <properties>
        <maven.compiler.source>17maven.compiler.source>
        <maven.compiler.target>17maven.compiler.target>
    properties>

    <dependencies>
        <dependency>
            <groupId>com.milkygroupId>
            <artifactId>*-serviceartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-spring-boot-starterartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.curatorgroupId>
            <artifactId>curator-x-discoveryartifactId>
        dependency>
    dependencies>

project>

service依赖


<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>*-zartifactId>
        <groupId>com.milkygroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>*-serviceartifactId>

    <properties>
        <maven.compiler.source>17maven.compiler.source>
        <maven.compiler.target>17maven.compiler.target>
    properties>

    <dependencies>
        <dependency>
            <groupId>com.milkygroupId>
            <artifactId>*-mapperartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
    dependencies>

project>

mapper依赖


<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>*-zartifactId>
        <groupId>com.milkygroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>*-mapperartifactId>

    <properties>
        <maven.compiler.source>17maven.compiler.source>
        <maven.compiler.target>17maven.compiler.target>
    properties>

    <dependencies>
        <dependency>
            <groupId>com.milkygroupId>
            <artifactId>*-pojoartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
    dependencies>

project>

pojo依赖


<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>*-zartifactId>
        <groupId>com.milkygroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>*-pojoartifactId>

    <properties>
        <maven.compiler.source>17maven.compiler.source>
        <maven.compiler.target>17maven.compiler.target>
    properties>

    <dependencies>
        <dependency>
            <groupId>com.milkygroupId>
            <artifactId>*-commonartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
    dependencies>

project>

common依赖


<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>*-zartifactId>
        <groupId>com.milkygroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>*-commonartifactId>

    <properties>
        <maven.compiler.source>17maven.compiler.source>
        <maven.compiler.target>17maven.compiler.target>
    properties>

project>

provider的application.yml配置文件

server:
  port: 9000

spring:
  application:
    name: *-provider

dubbo:
  application:
    name: dubbo-*-provider
  protocol:
    name: dubbo
    port: 20880
  registry:
    protocol: zookeeper
    address: 127.0.0.1:2181
    timeout: 20000
    file: ./dubbo-provider-*
  version: 1.0.0
  scan:
    base-packages:
      - com.milky.provider
  enable-file-cache: false

consumer的application.yml配置文件

server:
  port: 8088

spring:
  application:
    name: *-webadmin

dubbo:
  application:
    name: dubbo-consumer-webadmin
    enable-file-cache: false
  registry:
    protocol: zookeeper
    address: 127.0.0.1:2181
    timeout: 60000
    file: ./dubbo-consumer-webadmin
  version: 1.0.0
  scan:
    base-packages:
      - com.milky.webadmin

provider的启动类

package com.milky.provider;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class ProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class,args);
    }
}

consumer的启动类

package com.milky.webadmin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

一些注意事项

启动类需要加@EnableDubbo
服务实现类需要加@DubboService,指定向注册中心暴露该类,消费者可以调用其中的方法
订阅service要用到@DubboReference注入api模块的接口

你可能感兴趣的:(项目模块,dubbo,java-zookeeper,zookeeper,java,springboot)