dubbo整合springboot新手入门教程详解

 更新时间:2019年07月05日 08:28:27   作者:布尔bl  
这篇文章主要介绍了dubbo整合springboot新手入门详解,当一台计算机的程序需要调用另一台计算机代码的时候,就涉及远程调用。此时dubbo就粉末登场了,需要的朋友可以参考下

前言

目前互联网公司,大部分项目都是基于分布式,一个项目被拆分成几个小项目,这些小项目会分别部署在不同的计算机上面,这个叫做微服务。当一台计算机的程序需要调用另一台计算机代码的时候,就涉及远程调用。此时dubbo就粉末登场了。

搭建工程

idea新建工程后,删除src文件夹,然后在gradle文件中输入

buildscript {
  repositories {
    maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
    mavenCentral()
  }
  dependencies {
    classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.5.21.RELEASE'
  }
}


plugins {
  id 'java'
}
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
group 'com.demoMuty'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
  maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
  mavenCentral()
}

dependencies {
  compile 'org.springframework.boot:spring-boot-starter-mail'
  compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
  compile 'org.springframework.boot:spring-boot-starter-web'
  compile 'org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.4'
  compile 'com.alibaba.boot:dubbo-spring-boot-starter:0.1.0'
  compile 'com.101tec:zkclient:0.10'
// developmentOnly 'org.springframework.boot:spring-boot-devtools'
  runtime 'mysql:mysql-connector-java'
  compile("com.baomidou:mybatis-plus-boot-starter:3.1.0")
  compile("com.baomidou:mybatis-plus-generator:3.1.1")
  compileOnly 'org.projectlombok:lombok'
  testCompile 'org.springframework.boot:spring-boot-starter-test'
}

如图所示

boolean作为父工程,然后再见三个模块

booleanone作为父模块 booleanteo作为服务者模块 booleanthree作为消费者模块

添加dubbo.xml

然后在每个模块新建com.test包,在包下新建启动类

@SpringBootApplication
public class BaseApplication extends SpringBootServletInitializer {
}

然后在每个模块的gradle文件中引入上面的依赖,然后在消费者模块和生产者模块的依赖中加入父模块依赖,如图

然后在booleantwo的生产者模块的resource资源文件中加入dubbo文件

<?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://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="hello-world-app"/>

  <!-- 使用multicast广播注册中心暴露服务地址 -->
  <dubbo:registry address="zookeeper://localhost:2181"/>

  <!-- 用dubbo协议在20880端口暴露服务 -->
  <dubbo:protocol name="dubbo" port="20880"/>

  <!-- 声明需要暴露的服务接口 -->
  <dubbo:service
      interface="com.test1.provider.DemoService"
      ref="demoService"
      group="hello-world-app"
      version="1.0.0"
  />
</beans>

在启动类中加入注解

@ImportResource({"classpath:dubbo.xml"})

然后在booleantwo的消费者模块的resource资源文件中加入dubbo文件

<?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://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="hello-world-app"/>

<!-- 使用multicast广播注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://localhost:2181"/>

<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference
    interface="com.test1.provider.DemoService"
    group="hello-world-app"
    version="1.0.0"
    id="demoService"/>
</beans>

在启动类中加入注解

@ImportResource({"classpath:dubbo.xml"})

编写dubbo代码

在父模块中写dubbo接口

package com.test1.provider;
/**
 * @author buer
 * create 2019/7/2 22:13
 * description
 */
public interface DemoService {
  String sayHello(String name);
}

然后在生产者模块中写dubbo实现类

package com.test1.dubbo;

import com.test1.provider.DemoService;
import org.springframework.stereotype.Service;

/**
 * @author buer
 * create 2019/7/2 22:14
 * description
 */
@Service("demoService")
public class DemoServiceImpl implements DemoService {
  @Override
  public String sayHello(String name) {
    return "hello,dubbo"+name;
  }
}

然后在消费者模块中写dubbo调用

package com.test1.controller;

import com.test1.provider.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author boolean
 * Date: 2019/7/2 19:48
 * description:
 */
@RestController
public class he {
  @Autowired
  private DemoService demoService;

  @RequestMapping("/he")
  public String hello(){
    return "he";
  }

  @RequestMapping("/chen")
  public String hello1(){
    return demoService.sayHello("chen");
  }
}

启动

最后添加war包

打开zkServer.cmd


启动信息


如果启动有乱码的话

回到idea软件 打开tomcat的设置 找到VM options:,然后输入

-Dfile.encoding=UTF-8

测试


代码地址:

https://github.com/blackdogss/HelloWorld.git

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • easycode配置成mybatis-plus模板的实现方法

    easycode配置成mybatis-plus模板的实现方法

    本文主要介绍了easycode配置成mybatis-plus模板的实现方法,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • 简单介绍java中equals以及==的用法

    简单介绍java中equals以及==的用法

    这篇文章主要介绍了简单介绍java中equals以及==的用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • Spring中@Scope的几种取值方式

    Spring中@Scope的几种取值方式

    这篇文章主要介绍了Spring中@Scope的几种取值方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • java 学习笔记(入门篇)_多选择结构switch语句

    java 学习笔记(入门篇)_多选择结构switch语句

    在java中为多路分支选择流程专门提供了switch语句,switch语句根据一个表达式的值,选择运行多个操作中的一个,感兴趣的朋友可以了解下
    2013-01-01
  • Java多线程的其他知识_动力节点Java学院整理

    Java多线程的其他知识_动力节点Java学院整理

    这篇文章主要介绍了Java多线程的其他知识,需要的朋友可以参考下
    2017-05-05
  • Java中消息队列任务的平滑关闭详解

    Java中消息队列任务的平滑关闭详解

    对于消息队列的监听,我们一般使用Java写一个独立的程序,在Linux服务器上运行。程序启动后,通过消息队列客户端接收消息,放入一个线程池进行异步处理,并发的快速处理。这篇文章主要给大家介绍了关于Java中消息队列任务的平滑关闭的相关资料,需要的朋友可以参考下。
    2017-11-11
  • SpringBoot中配置双数据源的实现示例

    SpringBoot中配置双数据源的实现示例

    在许多应用程序中,可能会遇到需要连接多个数据库的情况,本文主要介绍了SpringBoot中配置双数据源的实现示例,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-08-08
  • Spring Boot Maven插件使用详解

    Spring Boot Maven插件使用详解

    这篇文章主要为大家详细介绍了Spring Boot Maven插件使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • java编程SpringSecurity入门原理及应用简介

    java编程SpringSecurity入门原理及应用简介

    Spring 是非常流行和成功的 Java 应用开发框架,Spring Security 正是 Spring 家族中的成员。Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性的完整解决方案
    2021-09-09
  • 完美解决MybatisPlus插件分页查询不起作用总是查询全部数据问题

    完美解决MybatisPlus插件分页查询不起作用总是查询全部数据问题

    这篇文章主要介绍了解决MybatisPlus插件分页查询不起作用总是查询全部数据问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08

最新评论