springboot2.0整合dubbo、zookeeper
ben 2018-08-07
dubbo
Dubbo建议使用Zookeeper作为服务的注册中心,所以在使用dubbo之前,必须先启动zookeeper的服务。
# 1.zookeeper
docker pull zookeeper
docker run --name zookeeper -p 2181:2181 -v /data/zookeeper:/data -d zookeeper
# 2.dubbo
github提供了整合方案,本文也是参考github上的,可以参考原文
incubator-dubbo-spring-boot-project
# 引入依赖
compile 'com.alibaba.boot:dubbo-spring-boot-starter:0.2.0'
# 服务提供者provider:
DemoService
public interface DemoService {
String sayHello(String name);
}
1
2
3
4
5
2
3
4
5
DemoServiceImpl
import com.alibaba.dubbo.config.annotation.Service;
@Service(version = "${demo.service.version}",
application = "${dubbo.application.id}",
protocol = "${dubbo.protocol.id}",
registry = "${dubbo.registry.id}")
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello, " + name + " (from Spring Boot)";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
application.yml
# Spring boot application
spring:
profiles:
active: local
application:
name: dubbo-provider-demo
# 对外service的版本号
demo:
service:
version: 1.0.0
# 对外提供的service包,配置供dubbo扫描
dubbo:
scan:
basePackages: com.jdkhome.attacking.core.service
# Dubbo Config properties
## dubbo服务应用名
application:
id: dubbo-provider-id #@service.application
name: dubbo-provider-name
qos:
port: 22222
enable: true
## 对外service的协议配置
protocol:
id: dubbo #@service.protocol
name: dubbo
port: 12345
status: server
## RegistryConfig Bean
registry:
id: my-registry #@service.registry
address: zookeeper://127.0.0.1:2181
# Enables Dubbo All Endpoints
management:
endpoint:
dubbo:
enabled: true
dubbo-shutdown:
enabled: true
dubbo-configs:
enabled: true
dubbo-services:
enabled: true
dubbo-references:
enabled: true
dubbo-properties:
enabled: true
# Dubbo Health
## StatusChecker Name defaults (default : "memory", "load" )
health:
dubbo:
status:
defaults: memory
## StatusChecker Name extras (default : empty )
extras: load,threadpool
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
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
# 服务消费者consumer:
DemoService
public interface DemoService {
String sayHello(String name);
}
1
2
3
4
5
2
3
4
5
DemoConsumerController
import com.alibaba.dubbo.config.annotation.Reference;
import com.jdkhome.attacking.core.service.DemoService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoConsumerController {
@Reference(version = "${demo.service.version}",
application = "${dubbo.application.id}",
url = "dubbo://127.0.0.1:12345")
private DemoService demoService;
@RequestMapping("/sayHello/{name}")
public String sayHello(@PathVariable("name") String name) {
return demoService.sayHello(name);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
application.xml
# Spring boot application
spring:
profiles:
active: local
application:
name: dubbo-consumer-demo
# DemoService service version
demo:
service:
version: 1.0.0
# Dubbo Config properties
## ApplicationConfig Bean
dubbo:
application:
id: dubbo-consumer-demo
name: dubbo-consumer-demo
## Legacy QOS Config
qos:
port: 22223
## ProtocolConfig Bean
protocol:
id: dubbo
name: dubbo
port: 12345
# Dubbo Endpoint (default status is disable)
endpoints:
dubbo:
enabled: true
# Dubbo Health
## StatusChecker Name defaults (default : "memory", "load" )
management:
server:
port: 8081
health:
dubbo:
status:
defaults: memory
# Enables Dubbo All Endpoints
endpoint:
dubbo:
enabled: true
dubbo-shutdown:
enabled: true
dubbo-configs:
enabled: true
dubbo-services:
enabled: true
dubbo-references:
enabled: true
dubbo-properties:
enabled: true
# Exposes all web endpoints
web:
exposure:
include: "*"
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
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
# 3.dubbo-admin
dubbo官方还提供了一个管理工具:dubbo-admin
# 安装方法:
docker pull changwentao/dubboadmin
docker run --name dubboadmin -d -p 8080:8080 --link zookeeper:zookeeper changwentao/dubboadmin
# 管理后台:
localhost:8080
root/root