(8)Dubbo与SpringBoot整合的四种方式

x33g5p2x  于2021-12-21 转载在 其他  
字(11.6k)|赞(0)|评价(0)|浏览(409)

本节介绍

本节将会介绍Dubbo与SpringBoot整合的四种方式:1、xml配置方式;2、使用@DubboComponentScan;3、使用Dubbo的外部化配置方式@EnableDubboConfig;4、使用 incubator-dubbo-spring-boot-project 或者**dubbo-spring-boot-starter**。第一种方式Dubbo的新老版本都可以使用,不会有版本限制;第二、第三种适合于Dubbo跟SpringBoot整合想用注解,但是又没用Dubbo的SpringBoot的starter;第四种使用了提供的starter,可以帮助我们自动配置,比二三种使用更加简便,也是目前比较流行的。

一:使用xml方式与SpringBoot整合

这种方式最为简单,Dubbo的新老版本都能使用,可以直接在项目的启动类中导入Dubbo的服务提供者、消费者的xml配置文件即可,如下所示

@SpringBootApplication
@ImportResource("classpath:dubbo-provider.xml")
public class Application {
	
	public static void main(String[] args) throws IOException {
		SpringApplication.run(Application.class, args);
	}
}
@SpringBootApplication
@ImportResource("classpath:dubbo-consumer.xml")
public class Application {
	
	public static void main(String[] args) throws IOException {
		SpringApplication.run(Application.class, args);
	}
}

 二:使用@DubboComponentScan与SpringBoot整合

主要就是利用@Configuration和@Bean注解将Dubbo的xml配置转化为spring的bean,并且利用Dubbo的@DubboComponentScan注解去扫描添加了Dubbo的@Service和@Reference注解的类。如果您感兴趣的话可以看一下我的上一节博客《(7)Dubbo的四种配置方式》中介绍的第四种注解配置方式。(注意:该注解在Dubbo2.5.7及以后版本才支持)

三:使用Dubbo外部化配置方式@EnableDubboConfig

在上面一种方式中,无论是服务提供方,还是服务消费方,都要配Dubbo相关配置Bean,用起来还是不够方便,Dubbo参考了SpringBoot的外部化配置(External Configuration),提供了一个比上面更好用更简便的注解@EnableDubboConfig(注意:该注解在Dubbo2.5.8及以后版本才支持)。当标注 @EnableDubboConfig 的类被扫描注册后,同时 Spring(SpringBoot)应用配置(PropertySources)中存在dubbo.application.* 时,ApplicationConfig Bean 将被注册到在 Spring 上下文。否则,不会被注册。如果出现dubbo.registry.*的配置,那么,RegistryConfig Bean 将会创建,以此类推。即按需装配 Dubbo 配置 Bean。举例如下:

# 单 Dubbo 配置 Bean 绑定
## application
dubbo.application.id = applicationBean
dubbo.application.name = dubbo-demo-application

## module
dubbo.module.id = moduleBean
dubbo.module.name = dubbo-demo-module

## registry
dubbo.registry.address = zookeeper://192.168.74.4:2181?backup=192.168.74.5:2181,192.168.74.6:2181
## protocol
dubbo.protocol.name = dubbo
dubbo.protocol.port = 20880

## monitor
dubbo.monitor.address = zookeeper://192.168.74.4:2181?backup=192.168.74.5:2181,192.168.74.6:2181

@EnableDubboConfig配置Dubbo的Bean

@EnableDubboConfig
@PropertySource("classpath:dubbo-config.properties")
@Configuration
public class DubboConfiguration {

}

项目启动类

@SpringBootApplication
public class ProviderTestApp {
    public static void main(String[] args) throws IOException {
	SpringApplication.run(ProviderTestApp.class, args);
    }
}

看过一篇博客对@EnableDubboConfig的使用讲的听详细的,如果您对这个很感兴趣的话,请参考https://segmentfault.com/a/1190000012661402  http://dubbo.apache.org/zh-cn/blog/dubbo-annotation.html

四:使用Dubbo的SpringBoot的starter

常用的主要有两个:

  1. dubbo-spring-boot-starter 项目地址: https://github.com/alibaba/dubbo-spring-boot-starter,在2018年3月11日更新到2.0.0版本后已经停止更新,最新版本依赖支持SpringBoot1.5.10.RELEASE、Dubbo2.6.0,因为Dubbo官方也提供了一个启动器incubator-dubbo-spring-boot-project,所以这个项目已经停更了。。。。。。所以不推荐大家使用了。这里就不进行演示了,感兴趣的话可以看下官方文档。
  2. Dubbo官方的 incubator-dubbo-spring-boot-project 项目地址:https://github.com/apache/incubator-dubbo-spring-boot-project。下面就通过一个demo案例来演示一下通过 incubator-dubbo-spring-boot-project 完成Dubbo与SpringBoot的整合。

incubator-dubbo-spring-boot-project介绍:

Dubbo Spring Boot 工程致力于简化 Dubbo RPC 框架在 Spring Boot 应用场景的开发。同时也整合了 Spring Boot 特性:

从现在开始, dubbo-spring-boot-project 将在每个发布中发行两个版本 (推荐使用0.2.X)

  • 0.2.x 是支持 Spring Boot 2.x 的主要版本(推荐,长期维护)
  • 0.1.x 是支持 Spring Boot 1.x 的维护版本(兼容,短期维护)
版本JavaSpring BootDubbo
0.2.01.8+2.0.x2.6.2+
0.1.11.7+1.5.x2.6.2+

关于incubator-dubbo-spring-boot-project中在application.properties的配置属性可以参考如下:dubbo-spring-boot-autoconfigure-0.2.0.jar下面的spring-configuration-metadata.json里面有完整的定义

下面通过一个案例,来介绍一下跟Dubbo的整合是怎么用的。

dubbo-spring-boot-demo-api

公共项目,服务提供者和消费者都会依赖这个项目,里面主要存放暴露的api接口,接口中用到的model、枚举、自定义异常类等。

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>
  <groupId>com.wkp</groupId>
  <artifactId>dubbo-spring-boot-demo-api</artifactId>
  <version>0.0.1-SNAPSHOT</version>
</project>

里面只有一个接口如下所示

package com.dubbo.springboot.demo.provider.service;

public interface DemoService {

	String sayHello(String name);
}

dubbo-spring-boot-demo-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>
	<groupId>com.wkp</groupId>
	<artifactId>dubbo-spring-boot-demo-provider</artifactId>
	<version>0.0.1-SNAPSHOT</version>

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

	<dependencies>
		<!-- Spring Boot dependencies -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>com.alibaba.boot</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>0.2.0</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba.boot</groupId>
			<artifactId>dubbo-spring-boot-actuator</artifactId>
			<version>0.2.0</version>
		</dependency>
		<dependency>
			<groupId>com.wkp</groupId>
			<artifactId>dubbo-spring-boot-demo-api</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

DemoService接口的实现类如下:

package com.dubbo.springboot.demo.provider.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dubbo.springboot.demo.provider.service.DemoService;
@Service
public class DemoServiceImpl implements DemoService {

	public String sayHello(String name) {
		return "hello "+name;
	}

}

application.properties配置如下

# Spring boot application
spring.application.name = dubbo-provider-demo

# Base packages to scan Dubbo Component: @com.alibaba.dubbo.config.annotation.Service
dubbo.scan.basePackages  = com.dubbo.springboot.demo.provider.service.impl

# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.id = dubbo-provider-demo
dubbo.application.name = dubbo-provider-demo

## ProtocolConfig Bean
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 20880

## RegistryConfig Bean
dubbo.registry.id = my-registry
dubbo.registry.address = zookeeper://192.168.74.4:2181?backup=192.168.74.5:2181,192.168.74.6:2181

服务提供者启动类,注意这里是以非web工程启动的

package com.dubbo.springboot.demo.provider.bootstrap;

import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class DubboProviderApplication {

	public static void main(String[] args) {
		new SpringApplicationBuilder(DubboProviderApplication.class)
		.web(WebApplicationType.NONE) // NONE为非web工程,一般用于专门提供接口的jar
		.run(args);
	}
}

 dubbo-spring-boot-demo-consumer

服务消费者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>
	<groupId>com.wkp</groupId>
	<artifactId>dubbo-spring-boot-demo-consumer</artifactId>
	<version>0.0.1-SNAPSHOT</version>

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

	<dependencies>
		<!-- Spring Boot dependencies -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>com.alibaba.boot</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>0.2.0</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba.boot</groupId>
			<artifactId>dubbo-spring-boot-actuator</artifactId>
			<version>0.2.0</version>
		</dependency>
		<dependency>
			<groupId>com.wkp</groupId>
			<artifactId>dubbo-spring-boot-demo-api</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

里面的调用业务类及Controller如下

package com.dubbo.springboot.demo.consumer.service;

import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dubbo.springboot.demo.provider.service.DemoService;

@Service
public class DemoConsumerService {

	@Reference
	private DemoService demoService;
	
	public String sayHello(String name) {
		return demoService.sayHello(name);
	}
}
package com.dubbo.springboot.demo.consumer.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dubbo.springboot.demo.consumer.service.DemoConsumerService;

@RestController
public class HelloController {
	
	@Resource
	private DemoConsumerService demoConsumerService;

	@RequestMapping("/sayHello")
	public String sayHello(@RequestParam String name) {
		return demoConsumerService.sayHello(name);
	}
}

application.properties配置如下

# Spring boot application
spring.application.name = dubbo-consumer-demo
server.port = 8080
management.server.port = 8081

# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.id = dubbo-consumer-demo
dubbo.application.name = dubbo-consumer-demo

## RegistryConfig Bean
dubbo.registry.id = my-registry
dubbo.registry.address = zookeeper://192.168.74.4:2181?backup=192.168.74.5:2181,192.168.74.6:2181

# Dubbo Endpoint (default status is disable)
endpoints.dubbo.enabled = true

# Dubbo Health
## StatusChecker Name defaults (default : "memory", "load" )
management.health.dubbo.status.defaults = memory

# Enables Dubbo All Endpoints
management.endpoint.dubbo.enabled = true
management.endpoint.dubbo-shutdown.enabled = true
management.endpoint.dubbo-configs.enabled = true
management.endpoint.dubbo-services.enabled = true
management.endpoint.dubbo-references.enabled = true
management.endpoint.dubbo-properties.enabled = true

# Exposes all web endpoints
management.endpoints.web.exposure.include = *

消费者启动类如下:

package com.dubbo.springboot.demo.consumer.bootstrap;

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

@SpringBootApplication(scanBasePackages = "com.dubbo.springboot.demo.consumer")
public class DubboConsumerApplication {
	public static void main(String[] args) {

		SpringApplication.run(DubboConsumerApplication.class, args);

	}
}

服务提供者、消费者启动完成后,访问结果如下表示已经成功。

健康检查 : http://localhost:8081/actuator/health

Dubbo Endpoints : http://localhost:8081/actuator/dubbo

至此,Dubbo与SpringBoot已经整合完成,使用@Service注解的时候注意一下不要把Dubbo跟Spring的弄错啦。

相关文章