Apache Dubbo (incubating) |ˈdʌbəʊ| 是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。
Dubbo的官网: www.dubbo.appche.org
节点角色说明:
节点 | 角色说明 |
---|---|
Provider | 暴露服务的服务提供方 |
Consumer | 调用远程服务的服务消费方 |
Registry | 服务注册与发现的注册中心 |
Monitor | 统计服务的调用次数和调用时间的监控中心 |
Container | 服务运行容器 |
不了解Springboot的童鞋可以去官网了解一下
Springboot的官网: http://spring.io/projects/spring-boot/
搭建项目之前先说一下需要项目的结构:
dubbo-test:是总项目
duboo-api:主要是实体类和接口服务
dubbo-consumer:调用远程服务的服务消费方
duboo-provider:暴露服务的服务提供方
我使用的工具是:IDEA
<groupId>com.mydubbogroupId>
<artifactId>dubboartifactId>
<version>v.1.0.0version>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<modules>
<module>dubbo-apimodule>
<module>dubbo-customermodule>
<module>dubbo-providermodule>
modules>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<properties>
<java.version>1.8java.version>
<zookeeper.version>3.4.13zookeeper.version>
<dubbo.starter.version>0.2.0dubbo.starter.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>${dubbo.starter.version}version>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>${zookeeper.version}version>
dependency>
<dependency>
<groupId>com.livedubbogroupId>
<artifactId>dubbo-apiartifactId>
<version>v1.0version>
dependency>
dependencies>
<properties>
<java.version>1.8java.version>
<zookeeper.version>3.4.13zookeeper.version>
<dubbo.starter.version>0.2.0dubbo.starter.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>${dubbo.starter.version}version>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>${zookeeper.version}version>
dependency>
<dependency>
<groupId>com.livedubbogroupId>
<artifactId>dubbo-apiartifactId>
<version>v1.0version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
public interface GreetingService {
String sayHello(String name);
}
server:
port: 9011
spring:
application:
name: dubbo-consumer
dubbo:
application: #应用配置,用于配置当前应用信息,不管该应用是提供者还是消费者。
name: dubbo-consumer
registry: #注册中心配置,用于配置连接注册中心相关信息。
address: zookeeper://172.0.0.1:2181
provider:
timeout: 1000
添加一个GreetingController
GreetingController的代码如下:
@RestController
public class GreetingController {
@Reference
private GreetingService greetingService;
@RequestMapping("/sayHello")
public String sayHello(@RequestParam String name) {
return greetingService.sayHello(name);
}
}
server:
port: 9010
spring:
application:
name: dubbo-provider
dubbo:
application: #应用配置,用于配置当前应用信息,不管该应用是提供者还是消费者。
name: dubbo-provider
protocol: #协议配置,用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受。
name: dubbo
port: 20880
registry: #注册中心配置,用于配置连接注册中心相关信息。
address: zookeeper://127.0.0.1:2181
provider:
timeout: 1000
@Service
public class GreetingServiceImpl implements GreetingService{
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}
注意:
在dubbo-provider启动类加上:注解@EnableDubbo、这样才能启动dubbo调用zookeeper
如下代码:
@EnableDubbo
@SpringBootApplication
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
通过postman进行测试:访问地址:http://127.0.0.1:9011/sayHello?name=“王思聪”