spring-boot整合Dubbo分布式架构案例

简介: 1.运行环境开发工具:intellij ideaJDK版本:1.8项目管理工具:Maven 3.2.52.项目文件目录3.Maven Plugin管理总项目 pom.xml配置代码: 1 2 5 4.

1.运行环境

开发工具:intellij idea

JDK版本:1.8

项目管理工具:Maven 3.2.5

2.项目文件目录

3.Maven Plugin管理

总项目 pom.xml配置代码:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>spring-boot-dubbo</groupId>
 8     <artifactId>spring-boot-dubbo</artifactId>
 9     <packaging>pom</packaging>
10     <version>1.0-SNAPSHOT</version>
11     <modules>
12         <module>spring-boot-dubbo-api</module>
13         <module>spring-boot-dubbo-provider</module>
14         <module>spring-boot-dubbo-consumer</module>
15     </modules>
16 
17     <parent>
18         <groupId>org.springframework.boot</groupId>
19         <artifactId>spring-boot-starter-parent</artifactId>
20         <version>1.5.6.RELEASE</version>
21     </parent>
22 
23     <properties>
24         <java.version>1.7</java.version>
25         <springboot.version>1.5.6.RELEASE</springboot.version>
26     </properties>
27 
28     <dependencies>
29         <!-- Spring Boot web依赖 -->
30         <dependency>
31             <groupId>org.springframework.boot</groupId>
32             <artifactId>spring-boot-starter-web</artifactId>
33         </dependency>
34         <!-- Spring Boot test依赖 -->
35         <dependency>
36             <groupId>org.springframework.boot</groupId>
37             <artifactId>spring-boot-starter-test</artifactId>
38             <scope>test</scope>
39         </dependency>
40         <!-- Spring Boot dubbo依赖 -->
41         <dependency>
42             <groupId>io.dubbo.springboot</groupId>
43             <artifactId>spring-boot-starter-dubbo</artifactId>
44             <version>1.0.0</version>
45         </dependency>
46     </dependencies>
47 
48 </project>
View Code

provider pom.xml配置代码:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>spring-boot-dubbo</artifactId>
 7         <groupId>spring-boot-dubbo</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9         <relativePath>../pom.xml</relativePath>
10     </parent>
11     <modelVersion>4.0.0</modelVersion>
12 
13     <artifactId>spring-boot-dubbo-provider</artifactId>
14 
15     <dependencies>
16         <dependency>
17             <groupId>spring-boot-dubbo</groupId>
18             <artifactId>spring-boot-dubbo-api</artifactId>
19             <version>1.0-SNAPSHOT</version>
20         </dependency>
21     </dependencies>
22     <build>
23         <plugins>
24             <plugin>
25                 <groupId>org.springframework.boot</groupId>
26                 <artifactId>spring-boot-maven-plugin</artifactId>
27                 <version>${springboot.version}</version>
28             </plugin>
29         </plugins>
30     </build>
31 
32 </project>
View Code

consumer pom.xml配置代码:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>spring-boot-dubbo</artifactId>
 7         <groupId>spring-boot-dubbo</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9         <relativePath>../pom.xml</relativePath>
10     </parent>
11     <modelVersion>4.0.0</modelVersion>
12 
13     <artifactId>spring-boot-dubbo-consumer</artifactId>
14 
15     <dependencies>
16         <dependency>
17             <groupId>spring-boot-dubbo</groupId>
18             <artifactId>spring-boot-dubbo-api</artifactId>
19             <version>1.0-SNAPSHOT</version>
20         </dependency>
21     </dependencies>
22     <build>
23         <plugins>
24             <plugin>
25                 <groupId>org.springframework.boot</groupId>
26                 <artifactId>spring-boot-maven-plugin</artifactId>
27                 <version>${springboot.version}</version>
28             </plugin>
29         </plugins>
30     </build>
31 
32 </project>
View Code

4.api相关配置编写

ExampleService接口类编写

1 package com.goku.demo.api.service;
2 
3 /**
4  * Created by nbfujx on 2017-11-23.
5  */
6 public interface ExampleService {
7     String echo(String str);
8 }
View Code

5.provider相关配置编写

application.properties编写

## 避免和 consumer 工程端口冲突
server.port=8081

spring.dubbo.application.name=provider
spring.dubbo.registry.address=zookeeper://localhost:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
## 本机的IP地址
spring.dubbo.protocol.host=127.0.0.1
spring.dubbo.scan=com.goku.demo
## 设置Module
spring.dubbo.module.default=false
View Code

ExampleServiceImpl接口实现类编写

package com.goku.demo.service.impl;

import com.goku.demo.api.service.ExampleService;
import com.alibaba.dubbo.config.annotation.Service;

/**
 * Created by nbfujx on 2017-11-23.
 */
@Service(version = "1.0.0")
public class ExampleServiceImpl implements ExampleService {

    @Override
    public String echo(String str) {
        return "hello"+ str;
    }
}
View Code

6.consumer相关配置编写

application.properties编写

spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://localhost:2181
spring.dubbo.scan=com.goku.demo
spring.dubbo.module.default=false
View Code

ExampleController控制器编写

 1 package com.goku.demo.controller;
 2 
 3 import com.alibaba.dubbo.config.annotation.Reference;
 4 import com.goku.demo.api.service.ExampleService;
 5 import org.springframework.web.bind.annotation.PathVariable;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RestController;
 8 
 9 /**
10  * Created by nbfujx on 2017-11-20.
11  */
12 @RestController
13 public class ExampleController {
14 
15     @Reference(version = "1.0.0")
16     public ExampleService exampleService;
17 
18     @RequestMapping("/")
19     public String helloWorld()
20     {
21         return "helloWorld";
22     }
23 
24     @RequestMapping("/{str}")
25     public String echo(@PathVariable  String str)
26     {
27         return exampleService.echo(str);
28     }
29 }
View Code

7.在页面上运行

http://localhost:8080/

http://localhost:8080/str

8.降级服务

mock的配置可以在出现非业务异常(比如超时,网络异常等)时执行。mock的配置支持两种,一种为boolean值,默认的为false。如果配置为true,则缺省使用mock类名,即类名+Mock后缀;另外一种则是配置"return null",可以很简单的忽略掉异常。

 ExampleServiceMock缺省类编写

 1 package com.goku.demo.api.service;
 2 
 3 import org.springframework.stereotype.Service;
 4 
 5 /**
 6  * Created by nbfujx on 2017-11-27.
 7  */
 8 @Service
 9 public class ExampleServiceMock  implements ExampleService {
10     @Override
11     public String echo(String str) {
12         return "hello"+ str+"this is error!";
13     }
14 }
View Code

ExampleController控制器修改

 1 package com.goku.demo.controller;
 2 
 3 import com.alibaba.dubbo.config.annotation.Reference;
 4 import com.goku.demo.api.service.ExampleService;
 5 import com.goku.demo.api.service.ExampleServiceMock;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.web.bind.annotation.PathVariable;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 import org.springframework.web.bind.annotation.RestController;
10 
11 /**
12  * Created by nbfujx on 2017-11-20.
13  */
14 @RestController
15 public class ExampleController {
16 
17     @Reference(version = "1.0.0",check=false,mock="com.goku.demo.api.service.ExampleServiceMock")
18     @Autowired
19     public ExampleService exampleService;
20 
21     @RequestMapping("/")
22     public String helloWorld()
23     {
24         return "helloWorld";
25     }
26 
27     @RequestMapping("/{str}")
28     public String echo(@PathVariable  String str)
29     {
30         return exampleService.echo(str);
31     }
32 }
View Code

测试服务未启动返回值

http://localhost:8080/str

 

 

9.GITHUB地址

https://github.com/nbfujx/springBoot-learn-demo/tree/master/spring-boot-dubbo

待续

目录
相关文章
|
1月前
|
SpringCloudAlibaba Java 持续交付
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
157 0
|
4天前
|
Dubbo Java 应用服务中间件
Java从入门到精通:3.2.2分布式与并发编程——了解分布式系统的基本概念,学习使用Dubbo、Spring Cloud等分布式框架
Java从入门到精通:3.2.2分布式与并发编程——了解分布式系统的基本概念,学习使用Dubbo、Spring Cloud等分布式框架
|
11天前
|
负载均衡 Java 开发者
细解微服务架构实践:如何使用Spring Cloud进行Java微服务治理
【4月更文挑战第17天】Spring Cloud是Java微服务治理的首选框架,整合了Eureka(服务发现)、Ribbon(客户端负载均衡)、Hystrix(熔断器)、Zuul(API网关)和Config Server(配置中心)。通过Eureka实现服务注册与发现,Ribbon提供负载均衡,Hystrix实现熔断保护,Zuul作为API网关,Config Server集中管理配置。理解并运用Spring Cloud进行微服务治理是现代Java开发者的关键技能。
|
26天前
|
负载均衡 网络协议 Java
构建高效可扩展的微服务架构:利用Spring Cloud实现服务发现与负载均衡
本文将探讨如何利用Spring Cloud技术实现微服务架构中的服务发现与负载均衡,通过注册中心来管理服务的注册与发现,并通过负载均衡策略实现请求的分发,从而构建高效可扩展的微服务系统。
|
29天前
|
存储 Java 应用服务中间件
【分布式技术专题】「架构实践于案例分析」盘点互联网应用服务中常用分布式事务(刚性事务和柔性事务)的原理和方案
【分布式技术专题】「架构实践于案例分析」盘点互联网应用服务中常用分布式事务(刚性事务和柔性事务)的原理和方案
52 0
|
1月前
|
敏捷开发 监控 前端开发
Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构
Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构
73 0
|
1月前
|
SpringCloudAlibaba 负载均衡 Java
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(目录大纲)
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(目录大纲)
67 1
|
7天前
|
敏捷开发 监控 数据管理
构建高效微服务架构的五大关键策略
【4月更文挑战第20天】在当今软件开发领域,微服务架构已经成为一种流行的设计模式,它允许开发团队以灵活、可扩展的方式构建应用程序。本文将探讨构建高效微服务架构的五大关键策略,包括服务划分、通信机制、数据管理、安全性考虑以及监控与日志。这些策略对于确保系统的可靠性、可维护性和性能至关重要。
|
8天前
|
消息中间件 监控 持续交付
构建高效微服务架构:后端开发的进阶之路
【4月更文挑战第20天】 随着现代软件开发的复杂性日益增加,传统的单体应用已难以满足快速迭代和灵活部署的需求。微服务架构作为一种新兴的分布式系统设计方式,以其独立部署、易于扩展和维护的特点,成为解决这一问题的关键。本文将深入探讨微服务的核心概念、设计原则以及在后端开发实践中如何构建一个高效的微服务架构。我们将从服务划分、通信机制、数据一致性、服务发现与注册等方面入手,提供一系列实用的策略和建议,帮助开发者优化后端系统的性能和可维护性。
|
2天前
|
消息中间件 负载均衡 持续交付
构建高效微服务架构:后端开发者的终极指南
【4月更文挑战第25天】在当今软件工程领域,微服务架构已经成为实现可扩展、灵活且容错的系统的首选模式。本文将探讨如何从零开始构建一个高效的微服务系统,涵盖关键组件的选择、通信机制、数据管理以及持续集成和部署策略。通过深入分析与案例研究,我们旨在为后端开发者提供一个全面的微服务实践指南,帮助他们在构建现代化应用时做出明智的架构决策。