Skip to content

Commit a7f1e77

Browse files
committed
java/ee: add dubbo usage
1 parent 55f7cfb commit a7f1e77

File tree

16 files changed

+327
-0
lines changed

16 files changed

+327
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" 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>boot-dubbo</artifactId>
7+
<groupId>cn.edu.ntu</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>dubbo-common</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>junit</groupId>
17+
<artifactId>junit</artifactId>
18+
<version>4.11</version>
19+
<scope>test</scope>
20+
</dependency>
21+
</dependencies>
22+
23+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package cn.edu.ntu.service;
2+
3+
public interface HelloService {
4+
/**
5+
* 问好
6+
*
7+
* @param name 姓名
8+
* @return 问好
9+
*/
10+
String sayHello(String name);
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/** 此 module 主要是用于公共部分, 主要存放工具类, 实体, 以及服务提供方/调用方的接口定义 */
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cn.edu.ntu;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertTrue;
6+
7+
/** Unit test for simple App. */
8+
public class AppTest {
9+
/** Rigorous Test :-) */
10+
@Test
11+
public void shouldAnswerWithTrue() {
12+
assertTrue(true);
13+
}
14+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" 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>boot-dubbo</artifactId>
7+
<groupId>cn.edu.ntu</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>dubbo-consumer</artifactId>
13+
14+
<name>dubbo-consumer</name>
15+
16+
17+
<dependencies>
18+
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-web</artifactId>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>com.alibaba.spring.boot</groupId>
26+
<artifactId>dubbo-spring-boot-starter</artifactId>
27+
<version>${dubbo.starter.version}</version>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>${project.groupId}</groupId>
32+
<artifactId>dubbo-common</artifactId>
33+
<version>${project.version}</version>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>com.101tec</groupId>
38+
<artifactId>zkclient</artifactId>
39+
<version>${zkclient.version}</version>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.projectlombok</groupId>
44+
<artifactId>lombok</artifactId>
45+
<optional>true</optional>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-test</artifactId>
51+
<scope>test</scope>
52+
</dependency>
53+
</dependencies>
54+
55+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cn.edu.ntu;
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+
/** Hello world! */
8+
@EnableDubboConfiguration
9+
@SpringBootApplication
10+
public class ConsumerApplication {
11+
12+
public static void main(String[] args) {
13+
SpringApplication.run(ConsumerApplication.class, args);
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cn.edu.ntu.controller;
2+
3+
import cn.edu.ntu.service.HelloService;
4+
import com.alibaba.dubbo.config.annotation.Reference;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RequestParam;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
@RestController
11+
@Slf4j
12+
public class HelloController {
13+
@Reference private HelloService helloService;
14+
15+
@GetMapping("/sayHello")
16+
public String sayHello(@RequestParam(defaultValue = "xkcoding") String name) {
17+
log.info("i'm ready to call someone......");
18+
return helloService.sayHello(name);
19+
}
20+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
server:
2+
port: 9091
3+
4+
spring:
5+
dubbo:
6+
application:
7+
name: dubbo-consumer
8+
registry: zookeeper://1.15.129.214:2181
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" 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>boot-dubbo</artifactId>
7+
<groupId>cn.edu.ntu</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>dubbo-provider</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-web</artifactId>
18+
</dependency>
19+
20+
<dependency>
21+
<groupId>com.alibaba.spring.boot</groupId>
22+
<artifactId>dubbo-spring-boot-starter</artifactId>
23+
<version>${dubbo.starter.version}</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>${project.groupId}</groupId>
28+
<artifactId>dubbo-common</artifactId>
29+
<version>${project.version}</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>com.101tec</groupId>
34+
<artifactId>zkclient</artifactId>
35+
<version>${zkclient.version}</version>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.projectlombok</groupId>
40+
<artifactId>lombok</artifactId>
41+
<optional>true</optional>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-test</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
</dependencies>
50+
51+
</project>
52+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cn.edu.ntu;
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+
/** Hello world! */
8+
@EnableDubboConfiguration
9+
@SpringBootApplication
10+
public class ProviderApplication {
11+
12+
public static void main(String[] args) {
13+
SpringApplication.run(ProviderApplication.class, args);
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cn.edu.ntu.service;
2+
3+
import com.alibaba.dubbo.config.annotation.Service;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.stereotype.Component;
6+
7+
@Slf4j
8+
@Component
9+
@Service
10+
public class HelloServiceImpl implements HelloService {
11+
/**
12+
* 问好
13+
*
14+
* @param name 姓名
15+
* @return 问好
16+
*/
17+
@Override
18+
public String sayHello(String name) {
19+
log.info("someone is calling me......");
20+
return "say hello to: " + name;
21+
}
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/** 此 module 主要是服务提供方示例 */
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
server:
2+
port: 9090
3+
4+
spring:
5+
dubbo:
6+
application:
7+
name: dubbo-provider
8+
registry: zookeeper://1.15.129.214:2181
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
3+
<parent>
4+
<artifactId>integration</artifactId>
5+
<groupId>cn.edu.ntu</groupId>
6+
<version>1.0-SNAPSHOT</version>
7+
</parent>
8+
<modelVersion>4.0.0</modelVersion>
9+
<packaging>pom</packaging>
10+
11+
12+
<artifactId>boot-dubbo</artifactId>
13+
14+
<modules>
15+
<module>dubbo-common</module>
16+
<module>dubbo-provider</module>
17+
<module>dubbo-consumer</module>
18+
</modules>
19+
20+
21+
<properties>
22+
<zkclient.version>0.10</zkclient.version>
23+
<dubbo.starter.version>2.0.0</dubbo.starter.version>
24+
</properties>
25+
26+
</project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## 注册中心
2+
3+
1. zookeeper:docker
4+
5+
```yml
6+
version: '3.0'
7+
services:
8+
zookepper:
9+
image: wurstmeister/zookeeper
10+
restart: on-failure
11+
container_name: dev-zookeeper
12+
#domainname:
13+
ports:
14+
- 2181:2181
15+
volumes:
16+
- /root/zookeeper/data:/data
17+
- /root/zookeeper/datalog:/datalog
18+
```
19+
20+
## layout
21+
22+
1. dubbo-common: 公共模块
23+
2. dubbo-provider: 服务提供方
24+
3. dubbo-consumer: 服务调用方
25+
26+
## dubbo
27+
28+
![avatar](https://camo.githubusercontent.com/e11a2ff9575abc290657ba3fdbff5d36f1594e7add67a72e0eda32e449508eef/68747470733a2f2f647562626f2e6170616368652e6f72672f696d67732f6172636869746563747572652e706e67) 0. intros - 底层推荐默认使用 dubbo 协议: [link](https://blog.csdn.net/hjy930226173/article/details/125770252)
29+
30+
1. flow
31+
32+
- 服务容器负责启动, 加载, 运行服务提供者
33+
- 服务提供者在启动时, 向注册中心注册自己提供的服务
34+
- 服务消费者在启动时, 向注册中心订阅自己所需的服务
35+
- 注册中心返回服务提供者地址列表给消费者
36+
1. 如果有变更, 注册中心将基于长连接推送变更数据给消费者
37+
- 服务消费者, 从提供者地址列表中基于软负载均衡算法, 选一台提供者进行调用
38+
1. 如果调用失败, 再选另一台调用
39+
- 服务消费者和提供者, 在内存中累计调用次数和调用时间, 定时每分钟发送一次统计数据到监控中心
40+
41+
2. RPC(Remote Procedure Call): 远程过程调用
42+
43+
- 是一种`通过网络`从远程计算机程序上请求服务, 而不需要了解底层网络技术的协议
44+
- 解决部署在两台机器上的相互调用问题: Http 也可但是性能不好
45+
46+
3. feature
47+
- 负载均衡: 同一个服务部署在不同的机器时该调用那一台机器上的服务
48+
- 服务调用链路生成: 服务之间互相是如何调用的
49+
- 服务访问压力以及时长统计: 当前系统的压力主要在哪里, 如何来扩容和优化
50+
- 服务降级: 某个服务挂掉之后调用备用服务
51+
52+
## reference
53+
54+
1. http://dubbo.apache.org/zh-cn/
55+
2. https://segmentfault.com/a/1190000017178722

java/javaee/spring-boot/integration/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<module>boot-grpc</module>
2828
<module>boot-event</module>
2929
<module>boot-advance</module>
30+
<module>boot-dubbo</module>
3031
</modules>
3132

3233
<properties>

0 commit comments

Comments
 (0)