1、为啥整合 Dubbo 实现 SOA2、运行 springboot-dubbo-server 和 springboot-dubbo-client 工程3、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=
'是个人故乡'
}
|
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();
}
}
|