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

作者:布尔bl 时间:2022-07-29 21:19:00 

前言

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

搭建工程

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

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

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

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'
}

如图所示

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

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

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

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

添加dubbo.xml

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

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


@SpringBootApplication
public class BaseApplication extends SpringBootServletInitializer {
}

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

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

然后在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包

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

打开zkServer.cmd

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

启动信息

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

如果启动有乱码的话

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

-Dfile.encoding=UTF-8

测试

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

代码地址:

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

来源:https://www.cnblogs.com/chenzhuantou/p/11135976.html

标签:教程,dubbo,整合,springboot
0
投稿

猜你喜欢

  • IKAnalyzer结合Lucene实现中文分词(示例讲解)

    2022-10-13 03:24:44
  • Java中EnvironmentAware 接口的作用

    2023-04-15 16:11:12
  • Android Flutter实现搜索的三种方式详解

    2023-07-10 18:00:49
  • SpringBoot中Dozer的使用小结

    2023-11-25 01:24:38
  • 关于Feign调用服务Headers传参问题

    2022-10-31 01:48:24
  • Android实现文字滚动播放效果的代码

    2021-07-15 22:35:15
  • Activiti开发环境的配置

    2021-07-31 21:57:51
  • C#处理Access中事务的方法

    2021-07-01 13:43:39
  • Java Email邮件发送简单实现介绍

    2023-10-07 01:05:11
  • C# datatable 不能通过已删除的行访问该行的信息处理方法

    2022-07-31 02:57:08
  • Android WebView开发之自定义WebView工具框

    2023-01-12 13:41:43
  • uploadify java实现多文件上传和预览

    2022-12-25 07:07:16
  • C#中String和StringBuilder的简介与区别

    2021-10-13 06:40:54
  • springmvc与mybatis集成配置实例详解

    2021-06-16 22:10:27
  • 基于Retrofit2+RxJava2实现Android App自动更新

    2021-09-04 20:19:29
  • 布隆过滤器面试如何快速判断元素是否在集合里

    2022-10-17 15:55:19
  • WPF实现调用本机摄像头的示例代码

    2023-03-15 15:40:24
  • Java中关于Map四种取值方式

    2022-04-22 09:24:09
  • jar包运行时提示jar中没有主清单属性的解决

    2023-11-23 19:04:10
  • Android Flutter实战之为照片添加颜色滤镜

    2023-05-17 01:27:12
  • asp之家 软件编程 m.aspxhome.com