一、Spring Cloud Alibaba依赖说明
使用Spring Cloud Alibaba
与其它组件集成时一定要先看下依赖说明,下图是Spring Cloud Alibaba
各组件对应的版本信息。
下图是Spring Cloud Alibaba
和Spring Cloud
以及Spring Boot
对应的适配版本关系。
备注:具体版本说明请参考Spring Cloud Alibaba版本说明。
二、代码示例
1、父工程spring-cloud-alibaba-learning版本依赖信息
<groupId>com.universe</groupId>
<artifactId>spring-cloud-alibaba-learning</artifactId>
<packaging>pom</packaging>
<version>1.0.0-SNAPSHOT</version>
<modules>
<module>spring-cloud-alibaba-seata-consumer</module>
<module>spring-cloud-alibaba-seata-provider</module>
</modules>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.boot.version>2.3.12.RELEASE</spring.boot.version>
<spring.cloud.version>Hoxton.SR12</spring.cloud.version>
<spring.cloud.alibaba.version>2.2.6.RELEASE</spring.cloud.alibaba.version>
<commons.lang3.version>3.12.0</commons.lang3.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons.lang3.version}</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
备注:
1、这里我们Spring Cloud Alibaba
的版本使用的是2.2.6.RELEASE,Spring Cloud
的版本是Hoxton.SR12,Spring Boot
的版本用的是2.3.12.RELEASE。
2、Spring Cloud Alibaba
版本2.2.6.RELEASE也是可以和上述依赖互相兼容的。
2、子工程spring-cloud-alibaba-seata-consumer服务消费者
(1) 版本依赖
<parent>
<groupId>com.universe</groupId>
<artifactId>spring-cloud-alibaba-learning</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>spring-cloud-alibaba-seata-consumer</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<seata.spring.boot.version>1.4.2</seata.spring.boot.version>
<mybatis.plus.boot.version>3.5.1</mybatis.plus.boot.version>
<druid.spring.boot.version>1.2.8</druid.spring.boot.version>
<mysql.connector.version>8.0.28</mysql.connector.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.spring.boot.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis.plus.boot.version}</version>
</dependency>
</dependencies>
(2) application.yml配置
# dubbo configuration
dubbo:
registry:
address: nacos://localhost:8848
consumer:
check: false
scan:
base-packages: com.universe.order.service.impl
spring:
# datasource configuration
datasource:
druid:
url: jdbc:mysql://localhost:3307/mall_order?serverTimeZone=UTC
username: root
password: root
initial-size: 5
max-active: 50
max-wait: 3000
test-on-borrow: false
test-on-return: false
min-idle: 10
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
cloud:
nacos:
discovery:
enabled: false # 禁用Nacos服务注册和发现
# mybatis-plus configuration
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
configuration:
map-underscore-to-camel-case: true
call-setters-on-nulls: true
备注:
1、为什么配置dubbo.consumer.check
为false
呢?
如果服务启动时拉取不到服务提供者列表,启动时会报错。
2、为什么配置spring.cloud.nacos.discovery.enabled
为false
呢?
因为spring-cloud-starter-alibaba-nacos-discovery中有相关自动配置类会自动做服务注册和服务发现,导致服务启动报错。
(3) Dubbo服务引用
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.universe.integral.MallIntegralService;
import com.universe.order.common.OrderStatusEnum;
import com.universe.order.mapper.MallOrderMapper;
import com.universe.order.pojo.ApiResponse;
import com.universe.order.pojo.domain.MallOrderDO;
import com.universe.order.service.MallOrderService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author 刘亚楼
* @date 2022/4/11
*/
@Service
public class MallOrderServiceImpl implements MallOrderService {
@Autowired
private MallOrderMapper mallOrderMapper;
@DubboReference(timeout = 3000)
private MallIntegralService mallIntegralService;
@Override
public ApiResponse<Void> finishOrder(String orderNo, Long userId) {
Wrapper<MallOrderDO> wrapper = Wrappers.<MallOrderDO>lambdaQuery().eq(MallOrderDO::getOrderNo, orderNo);
MallOrderDO mallOrderDO = mallOrderMapper.selectOne(wrapper);
if (mallOrderDO == null) {
return ApiResponse.error("B0001");
}
mallOrderDO.setOrderStatus(OrderStatusEnum.FINISHED.getStatus());
mallOrderDO.setUpdateTime(System.currentTimeMillis());
Wrapper<MallOrderDO> updateWrapper = Wrappers.<MallOrderDO>lambdaUpdate().eq(MallOrderDO::getOrderNo, orderNo);
mallOrderMapper.update(mallOrderDO, updateWrapper);
return null;
}
}
备注:Dubbo==2.7.8==版本服务引入注解由
@Reference
变成了@DubboReference
。
(4) 启动类
@MapperScan(basePackages = "com.universe.integral.mapper")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3、子工程spring-cloud-alibaba-seata-provider服务提供者
(1) 版本依赖
<parent>
<artifactId>spring-cloud-alibaba-learning</artifactId>
<groupId>com.universe</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>spring-cloud-alibaba-seata-provider</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<seata.spring.boot.version>1.4.2</seata.spring.boot.version>
<mybatis.plus.boot.version>3.5.1</mybatis.plus.boot.version>
<druid.spring.boot.version>1.2.8</druid.spring.boot.version>
<mysql.connector.version>8.0.28</mysql.connector.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.spring.boot.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis.plus.boot.version}</version>
</dependency>
</dependencies>
(2) application.yml配置
# dubbo configuration
dubbo:
registry:
address: nacos://localhost:8848
protocol:
port: 20881
name: dubbo # 这配置不能丢,丢了会报错
scan:
base-packages: com.universe.integral.service.impl
spring:
# datasource configuration
datasource:
druid:
url: jdbc:mysql://localhost:3307/mall_integral?serverTimeZone=UTC
username: root
password: root
initial-size: 5
max-active: 50
max-wait: 3000
test-on-borrow: false
test-on-return: false
min-idle: 10
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
cloud:
nacos:
discovery:
enabled: false # 禁用Nacos服务自动发现
# mybatis-plus configuration
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
configuration:
map-underscore-to-camel-case: true
call-setters-on-nulls: true
备注: 对于服务提供者,这里有个小坑,就是
dubbo.protocol.name
这项配置不能丢,丢了服务启动时会出现协议校验出错,如下图:
[图片上传失败...(image-9fae03-1655744670008)]
(3) 服务实现类
package com.universe.integral.service.impl;
import com.universe.integral.mapper.UserIntegralMapper;
import com.universe.integral.pojo.ApiResponse;
import com.universe.integral.pojo.request.AddUserIntegralRequest;
import com.universe.integral.service.MallIntegralService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author 刘亚楼
* @date 2022/4/11
*/
@DubboService
public class MallIntegralServiceImpl implements MallIntegralService {
@Autowired
private UserIntegralMapper userIntegralMapper;
@Override
public ApiResponse<AddUserIntegralRequest> dispatchUserIntegral(AddUserIntegralRequest request) {
return null;
}
}
备注:为了更好和Spring原生注解
@Service
区分,Dubbo==2.7.8==版本标识服务提供者注解变成了@DubboService
。
(4) 启动类
@MapperScan(basePackages = "com.universe.integral.mapper")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
三、启动服务&查看Nacos控制台
可以看到服务提供者和服务消费者已经成功注册到了Nacos上,说明Dubbo和Nacos整合是成功的。
四、总结
1、
Spring Cloud Alibaba
已经提供了相关Dubbo
的依赖,在==Github上==的版本依赖关系表中已经给出了相应的适配组件版本。
2、spring-cloud-starter-dubbo
依赖会引入dubbo-spring-boot-starter
依赖,这里我们使用的dubbo
版本是==2.7.8==。
3、注意禁用spring-cloud-starter-alibaba-nacos-discovery
的服务自动注册和发现,或者如果我们不禁用,可以通过@SpringBootApplication
注解排除NacosServiceRegistryAutoConfiguration和NacosDiscoveryClientConfiguration的自动配置,如下: