SpringBoot整合dubbo

[TOC]

1. 前期准备

###1.1 服务器中安装好zookeeper

安装过程看我之前的博客 点击这里

###1.2 服务器中安装dubbo的控制端

  1. 下载dubbo-admin的war包 点击下载

  2. 解压,修改WEB-INF下的dubbo.properties,将dubbo.registry.address修改为你服务器地址,root用户的密码为pibigstar

  3. 启动tomcat

浏览器访问http://你服务器地址:tomcat启动端口号/dubbo-admin 查看是否能进入dubbo控制端
这里写图片描述

2. 构建dubbo服务提供者(先有提供者才能有消费者)

###2.1 项目结构

这里写图片描述

2.2 添加依赖

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!-- dubbo started -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.4.10</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- dubbo end -->

2.3 编写暴露的服务接口

接口

1
2
3
4
5
6
7
package com.pibigstar.dubbo.remote;

public interface TestService {

String sayHello(String name);

}

实现类

1
2
3
4
5
6
7
8
9
10
11
12
package com.pibigstar.dubbo.remote.impl;

import com.pibigstar.dubbo.remote.TestService;

public class TestServiceImpl implements TestService {

@Override
public String sayHello(String name) {
return "Hello " + name + "!";
}

}

2.4 编写配置文件provider.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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="dubbo-provider" owner="dubbo-provider"/>

<!-- 定义 zookeeper 注册中心地址及协议 -->
<dubbo:registry protocol="zookeeper" address="139.199.64.253:2181" client="zkclient"/>

<!-- 定义 Dubbo 协议名称及使用的端口,dubbo 协议缺省端口为 20880,如果配置为 -1 或者没有配置 port,则会分配一个没有被占用的端口 -->
<dubbo:protocol name="dubbo" port="-1"/>

<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.pibigstar.dubbo.remote.TestService" ref="testService" timeout="10000"/>

<!-- 和本地 bean 一样实现服务 -->
<bean id="testService" class="com.pibigstar.dubbo.remote.impl.TestServiceImpl" />
</beans>

2.5 让SpringBoot启动加载配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.pibigstar;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource(value = {"classpath:provider.xml"})
public class DubboProviderApplication {

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

2.6 启动dubbo服务提供者

访问dubbo控制端
这里写图片描述

注意:这里有个大坑!!!!!!!!!!!
SpringBoot必须要有Controller,不然会自动退出
写一个空白的Controller上去。。。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.pibigstar.web;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

@RequestMapping("/say")
public String say() {
return "test";
}

}

3. 构建dubbo服务消费者

3.1 项目结构

这里写图片描述

3.2 添加依赖

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!-- dubbo-consumer Started -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.4.10</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- dubbo-consumer end -->

3.3 编写暴露的服务接口

1
2
3
4
5
6
package com.pibigstar.dubbo.remote;

public interface TestService {

String sayHello(String name);
}

3.4 编写Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

package com.pibigstar.dubbo.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.pibigstar.dubbo.remote.TestService;

@RestController
public class TestController {

@Autowired
TestService testService;

@RequestMapping("/test/{name}")
public JSONObject testJson(@PathVariable("name") String name) {
JSONObject jsonObject = new JSONObject();

String testStr = testService.sayHello(name);
jsonObject.put("str", testStr);
return jsonObject;
}
}

注意:这里接口的包名一定要和你前面服务提供者的包名一样!!

3.5 编写配置文件consumers.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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">

<!-- 配置可参考 http://dubbo.io/User+Guide-zh.htm -->

<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="dubbo-consumer" owner="dubbo-consumer"/>

<!-- 定义 zookeeper 注册中心地址及协议 -->
<dubbo:registry protocol="zookeeper" address="139.199.64.253:2181" client="zkclient" />

<!-- 生成远程服务代理,可以和本地 bean 一样使用 testService(要和提供者名字一样) -->
<dubbo:reference id="testService" interface="com.pibigstar.dubbo.remote.TestService"/>

</beans>

3.6 加载配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.pibigstar;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource(value = {"classpath:consumers.xml"})
public class DubboConsumerApplication {

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

3.7 启动dubbo服务消费者

这里写图片描述

注意:一定要先启动服务提供者,不然无法启动消费者的

4 测试

浏览器访问 http://localhost:8082/test/pibigstar
这里写图片描述
我们看到已经调用了服务提供者的接口实现类

5 项目以所有工具下载

https://pan.baidu.com/s/1fsd1LMiZ6kpQui0zpTiMIg

-------------本文结束感谢您的阅读-------------