使用Spring Boot 與Dubbo集成,這里我之前嘗試了使用注解的方式,簡(jiǎn)單的使用注解注冊(cè)服務(wù)其實(shí)是沒(méi)有問(wèn)題的,但是當(dāng)你涉及到使用注解的時(shí)候在服務(wù)里面引用事務(wù),注入其他對(duì)象的時(shí)候,會(huì)有一些問(wèn)題。于是我就果斷放棄了注解了,使用的是XML,這里可能介紹的是Dubbo,但是如果使用Dubbox的話,基本上是兼容的。接下來(lái),將說(shuō)說(shuō)使用XML的方式與Spring Boot在一起開(kāi)發(fā)。
1.創(chuàng)建工程在pom.xml中加入依賴
創(chuàng)建工程名為:
(1)springboot-dubbo-provide
(2)springboot-dubbo-api
(3)springboot-dubbo-consume
springboot-dubbo-api工程主要是放一些service接口,用于提供給消費(fèi)者使用 。springboot-dubbo-provide工程用于提供服務(wù)。 springboot-dubbo-consume工程為消費(fèi)者。在springboot-dubbo-provide工程中打開(kāi)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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
< project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion >4.0.0</ modelVersion > < groupId >com.chengli</ groupId > < artifactId >springboot-dubbo-provide</ artifactId > < version >0.0.1-SNAPSHOT</ version > < packaging >jar</ packaging > < name >springboot-dubbo-provide</ name > < url >http://maven.apache.org</ url > < parent > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-parent</ artifactId > < version >1.4.3.RELEASE</ version > </ parent > < properties > < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > < java.version >1.8</ java.version > < com.alibaba.dubbo.version >2.5.3</ com.alibaba.dubbo.version > < org.apache.zookeeper.version >3.4.6</ org.apache.zookeeper.version > < com.github.sgroschupf.zkclient.version >0.1</ com.github.sgroschupf.zkclient.version > </ properties > < dependencies > < dependency > < groupId >com.chengli</ groupId > < artifactId >springboot-dubbo-api</ artifactId > < version >0.0.1-SNAPSHOT</ version > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter</ artifactId > </ dependency > <!-- dubbo --> < dependency > < groupId >com.alibaba</ groupId > < artifactId >dubbo</ artifactId > < exclusions > < exclusion > < groupId >org.springframework</ groupId > < artifactId >spring</ artifactId > </ exclusion > </ exclusions > < version >${com.alibaba.dubbo.version}</ version > </ dependency > < dependency > < groupId >org.apache.zookeeper</ groupId > < artifactId >zookeeper</ artifactId > < version >${org.apache.zookeeper.version}</ version > </ dependency > < dependency > < groupId >com.github.sgroschupf</ groupId > < artifactId >zkclient</ artifactId > < version >${com.github.sgroschupf.zkclient.version}</ version > </ dependency > </ dependencies > < build > < plugins > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > </ plugin > </ plugins > </ build > </ project > |
打開(kāi)springboot-dubbo-consume工程,在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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
< project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion >4.0.0</ modelVersion > < groupId >com.chengli</ groupId > < artifactId >springboot-dubbo-consume</ artifactId > < version >0.0.1-SNAPSHOT</ version > < packaging >jar</ packaging > < name >springboot-dubbo-consume</ name > < url >http://maven.apache.org</ url > < parent > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-parent</ artifactId > < version >1.4.3.RELEASE</ version > </ parent > < properties > < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > < java.version >1.8</ java.version > < com.alibaba.dubbo.version >2.5.3</ com.alibaba.dubbo.version > < org.apache.zookeeper.version >3.4.6</ org.apache.zookeeper.version > < com.github.sgroschupf.zkclient.version >0.1</ com.github.sgroschupf.zkclient.version > </ properties > < dependencies > < dependency > < groupId >com.chengli</ groupId > < artifactId >springboot-dubbo-api</ artifactId > < version >0.0.1-SNAPSHOT</ version > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > <!-- dubbo --> < dependency > < groupId >com.alibaba</ groupId > < artifactId >dubbo</ artifactId > < exclusions > < exclusion > < groupId >org.springframework</ groupId > < artifactId >spring</ artifactId > </ exclusion > </ exclusions > < version >${com.alibaba.dubbo.version}</ version > </ dependency > < dependency > < groupId >org.apache.zookeeper</ groupId > < artifactId >zookeeper</ artifactId > < version >${org.apache.zookeeper.version}</ version > </ dependency > < dependency > < groupId >com.github.sgroschupf</ groupId > < artifactId >zkclient</ artifactId > < version >${com.github.sgroschupf.zkclient.version}</ version > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-configuration-processor</ artifactId > < optional >true</ optional > </ dependency > </ dependencies > < build > < plugins > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > </ plugin > </ plugins > </ build > </ project > |
2.Dubbo配置
2.1springboot-dubbo-provide服務(wù)提供者
(1)在springboot-dubbo-provide項(xiàng)目中創(chuàng)建入口啟動(dòng)類(lèi)MainConfig,完整代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.chengli.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MainConfig { public static void main(String[] args) { SpringApplication.run(MainConfig. class , args); try { System.in.read(); } catch (Exception e) { e.printStackTrace(); } } } |
(2)創(chuàng)建Dubbo配置類(lèi)
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.chengli.springboot.dubbo; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; import org.springframework.context.annotation.PropertySource; @Configuration @PropertySource ( "classpath:dubbo/dubbo.properties" ) @ImportResource ({ "classpath:dubbo/*.xml" }) public class DubboConfig { } |
(3)創(chuàng)建Dubbo配置文件
在src/main/resources下新建文件夾dubbo,并加入以下配置:
dubbo-provider.xml內(nèi)容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<? 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"> <!-- 提供方應(yīng)用信息,用于計(jì)算依賴關(guān)系 --> < dubbo:application name = "${dubbo.application.name}" /> <!-- 注冊(cè)中心暴露服務(wù)地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <!-- <dubbo:registry protocol="zookeeper" address="10.170.219.98:2181,10.173.55.173:2181" /> --> < dubbo:registry protocol = "${dubbo.registry.protocol}" address = "${dubbo.registry.address}" /> <!-- 暴露服務(wù) --> < dubbo:protocol name = "${dubbo.protocol.name}" port = "${dubbo.protocol.port}" /> < dubbo:service interface = "com.chengli.springboot.example.service.ExampleService" ref = "exampleServiceImpl" retries = "0" timeout = "6000" /> </ beans > |
注意:這里我發(fā)布的example服務(wù)是示例,具體的根據(jù)實(shí)際修改
(4)創(chuàng)建dubbo.properties
1
2
3
4
5
6
7
8
9
10
|
#應(yīng)用名稱 dubbo.application.name=example-provider #注冊(cè)中心類(lèi)型 dubbo.registry.protocol=zookeeper #注冊(cè)中心地址 dubbo.registry.address=127.0.0.1:2181 #暴露服務(wù)方式 dubbo.protocol.name=dubbo #暴露服務(wù)端口 dubbo.protocol.port=20880 |
2.2springboot-dubbo-consume服務(wù)消費(fèi)者
(1)創(chuàng)建入口啟動(dòng)類(lèi)MainConfig
1
2
3
4
5
6
7
8
9
10
11
|
package com.chengli.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MainConfig { public static void main(String[] args) { SpringApplication.run(MainConfig. class , args); } } |
(2)創(chuàng)建Dubbo配置類(lèi)
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.chengli.springboot.dubbo; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; import org.springframework.context.annotation.PropertySource; @Configuration @PropertySource ( "classpath:dubbo/dubbo.properties" ) @ImportResource ({ "classpath:dubbo/*.xml" }) public class DubboConfig { } |
(3)創(chuàng)建Dubbo配置文件
在src/main/resources下新建文件夾dubbo,并加入以下配置:
dubbo-consume.xml內(nèi)容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<? 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"> <!-- 提供方應(yīng)用信息,用于計(jì)算依賴關(guān)系 --> < dubbo:application name = "${dubbo.application.name}" /> <!-- 注冊(cè)中心暴露服務(wù)地址 --> < dubbo:registry protocol = "${dubbo.registry.protocol}" address = "${dubbo.registry.address}" /> < dubbo:reference id = "axempleService" interface = "com.chengli.springboot.example.service.ExampleService" /> </ beans > |
(4)創(chuàng)建dubbo.properties
1
2
3
4
5
6
|
#應(yīng)用名稱 dubbo.application.name=example-consume #注冊(cè)中心類(lèi)型 dubbo.registry.protocol=zookeeper #注冊(cè)中心地址 dubbo.registry.address=127.0.0.1:2181 |
到這里基本上就已經(jīng)可以了,不過(guò)測(cè)試類(lèi)的代碼我就不貼上來(lái)了。只要在API中定義接口實(shí)現(xiàn)即可。使用Spring Boot 與Dubbo集成的時(shí)候,需要注意的是,不要使用Spring Boot提供的devtools熱啟動(dòng),因?yàn)閐evtools提供了兩個(gè)ClassLoader,加載策略問(wèn)題導(dǎo)致出現(xiàn)錯(cuò)誤,無(wú)法啟動(dòng)。如果開(kāi)發(fā)中需要熱加載,那么使用Spring 提供的springloaded。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/cl_andywin/article/details/54318903