Skip to content

Commit

Permalink
Dubbo的高可用
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyouzhuguli committed Mar 25, 2019
1 parent 727f860 commit b6f95a6
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 0 deletions.
14 changes: 14 additions & 0 deletions 53.Dubbo-High-Availability/common-api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?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">
<parent>
<artifactId>dubbo-boot</artifactId>
<groupId>cc.mrbird</groupId>
<version>1.0</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>common-api</artifactId>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package cc.mrbird.common.api;

public interface HelloService {
String hello(String message);
}
73 changes: 73 additions & 0 deletions 53.Dubbo-High-Availability/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?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>cc.mrbird</groupId>
<artifactId>dubbo-boot</artifactId>
<packaging>pom</packaging>
<version>1.0</version>

<name>dubbo-boot</name>
<description>Spring Boot-Dubbo-ZooKeeper</description>

<modules>
<module>common-api</module>
<module>server-provider</module>
<module>server-consumer</module>
</modules>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<project.version>1.0</project.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>

<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
21 changes: 21 additions & 0 deletions 53.Dubbo-High-Availability/server-consumer/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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">
<parent>
<artifactId>dubbo-boot</artifactId>
<groupId>cc.mrbird</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>server-consumer</artifactId>

<dependencies>
<dependency>
<groupId>cc.mrbird</groupId>
<artifactId>common-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cc.mrbird;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableDubbo
@SpringBootApplication
public class ConsumerApplicaiton {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplicaiton.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cc.mrbird.consumer.controller;

import cc.mrbird.common.api.HelloService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

// @Reference(url = "http://127.0.0.1:8080")
// @Reference(loadbalance = RoundRobinLoadBalance.NAME)
@Reference(timeout = 1000)
private HelloService helloService;

@GetMapping("/hello/{message}")
public String hello(@PathVariable String message) {
return this.helloService.hello(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
server:
port: 8081

dubbo:
application:
# 服务名称,保持唯一
name: server-consumer
# zookeeper地址,用于从中获取注册的服务
registry:
address: zookeeper://127.0.0.1:2181
protocol:
# dubbo协议,固定写法
name: dubbo
monitor:
protocol: registry
27 changes: 27 additions & 0 deletions 53.Dubbo-High-Availability/server-provider/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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">
<parent>
<artifactId>dubbo-boot</artifactId>
<groupId>cc.mrbird</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>server-provider</artifactId>

<dependencies>
<dependency>
<groupId>cc.mrbird</groupId>
<artifactId>common-api</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cc.mrbird;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;

@EnableHystrix
@EnableDubbo
@SpringBootApplication
public class ProviderApplicaiton {
public static void main(String[] args) {
SpringApplication.run(ProviderApplicaiton.class, args);
System.out.println("complete");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cc.mrbird.provider.service;

import cc.mrbird.common.api.HelloService;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Service(
interfaceClass = HelloService.class,
weight = 100,
loadbalance = RoundRobinLoadBalance.NAME)
@Component
public class HelloServiceImpl implements HelloService {

@Override
@HystrixCommand(fallbackMethod = "defaultHello")
public String hello(String message) {
System.out.println("调用 cc.mrbird.provider.service.HelloServiceImpl#hello");
// try {
// TimeUnit.SECONDS.sleep(2);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
String a = null;
a.toString();
return "hello," + message;
}

public String defaultHello(String message) {
return "hello anonymous";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
server:
port: 8080

dubbo:
application:
# 服务名称,保持唯一
name: server-provider
# zookeeper地址,用于向其注册服务
registry:
address: zookeeper://127.0.0.1:2181
#暴露服务方式
protocol:
# dubbo协议,固定写法
name: dubbo
# 暴露服务端口 (默认是20880,不同的服务提供者端口不能重复)
port: 20880
monitor:
protocol: registry

0 comments on commit b6f95a6

Please sign in to comment.