dubbo+springboot项目的搭建

原因

因为公司的项目一直用的都是分布式项目,而我只知道用,却不知道怎么去搭建,就整的很烦,逐渐暴躁。这两天一直都在找怎么搭建,因为自己的原因,所以一直搭建不成功,来来回回删了五六遍,也重建了很多次。

 推荐

万幸我一直没有放弃,搜了两个文章,结合去搭建,终于成功。

dubbo项目搭建:https://blog.csdn.net/mapboo/article/details/106682815

多模块项目的搭建:https://blog.csdn.net/tangthh123/article/details/106837999

Dubbo配置:https://www.jianshu.com/p/150c11275a9e

 

 搭建

 提示

父层使用springboot框架,子层provider和customer使用springboot框架,api随意。

1、框架创建

dubbo+springboot项目的搭建

 

创建过程中不选择任何依赖。创建完成后删除文件(除.idea、pom.xml、springboot-dubbo-demo.iml外)。 

dubbo+springboot项目的搭建

 

 

然后点击左上角-》新建-》新模块,按照创建springboot的方式创建dubbo-api、dubbo-provider、dubbo-customer三个子模块。

其中api模块,我只用于放置enity、servic,主要用于实体类、service接口;

provider模块,用于放置serviceImpl,主要用于逻辑处理、操作数据库;

customer模块,用于放置controller,主要用于为前端页面提供接口。 按照这样的思路,在创建子模块的时候请自行按照需求添加依赖。

2、父层配置

dubbo+springboot项目的搭建

 

创建完之后的项目框架是这样的,先配置父层pom `

<?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><!--使用springboot项目创建完自带的,参考多个版本创建项目的经验才知道的-->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example</groupId>
    <artifactId>springboot-dubbo-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-dubbo-demo</name>
    <description>springboot-dubbo-demo</description>
    <packaging>pom</packaging><!--父层打包方式为pom,默认为jar-->

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

    <modules><!--添加三个子模块-->
        <module>dubbo-api</module>
        <module>dubbo-provider</module>
        <module>dubbo-customer</module>
    </modules>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope><!--这里是打包时可能会因为test块导致项目编译不了,所以需要排除test块的编译,也可点击maven窗口的类似于闪电-->
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

3、api层配置

api层只提供接口,或者实体类,所以不需要启动类和配置文件,可删除(根据个人意愿,不删也没事)。pom文件基本没什么可以动的,属于模块添加好就有的。

<?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.5.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>dubbo-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-api</name>
    <description>dubbo-api</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
<!--                仅需要被调用,不需要jar包-->
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

dubbo+springboot项目的搭建

 

 

 根据以上借鉴的教程创建service

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

4、provider层

首先先动pom

<?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.5.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example</groupId>
    <artifactId>dubbo-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-provider</name>
    <description>dubbo-provider</description>

    <properties>
        <java.version>1.8</java.version>
<!--        锁定版本,在下方的依赖中统一调用-->
        <zookeeper.version>3.4.13</zookeeper.version>
        <dubbo.version>0.2.0</dubbo.version>
    </properties>

    <dependencies>
<!--        按照provider调用api,customer调用provider的顺序,需要在调用者的pom导入被调用者的依赖,参数在被调用者pom的parent下-->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
<!--            使用以上被锁定的版本-->
            <version>${dubbo.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>${zookeeper.version}</version>
            <exclusions>
<!--                log4j 和 slf4j-log4j12 需要排除,否则会与zookeeper冲突;-->
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
<!--                    指定启动类-->
                    <mainClass>com.example.dubboprovider.DubboProviderApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

properties.yml的配置

server:
  port: 8091
dubbo:
  application:
    name: dubbo-provider
  protocol:
    name: dubbo
    port: 20880
  registry:
    address: zookeeper://127.0.0.1:2181


spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true
    username: root
    password: 123456

在resources下新建一个dubbo-provider.xml文件,用于存放调用的service

<?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://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <!--注册服务到zookeeper-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880"/>
    <dubbo:service ref="helloService" interface="com.example.dubboapi.service.HelloService"/>
</beans>

启动类注明dubbo-provider.xml的位置

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@EnableDubbo
@ImportResource({"classpath:dubbo-provider.xml"})
@SpringBootApplication
public class DubboProviderApplication {

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

}

实现类在调用时需要在@Service注明service的名字,dubbo-provider.xml的ref

import com.example.dubboapi.service.HelloService;
import org.springframework.stereotype.Service;

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

    @Override
    public String sayHello(String name) {
        System.out.println(name);
        return "Hello, " + name;
    }
}

项目接口如下

dubbo+springboot项目的搭建

 

 5、customer模块

先配置pom

<?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.5.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>dubbo-customer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-customer</name>
    <description>dubbo-customer</description>
    <properties>
        <java.version>1.8</java.version>
        <zookeeper.version>3.4.13</zookeeper.version>
        <dubbo.version>0.2.0</dubbo.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>dubbo-provider</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>${zookeeper.version}</version>
            <exclusions>
                <!--                log4j 和 slf4j-log4j12 需要排除,否则会与zookeeper冲突;-->
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                   <mainClass>com.example.dubbocustomer.DubboCustomerApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

properties.yml的配置

server:
  port: 8092
dubbo:
  application:
    name: dubbo-customer
  registry:
    address: zookeeper://127.0.0.1:2181

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true
    username: root
    password: 123456

在resources下新建一个dubbo-customer.xml文件,用于存放调用的service

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/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-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 系统服务 -->
    <dubbo:reference id="helloService" interface="com.example.dubboapi.service.HelloService" check="false" protocol="dubbo"/>

</beans>

启动类注明dubbo-customer.xml的位置

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

@ImportResource({"classpath:dubbo-customer.xml"})
@SpringBootApplication
public class DubboCustomerApplication {

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

}

controller的编写

import com.example.dubboapi.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired 
    private HelloService helloService;

    @GetMapping("/sayHello")
    private String sayHello(@RequestParam String name){

        System.out.println("调用sayHello成功了..." + " name:" + name);

        return helloService.sayHello(name);
    }

}

项目结构如下:

dubbo+springboot项目的搭建

 

 这时,大概就配置完成了,先启动provider,再启动customer(前提是zookeeper是开启的,不然项目无法启动)

地址栏输入http://localhost:8092/sayHello?name=一语惊醒梦中人

dubbo+springboot项目的搭建

 

上一篇:RabbitMQ学习笔记一-- rabbitMq的安装和使用


下一篇:Dubbo架构体系