博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring-boot整合Dubbo分布式架构案例
阅读量:6689 次
发布时间:2019-06-25

本文共 7257 字,大约阅读时间需要 24 分钟。

1.运行环境

开发工具:intellij idea

JDK版本:1.8

项目管理工具:Maven 3.2.5

2.项目文件目录

3.Maven Plugin管理

总项目 pom.xml配置代码:

1 
2
5
4.0.0
6 7
spring-boot-dubbo
8
spring-boot-dubbo
9
pom
10
1.0-SNAPSHOT
11
12
spring-boot-dubbo-api
13
spring-boot-dubbo-provider
14
spring-boot-dubbo-consumer
15
16 17
18
org.springframework.boot
19
spring-boot-starter-parent
20
1.5.6.RELEASE
21
22 23
24
1.7
25
1.5.6.RELEASE
26
27 28
29
30
31
org.springframework.boot
32
spring-boot-starter-web
33
34
35
36
org.springframework.boot
37
spring-boot-starter-test
38
test
39
40
41
42
io.dubbo.springboot
43
spring-boot-starter-dubbo
44
1.0.0
45
46
47 48
View Code

provider pom.xml配置代码:

1 
2
5
6
spring-boot-dubbo
7
spring-boot-dubbo
8
1.0-SNAPSHOT
9
../pom.xml
10
11
4.0.0
12 13
spring-boot-dubbo-provider
14 15
16
17
spring-boot-dubbo
18
spring-boot-dubbo-api
19
1.0-SNAPSHOT
20
21
22
23
24
25
org.springframework.boot
26
spring-boot-maven-plugin
27
${springboot.version}
28
29
30
31 32
View Code

consumer pom.xml配置代码:

1 
2
5
6
spring-boot-dubbo
7
spring-boot-dubbo
8
1.0-SNAPSHOT
9
../pom.xml
10
11
4.0.0
12 13
spring-boot-dubbo-consumer
14 15
16
17
spring-boot-dubbo
18
spring-boot-dubbo-api
19
1.0-SNAPSHOT
20
21
22
23
24
25
org.springframework.boot
26
spring-boot-maven-plugin
27
${springboot.version}
28
29
30
31 32
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=8081spring.dubbo.application.name=providerspring.dubbo.registry.address=zookeeper://localhost:2181spring.dubbo.protocol.name=dubbospring.dubbo.protocol.port=20880## 本机的IP地址spring.dubbo.protocol.host=127.0.0.1spring.dubbo.scan=com.goku.demo## 设置Modulespring.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=consumerspring.dubbo.registry.address=zookeeper://localhost:2181spring.dubbo.scan=com.goku.demospring.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 @RestController13 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.在页面上运行

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     @Override11     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 @RestController15 public class ExampleController {16 17     @Reference(version = "1.0.0",check=false,mock="com.goku.demo.api.service.ExampleServiceMock")18     @Autowired19     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

测试服务未启动返回值

 

 

9.GITHUB地址

待续

转载地址:http://dcuoo.baihongyu.com/

你可能感兴趣的文章
Nginx服务器部署 负载均衡 反向代理
查看>>
C++学习笔记:指向函数的指针
查看>>
Child Action
查看>>
# 2017-2018-1 20155319 实验五 《通讯协议设计》
查看>>
通用后台管理系统(1)-数据库设计
查看>>
做自适应网页
查看>>
ACM的奇计淫巧_bitset优化
查看>>
centos 配置防火墙操作
查看>>
比亚迪速锐F3专用夏季座套 夏天坐垫 四季坐套
查看>>
Java web 实现 之 Filter分析ip统计网站的访问次数
查看>>
bzoj1303
查看>>
2013-2-1 pdf中无法用金山词霸取词问题
查看>>
2015.3.12 C#运用正则表达式点滴
查看>>
CSS布局自适应等分比例
查看>>
requests
查看>>
安装Git
查看>>
设置启动图片LaunchScreen 和 LaunchImage
查看>>
Ubuntu下搭建openGL开发环境
查看>>
ThreadLocal详解,处理成员变量线程不安全的情况
查看>>
四个简单易用的demo,关于iOS定时器和延时的,非常好用。
查看>>