JAVA全系列 教程
3762个小节阅读:7088k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
xxxxxxxxxx
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.itbaizhan</groupId>
<artifactId>itbaizhan-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- dubbo和springboot整合依赖包 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<!-- dubbo 注册中心nacos依赖包 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
在 application.yml
配置文件中添加相关配置。
xxxxxxxxxx
#====================================Dubbo config===============================================
dubbo:
application:
id: dubbo-business-example
name: dubbo-business-example
qosEnable: false
protocol:
id: dubbo
name: dubbo
port: -1
registry:
id: dubbo-business-example-registry
address: nacos://192.168.47.100:8848
xxxxxxxxxx
@Slf4j
@SpringBootApplication
@EnableDubbo
public class BusinessApplication
{
public static void main(String[] args) {
SpringApplication.run(BusinessApplication.class, args);
log.info("****************** 业务系统启动成功 ***********");
}
}
xxxxxxxxxx
/**
* 商品购买服务
*/
@Service
public class BusinessServiceImpl implements IBusinessService {
@DubboReference
private IStorageService iStorageService;// 库存
@DubboReference
private IOrderService iOrderService;// 订单
/**
* 处理业务服务
* @param businessDTO
* @return
*/
@Override
public BaseResponse handleBusiness(BusinessDTO businessDTO) {
//1、扣减库存
BaseResponse storageBase = iStorageService.deduct(businessDTO.getCommodityCode(), businessDTO.getCount());
//2、创建订单
BaseResponse orderBase = iOrderService.createOrder(businessDTO.getUserId(),businessDTO.getCommodityCode(),businessDTO.getCount(),businessDTO.getMoney());
if(storageBase.getCode() == 200 && orderBase.getCode() ==200){
return BaseResponse.success();
}else {
return BaseResponse.error("服务异常");
}
}
}
xxxxxxxxxx
@RestController
@RequestMapping("/business/dubbo")
@Slf4j
public class BusinessController {
@Autowired
private IBusinessService iBusinessService;
@GlobalTransactional
@PostMapping("/buy")
public BaseResponse handleBusiness(@RequestBody BusinessDTO businessDTO) {
String xid = RootContext.getXID();
log.info("Business service Begin XID:{}",xid);
log.info("请求参数:{}", businessDTO.toString());
return iBusinessService.handleBusiness(businessDTO);
}
}