springboot 整合 dubbo 入门实例

前期准备:

       因为整合 dubbo 需要使用注册中心,我们先使用 zookeeper 来简单实现下,后续分析各种注册中心的优缺点。安装 zookeeper 的教程很多,可以参考这篇文章安装,这里不再赘述。

项目目录:

       整个项目的架构如下所示,创建的是一个 maven 多模块的工程,其中

       springboot_dubbo :父工程。

       springboot_dubbo_api :定义了简单的接口。

       springboot_dubbo_customer :服务的消费者。

       springboot_dubbo_provider :服务的生产者。

添加依赖:

       在父工程 springboot_dubbo 中添加 maven 依赖,pom.xml 的内容如下所示:

<?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>com</groupId>
	<artifactId>springboot_dubbo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	<modules>
		<module>springboot_dubbo_provider</module>
		<module>springboot_dubbo_customer</module>
		<module>springboot_dubbo_api</module>
	</modules>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<!-- <version>2.1.6.RELEASE</version> -->
		<version>2.1.4.RELEASE</version>
	</parent>
	<dependencies>
		<!-- 引入spring-boot-starter以及dubbo和curator的依赖 -->
		<dependency>
			<groupId>com.alibaba.boot</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>0.2.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>
</project>

       springboot_dubbo_api 工程的 pom.xml 的内容如下所示:

<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>
	<parent>
		<groupId>com</groupId>
		<artifactId>springboot_dubbo</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>springboot_dubbo_api</artifactId>
</project>

       springboot_dubbo_customer 工程的 pom.xml 的内容如下所示:

<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>
	<parent>
		<groupId>com</groupId>
		<artifactId>springboot_dubbo</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>springboot_dubbo_customer</artifactId>
	<dependencies>
		<dependency>
			<groupId>com</groupId>
			<artifactId>springboot_dubbo_api</artifactId>
			<version>0.0.1-SNAPSHOT</version>
			<scope>compile</scope>
		</dependency>
	</dependencies>
</project>

       springboot_dubbo_provider 工程的 pom.xml 的内容如下所示:

<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>
	<parent>
		<groupId>com</groupId>
		<artifactId>springboot_dubbo</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>springboot_dubbo_provider</artifactId>
	<dependencies>
		<dependency>
			<groupId>com</groupId>
			<artifactId>springboot_dubbo_api</artifactId>
			<version>0.0.1-SNAPSHOT</version>
			<scope>compile</scope>
		</dependency>
	</dependencies>
</project>

创建公共接口:

       在 springboot_dubbo_api 工程中,添加一个接口 HelloDubboService ,别的不需要创建,代码如下所示:

package com;

public interface HelloDubboService {

	 String sayHello(String name);
}

创建服务提供者:

       在 springboot_dubbo_provider 工程中创建一个类 HelloDubboServiceImpl 来充当服务的提供者,需要注意的是这里的 @Service 注解是 Dubbo 提供的注解,不是 Spring 提供的注解,这个注解的意思是用来暴露服务的。具体代码如下所示:

package com;

import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;

@Service
@Component
public class HelloDubboServiceImpl implements HelloDubboService{

	@Override
	public String sayHello(String name) {
		return "欢迎 " + name + " 开始学习 Dubbo";
	}
}

       然后配置 application.properties ,内容如下所示:

server.port=8081  
# 当前应用的名字
dubbo.application.name=springboot_dubbo_provider
# 注册中心的协议和地址
#dubbo.registry.protocol=zookeeper
dubbo.registry.address=zookeeper://127.0.0.1:2181
# 通信规则(通信协议和接口)
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
# 连接监控中心
dubbo.monitor.protocol=registry

       最后别忘了,创建一个启动类 ProviderApplication 来启动消费的提供者,@EnableDubbo  注解的意思是扫描包,实现 Dubbo 自动装配,相当于配置 dubbo.scan.base-packages ,具体内容如下所示:

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;

@SpringBootApplication
@EnableDubbo
public class ProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}

创建服务消费者:

       springboot_dubbo_customer 工程主要是来远程调用服务提供者的实现。首先需要定义一个接口并将其实现,代码如下所示:

package com;

public interface CustomerService {

	String sayHello(String message);
}

       这里的 @Service 注解需要注意下,使用 @Reference 来代替 @Autowired  

package com.impl;

import org.springframework.stereotype.Service;

import com.CustomerService;
import com.HelloDubboService;
import com.alibaba.dubbo.config.annotation.Reference;

@Service
public class CustomerServiceImpl implements CustomerService{

	@Reference
    HelloDubboService helloDubboService;
	
	@Override
	public String sayHello(String message) {
		 return helloDubboService.sayHello(message);
	}

}

        然后配置 application.properties ,内容如下所示:

server.port=8082 
dubbo.application.name=order-service-customer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.monitor.protocol=registry

       最后别忘了,创建一个启动类 CustomerApplication 来启动消费的提供者,@EnableDubbo  注解的意思是扫描包,实现 Dubbo 自动装配,相当于配置 dubbo.scan.base-packages ,具体内容如下所示:

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;

@EnableDubbo
@SpringBootApplication
public class CustomerApplication {

    public static void main(String[] args) {
        SpringApplication.run(CustomerApplication.class, args);
    }
}

测试:

       分别启动 zookeeperspringboot_dubbo_provider 和 springboot_dubbo_customer 工程,等到项目启动成功后,在浏览器输入 http://localhost:8082/sayHello?name=zhangsan ,显示的内容如下所示:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快乐的小三菊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值