本文介紹了springboot整合dubbo之代碼集成和發(fā)布,分享給大家,具體如下:
1. boot-dubbo-api相關(guān)
打開boot-dubbo-api項(xiàng)目,正在src/main/java下創(chuàng)建一個(gè)包,并創(chuàng)建你需要dubbo暴露的接口testservice.java,并創(chuàng)建一個(gè)實(shí)體類用于測試user.java。如下圖所示:
創(chuàng)建文件和包結(jié)構(gòu)
user.java
1
2
3
4
5
6
7
8
9
10
11
|
package com.boot.domain; import lombok.data; import java.io.serializable; @data public class user implements serializable { private integer id; private string username; private string password; private integer age; private integer gender; } |
testservice.java
1
2
3
4
5
6
|
package com.boot.service; import com.boot.domain.user; public interface testservice { string sayhello(string str); user finduser(); } |
2. boot-dubbo-provider相關(guān)
首先我們先看看總共需要編寫的內(nèi)容,文件的層次結(jié)構(gòu)圖
boot-dubbo-provider項(xiàng)目結(jié)構(gòu)圖
第一步:我們首先實(shí)現(xiàn)我們在boot-dubbo-api上定義的接口,創(chuàng)建一個(gè)testserviceimpl類并實(shí)現(xiàn)testservice
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.boot.service.impl; import com.alibaba.dubbo.config.annotation.service; import com.boot.domain.user; import com.boot.service.testservice; import java.text.simpledateformat; import java.util.date; @service (version = "1.0.0" ) public class testserviceimpl implements testservice { @override public string sayhello(string str) { simpledateformat dateformat = new simpledateformat( "yyyy-mm-dd hh:mm:ss" ); return dateformat.format( new date()) + ": " + str; } @override public user finduser() { user user = new user(); user.setid( 1001 ); user.setusername( "scott" ); user.setpassword( "tiger" ); user.setage( 20 ); user.setgender( 0 ); return user; } } |
注意:代碼里的@service注解是com.alibaba.dubbo.config.annotation.service的。
第二步:在resources下創(chuàng)建一個(gè)config文件夾,在config下創(chuàng)建spring-dubbo.xml配置文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?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= "provider" /> <!-- 注冊中心的ip地址 --> <dubbo:registry address= "zookeeper://127.0.0.1:2181" /> <!-- 掃描注解包路徑,多個(gè)包用逗號分隔,不填pacakge表示掃描當(dāng)前applicationcontext中所有的類 --> <dubbo:annotation package = "com.boot.service.impl" /> </beans> |
第三步:在com.boot包下新建springboot的入口類,創(chuàng)建一個(gè)providerapplication.java文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.boot; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.context.annotation.importresource; import java.io.ioexception; @springbootapplication @importresource ({ "classpath:config/spring-dubbo.xml" }) public class providerapplication { public static void main(string[] args) { springapplication.run(providerapplication. class , args); try { system.in.read(); } catch (ioexception e) { e.printstacktrace(); } } } |
第四步:最后在resources文件夾下面創(chuàng)建application.yml,springboot的配置文件。
1
|
# 在這里編寫springboot的配置信息 |
3. boot-dubbo-consumer相關(guān)
首先我們先看看總共需要編寫的內(nèi)容,文件的層次結(jié)構(gòu)圖
boot-dubbo-consumer項(xiàng)目結(jié)構(gòu)圖
第一步:編寫我們的controller控制類,在com.boot.controller包下新建一個(gè)testcontroller類,編寫訪問地址。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.boot.controller; import com.alibaba.dubbo.config.annotation.reference; import com.boot.domain.user; import com.boot.service.testservice; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @restcontroller @requestmapping ( "/" ) public class testcontroller { @reference (version = "1.0.0" ) private testservice testservice; @getmapping ( "hello" ) public string hello() { return testservice.sayhello( "hello springboot and dubbo!" ); } @getmapping ( "user" ) public user user() { return testservice.finduser(); } } |
第二步:在resources下創(chuàng)建一個(gè)config文件夾,在config下創(chuàng)建spring-dubbo.xml配置文件。
1
2
3
4
5
6
7
8
9
10
11
12
|
<?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= "consumer" /> <dubbo:registry address= "zookeeper://127.0.0.1:2181" /> <dubbo:annotation package = "com.boot.controller" /> </beans> |
第三步:在com.boot包下新建springboot的入口類,創(chuàng)建一個(gè)consumerapplication.java文件。
1
2
3
4
5
6
7
8
9
10
11
|
package com.boot; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.context.annotation.importresource; @springbootapplication @importresource ({ "classpath:config/spring-dubbo.xml" }) public class consumerapplication { public static void main(string[] args) { springapplication.run(consumerapplication. class , args); } } |
第四步:最后在resources文件夾下面創(chuàng)建application.yml,springboot的配置文件。
1
2
3
4
|
# 在這里編寫springboot的配置信息 server: port: 8080 context-path: / |
至此,代碼部分已經(jīng)編寫完畢!!!
4. 安裝zookeeper注冊中心到電腦中
下載地址 zookeeper下載地址
點(diǎn)擊后下載適合自己的版本,如圖所示
zookeeper的下載
下載完畢后,解壓縮該文件,進(jìn)入conf文件夾,拷貝一份zoo_sample.cfg,在該目錄生成zoo.cfg文件。
拷貝生成zoo.cfg文件
進(jìn)入bin目錄,運(yùn)行zkserver.cmd文件。
運(yùn)行zkserver.cmd
點(diǎn)擊運(yùn)行后出現(xiàn)如下圖所示
運(yùn)行成功
5. 現(xiàn)在終于可以運(yùn)行我們的項(xiàng)目了
先運(yùn)行我們的providerapplication.java文件的main函數(shù),再運(yùn)行consumerapplication.java文件的main函數(shù)。
打開瀏覽器訪問
http://localhost:8080/hello
http://localhost:8080/user
好了,激動(dòng)人心的時(shí)刻到了,我們終于完成了springboot和dubbo的集成了。
項(xiàng)目地址:springboot-dubbo項(xiàng)目github地址
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.jianshu.com/p/0837b48d1691