spring-boot整合Dubbo分布式架构案例
2024-09-05 19:45:49
1.运行环境
开发工具:intellij idea
JDK版本:1.8
项目管理工具:Maven 3.2.5
2.项目文件目录
3.Maven Plugin管理
总项目 pom.xml配置代码:
<?xml version="1.0" encoding="UTF-8"?>
<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>spring-boot-dubbo</groupId>
<artifactId>spring-boot-dubbo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>spring-boot-dubbo-api</module>
<module>spring-boot-dubbo-provider</module>
<module>spring-boot-dubbo-consumer</module>
</modules> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent> <properties>
<java.version>1.7</java.version>
<springboot.version>1.5.6.RELEASE</springboot.version>
</properties> <dependencies>
<!-- Spring Boot web依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot test依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot dubbo依赖 -->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies> </project>
provider pom.xml配置代码:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>spring-boot-dubbo</artifactId>
<groupId>spring-boot-dubbo</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>spring-boot-dubbo-provider</artifactId> <dependencies>
<dependency>
<groupId>spring-boot-dubbo</groupId>
<artifactId>spring-boot-dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${springboot.version}</version>
</plugin>
</plugins>
</build> </project>
consumer pom.xml配置代码:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>spring-boot-dubbo</artifactId>
<groupId>spring-boot-dubbo</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>spring-boot-dubbo-consumer</artifactId> <dependencies>
<dependency>
<groupId>spring-boot-dubbo</groupId>
<artifactId>spring-boot-dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${springboot.version}</version>
</plugin>
</plugins>
</build> </project>
4.api相关配置编写
ExampleService接口类编写
package com.goku.demo.api.service; /**
* Created by nbfujx on 2017-11-23.
*/
public interface ExampleService {
String echo(String str);
}
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
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;
}
}
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
ExampleController控制器编写
package com.goku.demo.controller; import com.alibaba.dubbo.config.annotation.Reference;
import com.goku.demo.api.service.ExampleService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* Created by nbfujx on 2017-11-20.
*/
@RestController
public class ExampleController { @Reference(version = "1.0.0")
public ExampleService exampleService; @RequestMapping("/")
public String helloWorld()
{
return "helloWorld";
} @RequestMapping("/{str}")
public String echo(@PathVariable String str)
{
return exampleService.echo(str);
}
}
7.在页面上运行
8.降级服务
mock的配置可以在出现非业务异常(比如超时,网络异常等)时执行。mock的配置支持两种,一种为boolean值,默认的为false。如果配置为true,则缺省使用mock类名,即类名+Mock后缀;另外一种则是配置"return null",可以很简单的忽略掉异常。
ExampleServiceMock缺省类编写
package com.goku.demo.api.service; import org.springframework.stereotype.Service; /**
* Created by nbfujx on 2017-11-27.
*/
@Service
public class ExampleServiceMock implements ExampleService {
@Override
public String echo(String str) {
return "hello"+ str+"this is error!";
}
}
ExampleController控制器修改
package com.goku.demo.controller; import com.alibaba.dubbo.config.annotation.Reference;
import com.goku.demo.api.service.ExampleService;
import com.goku.demo.api.service.ExampleServiceMock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* Created by nbfujx on 2017-11-20.
*/
@RestController
public class ExampleController { @Reference(version = "1.0.0",check=false,mock="com.goku.demo.api.service.ExampleServiceMock")
@Autowired
public ExampleService exampleService; @RequestMapping("/")
public String helloWorld()
{
return "helloWorld";
} @RequestMapping("/{str}")
public String echo(@PathVariable String str)
{
return exampleService.echo(str);
}
}
测试服务未启动返回值
9.GITHUB地址
https://github.com/nbfujx/springBoot-learn-demo/tree/master/spring-boot-dubbo
待续
最新文章
- Java线上应用故障排查之一:高CPU占用
- 观察者模式(Observer和Observable实现)
- [转]opencv3.0 鱼眼相机标定
- jquery常用选择器和常用方法
- Activity之多启动图标
- jquery replace用法汇总
- ccConfig(设置一些底层接口状态:是否支持动作叠加 设置fps更新间隔和位置 是否画边框等。。)
- .net重启iis线程池和iis站点程序代码分享
- Ubuntu Geany中文乱码
- Hash表题目整数hash-HDOJ1425(转载)
- EF中用Newtonsoft.Json引发的循环引用问题
- 函数&;闭包
- HDU - 1067 Gap (bfs + hash) [kuangbin带你飞]专题二
- [BZOJ4517] [Sdoi2016] 排列计数 (数学)
- MySQL系列教程(五)
- antd form 自定义验证表单使用方法
- mybatis-generator自动生成代码插件
- mysql8.0修改密码无效的问题
- linux下用户操作
- 使用python解决算法和数据结构--使用栈实现进制转换