资讯专栏INFORMATION COLUMN

dubbo整合springboot最详细入门教程

JinB / 2464人阅读

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

说明

目前互联网公司,大部分项目都是基于分布式,一个项目被拆分成几个小项目,这些小项目会分别部署在不同的计算机上面,这个叫做微服务。当一台计算机的程序需要调用另一台计算机代码的时候,就涉及远程调用。此时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文件




    
    

    
    

    
    

    
    

在启动类中加入注解

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

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













在启动类中加入注解

@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...

公众号

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/75181.html

相关文章

  • 详细,新手都能看懂 !使用SpringBoot+Dubbo 搭建一个简单的分布式服务

    Github 地址:https://github.com/Snailclimb/springboot-integration-examples ,欢迎各位 Star。 目录: 使用 SpringBoot+Dubbo 搭建一个简单分布式服务 实战之前,先来看几个重要的概念 什么是分布式? 什么是 Duboo? Dubbo 架构 什么是 RPC? 为什么要用 Dubbo? 开始实战 1 ...

    chengtao1633 评论0 收藏0
  • 两年了,我写了这些干货!

    摘要:开公众号差不多两年了,有不少原创教程,当原创越来越多时,大家搜索起来就很不方便,因此做了一个索引帮助大家快速找到需要的文章系列处理登录请求前后端分离一使用完美处理权限问题前后端分离二使用完美处理权限问题前后端分离三中密码加盐与中异常统一处理 开公众号差不多两年了,有不少原创教程,当原创越来越多时,大家搜索起来就很不方便,因此做了一个索引帮助大家快速找到需要的文章! Spring Boo...

    huayeluoliuhen 评论0 收藏0
  • 【推荐】新200篇:技术文章整理

    摘要:作为面试官,我是如何甄别应聘者的包装程度语言和等其他语言的对比分析和主从复制的原理详解和持久化的原理是什么面试中经常被问到的持久化与恢复实现故障恢复自动化详解哨兵技术查漏补缺最易错过的技术要点大扫盲意外宕机不难解决,但你真的懂数据恢复吗每秒 作为面试官,我是如何甄别应聘者的包装程度Go语言和Java、python等其他语言的对比分析 Redis和MySQL Redis:主从复制的原理详...

    BicycleWarrior 评论0 收藏0

发表评论

0条评论

JinB

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<