dubbo整合springboot最詳細入門教程

ZaELxe.jpg

說明

目前互聯網公司,大部分項目都是基於分佈式,一個項目被拆分紅幾個小項目,這些小項目會分別部署在不一樣的計算機上面,這個叫作微服務。當一臺計算機的程序須要調用另外一臺計算機代碼的時候,就涉及遠程調用。此時dubbo就粉末登場了。java

搭建工程

ZU6JWq.png

ZU6aOU.png

ZU6BTJ.png

idea新建工程後,刪除src文件夾,而後在gradle文件中輸入mysql

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

如圖所示
ZUcM1x.png
boolean做爲父工程,而後再見三個模塊
ZUcsHg.pnggit

booleanone做爲父模塊 booleanteo做爲服務者模塊 booleanthree做爲消費者模塊github

添加dubbo.xml

而後在每一個模塊新建com.test包,在包下新建啓動類
ZUgvJs.pngweb

@SpringBootApplication
public class BaseApplication extends SpringBootServletInitializer {
}

而後在每一個模塊的gradle文件中引入上面的依賴,而後在消費者模塊和生產者模塊的依賴中加入父模塊依賴,如圖spring

ZUg6sK.png

而後在booleantwo的生產者模塊的resource資源文件中加入dubbo文件sql

<?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>

在啓動類中加入註解tomcat

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

而後在booleantwo的消費者模塊的resource資源文件中加入dubbo文件mybatis

<?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>

在啓動類中加入註解app

@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包
ZUXOGq.png
打開zkServer.cmd
ZUjMod.png
啓動信息
ZUXzsU.png
若是啓動有亂碼的話

回到idea軟件 打開tomcat的設置 找到VM options:,而後輸入
-Dfile.encoding=UTF-8

測試

ZUxRi9.png
代碼地址:

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

公衆號

ZaAFFf.png

相關文章
相關標籤/搜索