Dubbo入门示例注解版

有关dubbo的基础、架构等介绍请参考之前博客:Dubbo背景及架构简介
XML版(官方推荐)示例请参考:Dubbo入门示例XML版(官方推荐)

1. 创建暴露服务模块(dubbo-demo-api)

本模块下没有实际的业务逻辑,主要是定义提供者和消费者公用服务接口

/**
 * 需要暴露出去的服务
 *
 */
public interface HelloService {
	/**
	 * say hello
	 * 
	 * @param name
	 * @return
	 */
	String sayHello(String name);

}

2. 创建服务提供者和消费者父模块管理和引入公用依赖

依赖版本管理在父工程(dubbo-demo),采用dubbo-2.7.5

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.qqxhb</groupId>
		<artifactId>dubbo-demo</artifactId>
		<version>0.0.1</version>
	</parent>
	<artifactId>dubbo-annotation-demo</artifactId>
	<packaging>pom</packaging>
	<name>dubbo-annotation-demo</name>

	<dependencies>
		<!-- 引入Dubbo依赖 -->
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo</artifactId>
		</dependency>
		<!-- 使用zookeeper作为注册中心 -->
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
		</dependency>
		<!-- 使用curator作为zookeeper客户端 -->
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-recipes</artifactId>
		</dependency>
		<!-- 引入需要暴露的服务 -->
		<dependency>
			<groupId>com.qqxhb</groupId>
			<artifactId>dubbo-demo-api</artifactId>
			<version>0.0.1</version>
		</dependency>
	</dependencies>
	<modules>
		<module>dubbo-annotation-demo-consumer</module>
		<module>ddubbo-annotation-demo-provider</module>
	</modules>
</project>


3. 创建服务提供者

1).创建dubbo-annotation-demo-provider模块
2).编写服务实现类、配置类和启动类
3).编写配置文件

1).创建dubbo-annotation-demo-provider模块

<?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>
		<groupId>com.qqxhb</groupId>
        <artifactId>dubbo-annotation-demo</artifactId>
        <version>0.0.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-annotation-demo-provider</artifactId>

</project>

2).编写服务实现类(HelloServiceImpl)、配置类(ProviderConfiguration)和启动类(ProviderApplication)

package com.qqxhb.annotation.provider.impl;

import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.rpc.RpcContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qqxhb.demo.api.HelloService;

@Service//标识为dubbo的服务
public class HelloServiceImpl implements HelloService {
	private static final Logger logger = LoggerFactory.getLogger(HelloServiceImpl.class);

	public String sayHello(String name) {
		logger.info("========Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
		return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
	}
}

package com.qqxhb.annotation.provider.config;

import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
//使用dubbo,并制定扫描包
@EnableDubbo(scanBasePackages = "com.qqxhb.annotation.provider")
//指定配置文件
@PropertySource("classpath:/dubbo-provider.properties")
public class ProviderConfiguration {
	@Bean //可以直接在配置文件写:dubbo.registry.address=zookeeper://127.0.0.1:2181
	public RegistryConfig registryConfig() {
		RegistryConfig registryConfig = new RegistryConfig();
		registryConfig.setAddress("zookeeper://127.0.0.1:2181");
		return registryConfig;
	}
}
package com.qqxhb.annotation.provider;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.qqxhb.annotation.provider.config.ProviderConfiguration;

public class ProviderApplication {
	public static void main(String[] args) throws Exception {
		// 使用spring的注解容器加载配置并启动
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
				ProviderConfiguration.class);
		context.start();
		// 阻塞服务,否则会直接关闭
		System.in.read();
		context.close();
	}
}

3).编写配置文件dubbo-provider.properties

dubbo.application.name=dubbo-annotation-demo-provider
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.application.qos.port=22222
4. 创建服务消费者

1).创建dubbo-annotation-demo-consumer模块
2).编写服务引用类、消费者配置类和消费者启动类
3).编写配置文件

1).创建dubbo-annotation-demo-consumer模块

<?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>
		<groupId>com.qqxhb</groupId>
        <artifactId>dubbo-annotation-demo</artifactId>
        <version>0.0.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-annotation-demo-consumer</artifactId>
</project>

2).编写服务引用类(ReferenceHelloService)、消费者配置类(ConsumerConfiguration)和消费者启动类(ConsumerApplication)

package com.qqxhb.annotation.consumer.service;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

import com.qqxhb.demo.api.HelloService;

/**
 * 服务引用类(不强制要求试下服务提供者接口)
 *
 */
@Component("referenceHelloService")
public class ReferenceHelloService implements HelloService {

	@Reference // 注入或者是引用远程服务
	private HelloService helloService;

	@Override
	public String sayHello(String name) {
		return helloService.sayHello(name);
	}

}
package com.qqxhb.annotation.consumer.config;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@EnableDubbo(scanBasePackages = "com.qqxhb.annotation.consumer.service")
@PropertySource("classpath:/dubbo-consumer.properties")
@ComponentScan(value = { "com.qqxhb.annotation.consumer.service" })
public class ConsumerConfiguration {

}
package com.qqxhb.annotation.consumer;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.qqxhb.annotation.consumer.config.ConsumerConfiguration;
import com.qqxhb.annotation.consumer.service.ReferenceHelloService;

public class ConsumerApplication {
	public static void main(String[] args) throws Exception {
		// 加载bean的配置并启动spring容器
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
				ConsumerConfiguration.class);
		context.start();
		// 从spring容器中获取提供者端暴露出的服务实现
		ReferenceHelloService helloService = context.getBean("referenceHelloService", ReferenceHelloService.class);
		System.out.println("======== result: " + helloService.sayHello("annotation-comsumer"));
		context.close();
	}
}

3).编写配置文件dubbo-consumer.properties

dubbo.application.name=dubbo-annotation-demo-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.application.qos.port=33333
5. 启动测试

1).依赖zookeeper作为注册中心,因此需要优先启动zookeeper
zookeeper相关知识请参考之前博客:Zookeeper入门及单机及集群环境搭建
2). 启动 ProviderApplication 、启动ConsumerApplication
ProviderApplication 端窗口打印日志======== result: Hello annotation-comsumer, response from provider: 192.168.25.1:20880
ConsumerApplication 端窗口打印日志========Hello annotation-comsumer, request from consumer: /192.168.25.1:7079

源码地址:https://github.com/qqxhb/dubbo-demo
原生API示例请参考下篇博客:Dubbo入门示例原生API版

发布了131 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43792385/article/details/105219050