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程序猿成長之路

                                                    分享學習資源,學習方法,記錄程序員生活。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章