3.Dubbo之SpringBoot篇

GitHub 详细介绍地址

https://github.com/apache/incubator-dubbo-spring-boot-project/blob/master/README_CN.md

https://github.com/apache/incubator-dubbo-spring-boot-project/tree/master/dubbo-spring-boot-actuator#health-checks  //端点

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

开发版本

从现在开始, dubbo-spring-boot-project 将在每个发布中发行两个版本 :

  • 0.2.x 是支持 Spring Boot 2.x 的主要版本(推荐,长期维护)

  • 0.1.x 是支持 Spring Boot 1.x 的维护版本(兼容,短期维护)

依赖关系

版本 Java Spring Boot Dubbo
0.2.0 1.8+ 2.0.x 2.6.2+
0.1.1 1.7+ 1.5.x 2.6.2+

已发行版本

您可以为您的工程引入最新 dubbo-spring-boot-starter 的发布,增加以下依赖到工程的 pom.xml 文件中:

<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>0.2.0</version>
</dependency>

如果您的工程遇到了依赖问题, 请尝试添加如下 Maven 参考到工程的 pom.xml 文件中:

<repositories>
    <repository>
        <id>sonatype-nexus-snapshots</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

通常情况 , Dubbo 应用有两种使用场景 , 其一为 Dubbo 服务提供方 , 另外一个是 Dubbo 服务消费方,当然也允许两者混合.

示例演示

新建dubbo-spring-boot maven项目,工程结构如下:

dubbo-spring-boot 

pom.xml配置如下:

springboot版本为2.0.3

<!--dependencyManagement父类工程管理包  -->
<dependencyManagement>
		<dependencies>
			<!--引入springboot  -->
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-dependencies</artifactId>
				<version>2.0.3.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
</dependencyManagement>
<modules>
  	<module>dubbo-spring-boot-api</module>
  	<module>dubbo-spring-boot-provider</module>
  	<module>dubbo-spring-boot-consumer</module>
</modules>

dubbo-spring-boot-provider

pom.xml配置如下:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
  <!--引入dubbo 集成springboot starter  -->
<dependency>
	<groupId>com.alibaba.boot</groupId>
	<artifactId>dubbo-spring-boot-starter</artifactId>
	<version>0.2.0</version>
</dependency>
<!--redis -->
<dependency>
    <groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
</dependency>

DemoService.java

对外暴露服务接口

package org.niugang.service;
/**
 * 
 * @ClassName:  DemoService   
 * @Description: Dubbo RPC API ,由服务提供方为服务消费方暴露接口
 * @author: niugang
 * @date:   2018年8月17日 下午7:49:11   
 * @Copyright: [email protected]. All rights reserved. 
 *
 */
public interface DemoService {
	   String sayHello(String name);
}

DefaultDemoService.java

对外暴露服务接口实现类

package org.niugang.service;

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

/**
 * 
 * @ClassName:  DefaultDemoService   
 * @Description:对外暴露接口实现类
 * @author: niugang
 * @date:   2018年8月17日 下午7:50:47   
 * @Copyright: [email protected]. All rights reserved. 
 *
 */
@Service
public class DefaultDemoService implements DemoService {

    public String sayHello(String name) {
        return "Hello, " + name + " (from Spring Boot)";
    }

}

application.properties

# springboot应用
spring.application.name = dubbo-provider-demo
server.port = 9090

# 对外暴露服务版本
demo.service.version = 1.0.0

# 扫描带阿里注解的的类(e.g @Service , @Reference)
dubbo.scan.basePackages  = org.niugang.service

# Dubbo Config properties
## 应用配置
dubbo.application.id = dubbo-provider-demo
dubbo.application.name = dubbo-provider-demo

## 协议配置
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 12345

##注册配置  采用redis注册中心
dubbo.registry.id = my-registry
dubbo.registry.address =redis://localhost:6379

启动类

package org.niugang;

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

/**
 * 
 * @ClassName:  DubboProviderApplication   
 * @Description:启动类 
 * @author: niugang
 * @date:   2018年8月17日 下午7:52:56   
 * @Copyright: [email protected]. All rights reserved. 
 *
 */
@SpringBootApplication
public class DubboProviderApplication {

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

}

dubbo-spring-boot-consumer

pom.xml配置如下

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>com.alibaba.boot</groupId>
	<artifactId>dubbo-spring-boot-starter</artifactId>
	<version>0.2.0</version>
</dependency>
	<!--redis -->
<dependency>
	<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!--引入服务提供者 -->
<dependency>
	<groupId>org.niugag</groupId>
	<artifactId>dubbo-spring-boot-provider</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependency>

DemoConsumerController.java

web层调用服务提供者对我暴露的rpc接口

package org.niugang.controller;

import org.niugang.service.DemoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
/**
 * 
 * @ClassName:  DemoConsumerController   
 * @Description:web调用服务提供者对外暴露的rpc接口
 * @author: niugang
 * @date:   2018年8月18日 上午9:41:30   
 * @Copyright: [email protected]. All rights reserved. 
 *
 */
@RestController
public class DemoConsumerController {

	/**
	 * 引入服务提供者
	 */
	//com.alibaba.dubbo.config.annotation.Reference
    @Reference
    private DemoService demoService;

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

}

application.properties

#springboot应用名
spring.application.name = dubbo-consumer-demo
server.port = 8080

# 服务版本
demo.service.version = 1.0.0

# Dubbo Config properties
## 应用配置
dubbo.application.id = dubbo-consumer-demo
dubbo.application.name = dubbo-consumer-demo

## 协议配置  Bean
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 12345

#dubbo 2.6.2版本需要写注册中心配置
dubbo.registry.address =redis://localhost:6379

启动类

package org.niugang;

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

/**
 * 
 * @ClassName:  DubboConsumerApplication   
 * @Description:消费者
 * @author: niugang
 * @date:   2018年8月17日 下午8:50:33   
 * @Copyright: [email protected]. All rights reserved. 
 *
 */
@SpringBootApplication
public class DubboConsumerApplication {

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

}

启动服务提供者,服务消费者

访问消费者web接口。

源码地址:https://gitee.com/niugangxy/dubbo 

                                           

                                                                       JAVA程序猿成长之路

                                                    分享学习资源,学习方法,记录程序员生活。

猜你喜欢

转载自blog.csdn.net/niugang0920/article/details/81777045