Skip to content

Commit d0185e6

Browse files
committed
Spring Boot整合Dubbo&Zookeeper
1 parent 9e4267f commit d0185e6

File tree

11 files changed

+227
-0
lines changed

11 files changed

+227
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>dubbo-boot</artifactId>
7+
<groupId>cc.mrbird</groupId>
8+
<version>1.0</version>
9+
</parent>
10+
11+
<modelVersion>4.0.0</modelVersion>
12+
<artifactId>common-api</artifactId>
13+
14+
</project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package cc.mrbird.common.api;
2+
3+
public interface HelloService {
4+
String hello(String message);
5+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>cc.mrbird</groupId>
8+
<artifactId>dubbo-boot</artifactId>
9+
<packaging>pom</packaging>
10+
<version>1.0</version>
11+
12+
<name>dubbo-boot</name>
13+
<description>Spring Boot-Dubbo-ZooKeeper</description>
14+
15+
<modules>
16+
<module>common-api</module>
17+
<module>server-provider</module>
18+
<module>server-consumer</module>
19+
</modules>
20+
21+
<parent>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-parent</artifactId>
24+
<version>2.0.4.RELEASE</version>
25+
<relativePath/>
26+
</parent>
27+
28+
<properties>
29+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
30+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
31+
<java.version>1.8</java.version>
32+
<project.version>1.0</project.version>
33+
</properties>
34+
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</artifactId>
39+
</dependency>
40+
<!-- dubbo -->
41+
<dependency>
42+
<groupId>com.alibaba.spring.boot</groupId>
43+
<artifactId>dubbo-spring-boot-starter</artifactId>
44+
<version>2.0.0</version>
45+
</dependency>
46+
<!-- zookeeper -->
47+
<dependency>
48+
<groupId>org.apache.zookeeper</groupId>
49+
<artifactId>zookeeper</artifactId>
50+
<version>3.4.8</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.101tec</groupId>
54+
<artifactId>zkclient</artifactId>
55+
<version>0.10</version>
56+
</dependency>
57+
</dependencies>
58+
59+
<build>
60+
<plugins>
61+
<plugin>
62+
<groupId>org.apache.maven.plugins</groupId>
63+
<artifactId>maven-compiler-plugin</artifactId>
64+
<configuration>
65+
<source>${java.version}</source>
66+
<target>${java.version}</target>
67+
<encoding>${project.build.sourceEncoding}</encoding>
68+
</configuration>
69+
</plugin>
70+
</plugins>
71+
</build>
72+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>dubbo-boot</artifactId>
7+
<groupId>cc.mrbird</groupId>
8+
<version>1.0</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>server-consumer</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>cc.mrbird</groupId>
17+
<artifactId>common-api</artifactId>
18+
<version>${project.version}</version>
19+
</dependency>
20+
</dependencies>
21+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cc.mrbird;
2+
3+
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
@SpringBootApplication
8+
@EnableDubboConfiguration
9+
public class Applicaiton {
10+
public static void main(String[] args) {
11+
SpringApplication.run(Applicaiton.class, args);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cc.mrbird.consumer.controller;
2+
3+
import cc.mrbird.common.api.HelloService;
4+
import com.alibaba.dubbo.config.annotation.Reference;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.PathVariable;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@RestController
10+
public class HelloController {
11+
12+
@Reference
13+
private HelloService helloService;
14+
15+
@GetMapping("/hello/{message}")
16+
public String hello(@PathVariable String message) {
17+
return this.helloService.hello(message);
18+
}
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
server:
2+
port: 8081
3+
spring:
4+
dubbo:
5+
application:
6+
# 服务名称,保持唯一
7+
name: server-consumer
8+
# zookeeper地址,用于向其注册服务
9+
registry:
10+
address: zookeeper://127.0.0.1:2181
11+
protocol:
12+
# dubbo协议,固定写法
13+
name: dubbo
14+
# 扫描需要调用服务的类路径
15+
scan: cc.mrbird.consumer.controller
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>dubbo-boot</artifactId>
7+
<groupId>cc.mrbird</groupId>
8+
<version>1.0</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>server-provider</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>cc.mrbird</groupId>
17+
<artifactId>common-api</artifactId>
18+
<version>${project.version}</version>
19+
</dependency>
20+
</dependencies>
21+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cc.mrbird;
2+
3+
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
@SpringBootApplication
8+
@EnableDubboConfiguration
9+
public class Applicaiton {
10+
public static void main(String[] args) {
11+
SpringApplication.run(Applicaiton.class, args);
12+
System.out.println("complete");
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cc.mrbird.provider.service;
2+
3+
import cc.mrbird.common.api.HelloService;
4+
import com.alibaba.dubbo.config.annotation.Service;
5+
import org.springframework.stereotype.Component;
6+
7+
@Service(interfaceClass = HelloService.class)
8+
@Component
9+
public class HelloServiceImpl implements HelloService {
10+
@Override
11+
public String hello(String message) {
12+
return "hello," + message;
13+
}
14+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
server:
2+
port: 8080
3+
spring:
4+
dubbo:
5+
application:
6+
# 服务名称,保持唯一
7+
name: server-provider
8+
# zookeeper地址,用于向其注册服务
9+
registry:
10+
address: zookeeper://127.0.0.1:2181
11+
#暴露服务方式
12+
protocol:
13+
# dubbo协议,固定写法
14+
name: dubbo
15+
# 暴露服务端口 (默认是20880,不同的服务提供者端口不能重复)
16+
port: 20880
17+
server: true
18+
# 扫描需要暴露服务的类路径
19+
scan: cc.mrbird.provider.service

0 commit comments

Comments
 (0)