Springboot 整合 Dubbo/ZooKeeper 詳解 SOA 案例
一、爲啥整合 Dubbo 實現 SOA二、運行 springboot-dubbo-server 和 springboot-dubbo-client 工程三、springboot-dubbo-server 和 springboot-dubbo-client 工程配置詳解
一、爲啥整合 Dubbo 實現 SOA
二、運行 springboot-dubbo-server 和 springboot-dubbo-client 工程
1
|
tar zxvf
zookeeper-3.4.8. tar .gz |
1
2
|
cd zookeeper-3.3.6 /conf vim
zoo.cfg |
1
2
3
4
|
tickTime=2000 dataDir= /javaee/zookeeper/data dataLogDir= /javaee/zookeeper/log clientPort=2181 |
1
2
|
cd zookeeper-3.3.6 /bin . /zkServer .sh
start |
1
2
|
cd springboot-learning-example mvn
clean install |
1
2
3
4
5
|
... 2017-03-01
16:31:38.473 INFO 9896 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans
for JMX
exposure on startup 2017-03-01
16:31:38.538 INFO 9896 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081 (http) 2017-03-01
16:31:38.547 INFO 9896 --- [ main] org.spring.springboot.ClientApplication : Started ClientApplication
in 6.055
seconds (JVM running for 7.026) City{ id =1,
provinceId=2, cityName= '溫嶺' ,
description= '是我的故鄉' } |
三、springboot-dubbo-server 和 springboot-dubbo-client 工程配置詳解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
├──
pom.xml └──
src
└──
main
├──
java
│
└── org
│
└── spring
│
└── springboot
│
├── ServerApplication.java
│
├── domain
│
│ └── City.java
│
└── dubbo
│
├── CityDubboService.java
│
└── impl
│
└── CityDubboServiceImpl.java
└──
resources
└──
application.properties |
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
|
<?xml
version= "1.0" encoding= "UTF-8" ?> <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>springboot< /groupId > <artifactId>springboot-dubbo-server< /artifactId > <version>0.0.1-SNAPSHOT< /version > <name>springboot-dubbo
服務端:: 整合 Dubbo /ZooKeeper 詳解
SOA 案例< /name > <!--
Spring Boot 啓動父依賴 --> <parent> <groupId>org.springframework.boot< /groupId > <artifactId>spring-boot-starter-parent< /artifactId > <version>1.5.1.RELEASE< /version > < /parent > <properties> <dubbo-spring-boot>1.0.0< /dubbo-spring-boot > < /properties > <dependencies> <!--
Spring Boot Dubbo 依賴 --> <dependency> <groupId>io.dubbo.springboot< /groupId > <artifactId>spring-boot-starter-dubbo< /artifactId > <version>${dubbo-spring-boot}< /version > < /dependency > <!--
Spring Boot Web 依賴 --> <dependency> <groupId>org.springframework.boot< /groupId > <artifactId>spring-boot-starter-web< /artifactId > < /dependency > <!--
Spring Boot Test 依賴 --> <dependency> <groupId>org.springframework.boot< /groupId > <artifactId>spring-boot-starter- test < /artifactId > <scope> test < /scope > < /dependency > <!--
Junit --> <dependency> <groupId>junit< /groupId > <artifactId>junit< /artifactId > <version>4.12< /version > < /dependency > < /dependencies > < /project > |
1
2
3
4
5
6
|
##
Dubbo 服務提供者配置 spring.dubbo.application.name=provider spring.dubbo.registry.address=zookeeper: //127 .0.0.1:2181 spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=20880 spring.dubbo.scan=org.spring.springboot.dubbo |
1
2
3
4
5
6
7
8
|
//
註冊爲 Dubbo 服務 @Service (version
= "1.0.0" ) public class CityDubboServiceImpl
implements CityDubboService
{
public City
findCityByName(String cityName) {
return new City(1L,2L, "溫嶺" , "是我的故鄉" );
} } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
├──
pom.xml └──
src
└──
main
├──
java
│
└── org
│
└── spring
│
└── springboot
│
├── ClientApplication.java
│
├── domain
│
│ └── City.java
│
└── dubbo
│
├── CityDubboConsumerService.java
│
└── CityDubboService.java
└──
resources
└──
application.properties |
1
2
3
4
5
6
7
|
##
避免和 server 工程端口衝突 server.port=8081 ##
Dubbo 服務消費者配置 spring.dubbo.application.name=consumer spring.dubbo.registry.address=zookeeper: //127 .0.0.1:2181 spring.dubbo.scan=org.spring.springboot.dubbo |
1
2
3
4
5
6
7
8
9
10
11
12
|
@Component public class CityDubboConsumerService
{
@Reference (version
= "1.0.0" )
CityDubboService
cityDubboService;
public void printCity()
{
String
cityName= "溫嶺" ;
City
city = cityDubboService.findCityByName(cityName);
System.out.println(city.toString());
} } |
1
2
3
4
5
6
7
8
9
10
11
|
@SpringBootApplication public class ClientApplication
{
public static void main(String[]
args) {
//
程序啓動入口
//
啓動嵌入式的 Tomcat 並初始化 Spring 環境及其各 Spring 組件
ConfigurableApplicationContext
run = SpringApplication.run(ClientApplication. class ,
args);
CityDubboConsumerService
cityService = run.getBean(CityDubboConsumerService. class );
cityService.printCity();
} } |