Skip to content

Commit b6f95a6

Browse files
committed
Dubbo的高可用
1 parent 727f860 commit b6f95a6

File tree

11 files changed

+259
-0
lines changed

11 files changed

+259
-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+
}

53.Dubbo-High-Availability/pom.xml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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.boot</groupId>
43+
<artifactId>dubbo-spring-boot-starter</artifactId>
44+
<version>0.2.0</version>
45+
</dependency>
46+
47+
<!-- zookeeper -->
48+
<dependency>
49+
<groupId>org.apache.zookeeper</groupId>
50+
<artifactId>zookeeper</artifactId>
51+
<version>3.4.8</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>com.101tec</groupId>
55+
<artifactId>zkclient</artifactId>
56+
<version>0.10</version>
57+
</dependency>
58+
</dependencies>
59+
60+
<build>
61+
<plugins>
62+
<plugin>
63+
<groupId>org.apache.maven.plugins</groupId>
64+
<artifactId>maven-compiler-plugin</artifactId>
65+
<configuration>
66+
<source>${java.version}</source>
67+
<target>${java.version}</target>
68+
<encoding>${project.build.sourceEncoding}</encoding>
69+
</configuration>
70+
</plugin>
71+
</plugins>
72+
</build>
73+
</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.config.spring.context.annotation.EnableDubbo;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
@EnableDubbo
8+
@SpringBootApplication
9+
public class ConsumerApplicaiton {
10+
public static void main(String[] args) {
11+
SpringApplication.run(ConsumerApplicaiton.class, args);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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(url = "http://127.0.0.1:8080")
13+
// @Reference(loadbalance = RoundRobinLoadBalance.NAME)
14+
@Reference(timeout = 1000)
15+
private HelloService helloService;
16+
17+
@GetMapping("/hello/{message}")
18+
public String hello(@PathVariable String message) {
19+
return this.helloService.hello(message);
20+
}
21+
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
server:
2+
port: 8081
3+
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+
monitor:
15+
protocol: registry
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
21+
<dependency>
22+
<groupId>org.springframework.cloud</groupId>
23+
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
24+
<version>2.0.2.RELEASE</version>
25+
</dependency>
26+
</dependencies>
27+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cc.mrbird;
2+
3+
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
7+
8+
@EnableHystrix
9+
@EnableDubbo
10+
@SpringBootApplication
11+
public class ProviderApplicaiton {
12+
public static void main(String[] args) {
13+
SpringApplication.run(ProviderApplicaiton.class, args);
14+
System.out.println("complete");
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cc.mrbird.provider.service;
2+
3+
import cc.mrbird.common.api.HelloService;
4+
import com.alibaba.dubbo.config.annotation.Service;
5+
import com.alibaba.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance;
6+
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
7+
import org.springframework.stereotype.Component;
8+
9+
import java.util.concurrent.TimeUnit;
10+
11+
@Service(
12+
interfaceClass = HelloService.class,
13+
weight = 100,
14+
loadbalance = RoundRobinLoadBalance.NAME)
15+
@Component
16+
public class HelloServiceImpl implements HelloService {
17+
18+
@Override
19+
@HystrixCommand(fallbackMethod = "defaultHello")
20+
public String hello(String message) {
21+
System.out.println("调用 cc.mrbird.provider.service.HelloServiceImpl#hello");
22+
// try {
23+
// TimeUnit.SECONDS.sleep(2);
24+
// } catch (InterruptedException e) {
25+
// e.printStackTrace();
26+
// }
27+
String a = null;
28+
a.toString();
29+
return "hello," + message;
30+
}
31+
32+
public String defaultHello(String message) {
33+
return "hello anonymous";
34+
}
35+
}

0 commit comments

Comments
 (0)