Spring Boot方式的Dubbo项目

项目依赖

需要org.apache.dubbo.dubbo-dependencies-bom, 需要org.apache.dubbo.dubbo-spring-boot-starter, 当前版本有2.7.0和2.7.1, dubbo版本已经发布到2.7.2了, 但是starter还没更新到2.7.2, 所以整体使用2.7.1版本

另外就是spring-boot的org.springframework.boot.spring-boot-dependencies, org.springframework.boot.spring-boot-starter-test

org.apache.dubbo.dubbo中带了springframework, javax.servlet, log4j, 在pom中要排除掉

在项目的maven settings.xml中, 只需要配public就可以, 不需要Apache Snapshot. 检查一下public底下有没有org.apache.dubbo, 没有的话, 把Central的proxy源配置到 http://repo1.maven.org/maven2/ 这个是最源头的仓库了, 现在从墙里访问速度也不慢.

Root模块

给项目随便起了个名字叫DB, 这个是root的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.rockbb</groupId>
    <artifactId>db</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <name>DB: Root</name>
    <modules>
        <module>../db-commons-api</module>
        <module>../db-commons-impl</module>
        <module>../db-admin</module>
    </modules>

    <prerequisites>
        <maven>3.3.9</maven>
    </prerequisites>

    <properties>
        <!-- Global encoding -->
        <project.jdk.version>1.8</project.jdk.version>
        <project.source.encoding>UTF-8</project.source.encoding>

        <!-- Global dependency versions -->
        <springframework.boot.version>2.1.6.RELEASE</springframework.boot.version>
        <dubbo.version>2.7.1</dubbo.version>

        <development.mode>1</development.mode>
        <development.developer>milton</development.developer>
        <timestamp>${maven.build.timestamp}</timestamp>
        <maven.build.timestamp.format>yyyyMMdd_HHmm</maven.build.timestamp.format>
        <dubbo.application.version>develop-${development.developer}</dubbo.application.version>
        <log.path>d:/logs</log.path>
        <log.level>INFO</log.level>
        <log.appender>stdout</log.appender>
        <admin.root.path></admin.root.path>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- Spring Boot Dependencies -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${springframework.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- Apache Dubbo Dependencies -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-dependencies-bom</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <version>${springframework.boot.version}</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>${dubbo.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo</artifactId>
                <version>${dubbo.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>javax.servlet</groupId>
                        <artifactId>servlet-api</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>log4j</groupId>
                        <artifactId>log4j</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                    <configuration>
                        <source>${project.jdk.version}</source>
                        <target>${project.jdk.version}</target>
                        <encoding>${project.source.encoding}</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.1.0</version>
                    <configuration>
                        <encoding>${project.source.encoding}</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${springframework.boot.version}</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

root模块用于规定全局的版本号, 以及完整构建

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

    <parent>
        <groupId>com.rockbb</groupId>
        <artifactId>db</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../db/pom.xml</relativePath>
    </parent>
    <artifactId>db-commons-api</artifactId>
    <packaging>jar</packaging>

    <name>DB: Commons API</name>

    <dependencies>
    </dependencies>
</project>

Commons模块

这个是服务端接口具体实现的模块, 需要启动服务, 注册自己到服务注册中心(这里使用的是zookeeper).

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

    <parent>
        <groupId>com.rockbb</groupId>
        <artifactId>db</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../db/pom.xml</relativePath>
    </parent>
    <artifactId>db-commons-impl</artifactId>
    <packaging>jar</packaging>

    <name>DB: Commons Implementation</name>

    <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>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.rockbb</groupId>
            <artifactId>db-commons-api</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>db-commons</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

.因为zookeeper里带的logger和外面的logger冲突并且版本较低, 所以将zookeeper里的logger排除掉了.

.在build模块中, 用maven-deploy-plugin.configruation.skip: true 避免将本模块install到本地. 因为这是最终部署用的末端模块, 不会再有模块依赖于这个模块

application.yml文件

server:
  port: 8760

spring:
  application:
    name: db-commons

dubbo:
  application:
    name: [email protected]@ # Application name is the unique identifier of an application. It is for registry combing the dependencies of applications. Note: Consumer and provider application name should not be the same, and this parameter is not a match condition. As a suggestion, you can name it as your project name.
    qos:
      enable: false # Activate QoS or not
      port: 33333 # The port QoS would bind to
      accept:
        foreign:
          ip: false # Enable remote access or not

  registry:
    protocol: zookeeper # The protocol of the registry center. dubbo, multicast, zookeeper, redis, consul(2.7.1), sofa(2.7.2), etcd(2.7.2), nacos(2.7.2) are available.
    address: 127.0.0.1:2181

  provider:
    delay: -1 # The delay time(ms) for registering services. When set to -1, it indicates that the services will expose to registry after the Spring context is initialized
    timeout: 30000 # The RPC timeout(ms)
    retries: 0 # The retry count for RPC, not including the first invoke. Please set it to 0 if don‘t need to retry.
    version: @[email protected]

  protocol:
    id: dubbo # Bean Id of the protocol, can be referenced in <dubbo:service protocol=""> The default value is equal to the value of name attribute while id is not filled. If name value has already existed, it will add index to it‘s suffix.
    name: dubbo
    accepts: 500 # The maximum connection count of the service provider
    # dubbo协议缺省端口为20880,rmi协议缺省端口为1099,http和hessian协议缺省端口为80;如果配置为-1 或者 没有配置port则会分配一个没有被占用的端口。Dubbo 2.4.0+,分配的端口在协议缺省端口的基础上增长,确保端口段可控。
    port: 10080
    serialization: hessian2 # The default serialization of dubbo protocol is hessian2, rmi protocol is java, http protocol is json

ApplicationBoot.java文件

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

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

DemoServiceImpl文件  

import com.rockbb.db.commons.api.service.DemoService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

@Service
@Component
public class DemoServiceImpl implements DemoService {
    @Override
    public String hello(String name) {
        return "Hello " + name;
    }
}

Admin模块(消费端)

这里用到了web, 展示层使用的是freemarker.

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

    <parent>
        <groupId>com.rockbb</groupId>
        <artifactId>db</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../db/pom.xml</relativePath>
    </parent>
    <artifactId>db-admin</artifactId>
    <packaging>jar</packaging>

    <name>DB: Admin</name>

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

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.rockbb</groupId>
            <artifactId>db-commons-api</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>db-admin</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>
                <excludes>
                    <exclude>*.yml</exclude>
                    <exclude>*.xml</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>*.yml</include>
                    <include>*.xml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

application.yml文件

server:
  port: 8764
  servlet:
    session:
      timeout: 600
    context-path: @[email protected]

spring:
  application:
    name: db-admin

  freemarker:
    cache: true
    settings:
      auto_import: spring.ftl as S
      number_format: ‘#‘
      template_update_delay: 0
      default_encoding: UTF-8
      datetime_format: yyyy-MM-dd HH:mm:ss
      classic_compatible: true
      template_exception_handler: ignore
  mvc:
    view:
      prefix: /templates/
      suffix: .ftl

dubbo:
  application:
    name: [email protected]@ # Application name is the unique identifier of an application. It is for registry combing the dependencies of applications. Note: Consumer and provider application name should not be the same, and this parameter is not a match condition. As a suggestion, you can name it as your project name.
    qos:
      enable: false # Activate QoS or not
      port: 33333 # The port QoS would bind to
      accept:
        foreign:
          ip: false # Enable remote access or not

  registry:
    protocol: zookeeper # The protocol of the registry center. dubbo, multicast, zookeeper, redis, consul(2.7.1), sofa(2.7.2), etcd(2.7.2), nacos(2.7.2) are available.
    address: 127.0.0.1:2181

  consumer:
    version: @[email protected]
    check: false

ApplicationBoot.java

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

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

DemoController.java

import com.rockbb.db.commons.api.service.DemoService;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class DemoController {
    private static Logger logger = LoggerFactory.getLogger(DemoController.class);

    @Reference
    private DemoService demoService;

    @RequestMapping(value = {"/", "/index"})
    public String doIndex(HttpServletRequest request) {
        String name = request.getParameter("name");
        if (name != null) {
            name = demoService.hello(name);
        }
        logger.info("Name: {}", name);
        return "index";
    }
}

原文地址:https://www.cnblogs.com/milton/p/11194076.html

时间: 2024-11-13 08:11:42

Spring Boot方式的Dubbo项目的相关文章

构建 Zookeep + Dubbo + Spring Boot 的分布式调用项目(一)

一.写在前头 在开始构建前,默认你已经能够成功安装并启动 Zookeeper 注册中心,能够成功安装并启动 Dubbo 控制台: 在本例中,我的 Zookeeper 注册中心 IP 地址为 192.168.10.41,端口号为 4183,Dubbo 控制台的访问地址为 http://192.168.10.41:8080: 为了体现分布式调用,本例中创建了以下两个项目,dubbo-consumer 远程调用 dubbo-provider 提供的服务: dubbo-provider        

[转] 使用Spring Boot和Gradle创建项目

Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. 本文主要是记录使用 Spring Boot 和 Gradle 创建项目的过程,其中会包括 Spring Boot 的安装及使用方法,希望通过这篇文章能够快速搭建一个项目. 开发环境 操作系统: mac JDK:1.7.0_60 Gradle:2.2.1 创建项目 你可以通过 Spring I

spring boot 依赖环境和项目结构介绍

1.环境介绍 使用 Spring Boot 开发项目需要有两个基础环境和一个开发工具,这两个环境是指 Java 编译环境和构建工具环境,一个开发工具是指 IDE 开发工具. Spring Boot 2.0 要求 Java 8 作为最低版本,需要在本机安装 JDK 1.8 并进行环境变量配置,同时需要安装构建工具编译 Spring Boot 项目,最后准备个顺手的 IDE 开发工具即可. 1.1.构建工具 构建工具是一个把源代码生成可执行应用程序的自动化工具,Java 领域中主要有三大构建工具:A

spring cloud和spring boot两个完整项目

spring cloud和spring boot两个完整项目 spring cloud 是基于Spring Cloud的云分布式后台管理系统架构,核心技术采用Eureka.Fegin.Ribbon.Zuul.Hystrix.Security.OAth.Mybatis.Ace-cache等主要框架和中间件,UI采用Bootstrap.jquery等前端组件. spring boot项目是使用spring boot + thymeleaf 开发个人博客项目. CSDN下载地址: https://do

Spring Boot + JPA 多模块项目无法注入 JpaRepository 接口

问题描述 Spring Boot + JPA 多模块项目,启动报异常: nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type '***.***.***Dao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency

Spring Boot构建的Web项目如何在服务端校验表单输入

本文首发于个人网站:Spring Boot构建的Web项目如何在服务端校验表单输入 这个例子用于演示在Spring Boot应用中如何验证Web 应用的输入,我们将会建立一个简单的Spring MVC应用,来读取用户输入并使用validation注解来检查,并且当用户输入错误时,应用需要再屏幕上显示错误信息提示用户重新输入. 首先构建Maven项目,该项目的pom文件内容如下: <?xml version="1.0" encoding="UTF-8"?>

微服务中基于Spring Boot的maven分布式项目框架的搭建

项目介绍 这里搭建的是基于 maven 的分布式工程,因为在一个项目中,多个微服务是属于同一个工程,只不过是提供不同的服务而已,再加上 IDEA 是默认一个窗口打开一个项目工程(这点和 eclipse 不同),如果项目大,不用 maven 聚合工程的话,那估计会打开十几个窗口--会崩溃--而且在架构上,也应该使用 maven 分布式工程来搭建微服务架构.这里手把手教大家在 IDEA 中搭建基于 maven 分布式的 Spring Cloud 微服务工程架构. maven分布式工程架构首先来看一下

spring Boot环境下dubbo+zookeeper的一个基础讲解与示例

一,学习背景 1.   前言 对于我们不管工作还是生活中,需要或者想去学习一些东西的时候,大致都考虑几点: a)      我们为什么需要学习这个东西? b)     这个东西是什么? c)      这个东西能为我们做什么? d)     如何去学? 结合以上几点,我们一起学习下Dubbo这个框架! 2.   学习背景 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取

Spring Boot 2 整合 Dubbo 框架 ,实现 RPC 服务远程调用

一.Dubbo框架简介 1.框架依赖 图例说明: 1)图中小方块 Protocol, Cluster, Proxy, Service, Container, Registry, Monitor 代表层或模块,蓝色的表示与业务有交互,绿色的表示只对 Dubbo 内部交互. 2)图中背景方块 Consumer, Provider, Registry, Monitor 代表部署逻辑拓扑节点. 3)图中蓝色虚线为初始化时调用,红色虚线为运行时异步调用,红色实线为运行时同步调用. 4)图中只包含 RPC