基于 Spring Cloud+Spring Boot+Nacos+Dubbo+mybatis plus+RocketMQ+Sentinel+Seata+mysql 搭建一个项目
时间: 2023-09-11 15:03:57 浏览: 308
好的,以下是基于 Spring Cloud+Spring Boot+Nacos+Dubbo+mybatis plus+RocketMQ+Sentinel+Seata+mysql 搭建一个项目的步骤:
1. 首先,你需要安装并启动 Nacos 作为服务注册中心和配置中心。可以去官网下载最新版本的 Nacos,然后解压并启动即可。
2. 接下来,使用 Spring Initializr 创建一个基于 Spring Boot 的项目,并添加以下依赖:
```
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-seata</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
```
3. 配置 Nacos 作为服务注册中心和配置中心:
```
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.prefix=/config
spring.cloud.nacos.config.file-extension=properties
```
4. 配置 Dubbo:
```
spring.application.name=provider-service
spring.dubbo.application.name=provider-service
spring.dubbo.registry.address=nacos://127.0.0.1:8848
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
```
5. 配置 RocketMQ:
```
spring.rocketmq.name-server=127.0.0.1:9876
spring.rocketmq.producer.group=producer-group
spring.rocketmq.consumer.group=consumer-group
```
6. 配置 Sentinel:
```
spring.cloud.sentinel.transport.dashboard=127.0.0.1:8080
```
7. 配置 Seata:
```
spring.cloud.alibaba.seata.tx-service-group=my_seata_tx_group
spring.cloud.alibaba.seata.tx-service-mode=AT
spring.cloud.alibaba.seata.config.type=nacos
spring.cloud.alibaba.seata.config.nacos.server-addr=127.0.0.1:8848
spring.cloud.alibaba.seata.config.nacos.namespace=seata-dev
```
8. 配置 MySQL 数据源:
```
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
```
9. 编写 Dubbo 服务接口和实现类:
```
public interface UserService {
User getUserById(Long id);
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public User getUserById(Long id) {
return userDao.selectById(id);
}
}
```
10. 配置 Dubbo 服务提供者:
```
@Configuration
public class DubboConfig {
@Reference
private UserService userService;
@Bean
public ApplicationRunner runner() {
return args -> {
User user = userService.getUserById(1L);
System.out.println(user);
};
}
}
```
11. 编写 RocketMQ 生产者和消费者:
```
@Component
public class MessageProducer {
@Autowired
private RocketMQTemplate rocketMQTemplate;
public void send(String message) {
rocketMQTemplate.convertAndSend("test-topic", message);
}
}
@Component
@RocketMQMessageListener(topic = "test-topic", consumerGroup = "test-consumer-group")
public class MessageConsumer implements RocketMQListener<String> {
@Override
public void onMessage(String message) {
System.out.println("Received message: " + message);
}
}
```
12. 编写 MySQL 数据访问层:
```
@Mapper
public interface UserDao extends BaseMapper<User> {
}
```
13. 使用 Seata 进行分布式事务控制:
```
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderDao orderDao;
@GlobalTransactional
@Override
public void createOrder(Order order) {
orderDao.insert(order);
// 调用其他服务,可能会涉及到分布式事务操作
// ...
}
}
```
14. 使用 Sentinel 进行服务限流和熔断:
```
@GetMapping("/hello")
@SentinelResource(value = "hello", blockHandler = "helloBlockHandler")
public String hello() {
return "Hello World!";
}
public String helloBlockHandler(BlockException ex) {
return "Blocked by Sentinel: " + ex.getClass().getSimpleName();
}
```
15. 最后,启动项目并测试各个功能是否正常运行。
以上就是基于 Spring Cloud+Spring Boot+Nacos+Dubbo+mybatis plus+RocketMQ+Sentinel+Seata+mysql 搭建一个项目的步骤。
阅读全文