Spring Cloud Alibaba + Spring Boot +dubbo+ Nacos + Mybatis Plus + MySQL 项目搭建步骤
时间: 2023-11-22 16:37:45 浏览: 178
Spring Cloud + Spring Boot 项目的搭建
下面是Spring Cloud Alibaba + Spring Boot + Dubbo + Nacos + Mybatis Plus + MySQL 项目搭建步骤:
1. 创建一个Spring Boot项目,并添加以下依赖:
```xml
<!--Spring Boot依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Dubbo依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<!--Nacos依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<!--Mybatis Plus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<!--MySQL依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
```
2. 配置application.yml文件,添加以下内容:
```yaml
spring:
application:
name: service-provider # 服务名称
datasource:
url: jdbc:mysql://localhost:3306/db_example # 数据库URL
username: root # 数据库用户名
password: root # 数据库密码
driver-class-name: com.mysql.cj.jdbc.Driver # 数据库驱动
mybatis-plus:
mapper-locations: classpath:mapper/*.xml # Mybatis Plus的mapper文件路径
dubbo:
application:
name: dubbo-service-provider # Dubbo应用名称
registry:
address: nacos://localhost:8848 # Nacos注册中心地址
protocol:
name: dubbo # Dubbo协议名称
port: 20880 # Dubbo协议端口号
```
3. 创建一个数据模型类,例如:
```java
@Data
public class User {
private Long id;
private String username;
private String password;
private String email;
}
```
4. 创建一个Mapper类,例如:
```java
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
```
5. 创建一个Service类,例如:
```java
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
```
6. 创建一个Controller类,例如:
```java
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public User getUser(@PathVariable Long id) {
return userService.getById(id);
}
}
```
7. 在Nacos控制台中添加服务提供者的配置信息。
8. 启动服务提供者,并在Nacos控制台中查看服务是否注册成功。
9. 创建一个服务消费者项目,并添加以下依赖:
```xml
<!--Spring Boot依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Dubbo依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<!--Nacos依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
```
10. 配置application.yml文件,添加以下内容:
```yaml
spring:
application:
name: service-consumer # 服务名称
dubbo:
application:
name: dubbo-service-consumer # Dubbo应用名称
registry:
address: nacos://localhost:8848 # Nacos注册中心地址
```
11. 创建一个Service类,例如:
```java
@Service
public class UserServiceImpl implements UserService {
@Reference
private UserService userService;
@Override
public User getUser(Long id) {
return userService.getById(id);
}
}
```
12. 创建一个Controller类,例如:
```java
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public User getUser(@PathVariable Long id) {
return userService.getUser(id);
}
}
```
13. 启动服务消费者,访问http://localhost:8080/user/{id},查看服务是否调用成功。
以上就是Spring Cloud Alibaba + Spring Boot + Dubbo + Nacos + Mybatis Plus + MySQL项目搭建步骤。
阅读全文