Dubbo实战案例
在Dubbo中所有的的服务调用都是基于接口去进行双方交互的。双方协定好Dubbo调用中的接口,提供者来提供实现类并且注册到注册中心上。调用方则只需要引入该接口,并且同样注册到相同的注册中心上(消费者)。即可利用注册中心来实现集群感知功能,之后消费者即可对提供者进行调用。
我们所有的项目都是基于Maven去进行创建,这样相互在引用的时候只需要以依赖的形式进行展现就可
以了。
并且这里我们会通过maven的父工程来统一依赖的版本
程序实现分为以下几步骤:
- 建立maven工程 并且 创建API模块: 用于规范双方接口协定
- 提供provider模块,引入API模块,并且对其中的服务进行实现。将其注册到注册中心上,对外来
统一提供服务。 - 提供consumer模块,引入API模块,并且引入与提供者相同的注册中心。再进行服务调用。
开发过程
接口协定
- 定义maven。
<groupId>com.lwl</groupId>
<artifactId>service-api</artifactId>
<version>1.0-SNAPSHOT</version>
- 定义接口,这里为了方便,只是写一个基本的方法。
public interface HelloService {
String sayHello(String name);
}
创建接口提供者
- 引入API模块。
<dependency>
<groupId>com.lwl</groupId>
<artifactId>service-api</artifactId>
<version>${project.version}</version>
</dependency>
- 引入Dubbo相关依赖,这里为了方便,使用注解方式。
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-rpc-dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-remoting-netty4</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-serialization-hessian2</artifactId>
</dependency>
- 编写实现类。注意这里也使用了Dubbo中的 @Service 注解来声明他是一个服务的提供者。
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name){
return "hello:"+name;
}
}
- 编写配置文件,用于配置dubbo。比如这里我就叫 dubbo-provider.properties ,放入到
resources 目录下。
dubbo.application.name=dubbo-demo-annotation-provider
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.application.name: 当前提供者的名称
dubbo.protocol.name: 对外提供的时候使用的协议
dubbo.protocol.port: 该服务对外暴露的端口是什么,在消费者使用时,则会使用这个端口
并且使用指定的协议与提供者建立连接。
- 编写启动的 main 函数。这里面做的比较简单,主要要注意注解方式中的注册中心这里是使用的本机2181端口来作为注册中心。
public class DubboPureMain {
public static void main(String[] args) throws Exception{
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);
context.start();
System.in.read();
}
@Configuration
@EnableDubbo(scanBasePackages = "com.lwl.service.impl")
@PropertySource("classpath:/dubbo-provider.properties")
static class ProviderConfiguration{
@Bean
public RegistryConfig registryConfig(){
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("zookeeper://127.0.0.1:2181?timeout=10000");
//registryConfig.setTimeout(10000);
return registryConfig;
}
}
}
创建消费者
- 引入API模块。
<dependency>
<groupId>com.lagou</groupId>
<artifactId>service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
- 引入Dubbo依赖 ,同服务提供者。
- 编写服务,用于真实的引用dubbo接口并使用。因为这里是示例,所以比较简单一些。这里面
@Reference 中所指向的就是真实的第三方服务接口。
@Component
public class ComsumerComponet {
@Reference
private HelloService helloService;
public String sayHello(String name){
return helloService.sayHello(name);
}
}
- 编写消费者的配置文件。这里比较简单,主要就是指定了当前消费者的名称和注册中心的位置。通
过这个注册中心地址,消费者就会注册到这里并且也可以根据这个注册中心找到真正的提供者列
表
dubbo.application.name=service-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
- 编写启动类,这其中就会当用户在控制台输入了一次换行后,则会发起一次请求。
public class AnnotationConsumerMain {
public static void main(String[] args) throws Exception {
System.out.println("-------------");
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
context.start();
// 获取消费者组件
ComsumerComponet service = context.getBean(ComsumerComponet.class);
while(true){
System.in.read();
String hello = service.sayHello("world");
System.out.println("result:"+hello);
}
}
@Configuration
@PropertySource("classpath:/dubbo-consumer.properties")
@ComponentScan(basePackages = "com.lwl.bean")
@EnableDubbo
static class ConsumerConfiguration{
}
}