448 lines
12 KiB
Markdown
448 lines
12 KiB
Markdown
# mall4cloud项目新增一个微服务
|
||
|
||
```
|
||
说明: 本文以新增一个分销微服务(mall4cloud-distribution)项目模块为例子
|
||
(默认已创建mall4cloud_distribution数据库)
|
||
```
|
||
|
||
> 总体上分为5步:
|
||
>
|
||
> 1. 新建mall4cloud-distribution微服务模块
|
||
>
|
||
> 2. 修改或者新增mall4cloud_distribution的pom.xml、bootstrap.yml、Dockerfile、启动类上注解
|
||
>
|
||
> 3. nacos上新增mall4cloud-distribution.yml文件,配置数据源;
|
||
>
|
||
> nacos上修改mall4cloud-gateway.yml文件配置路由;
|
||
>
|
||
> nacos上新增mall4cloud-distribution相关seata的配置;
|
||
>
|
||
> 4. 新建mall4cloud-api-distribution模块远程feign调用接口模块
|
||
>
|
||
> 5. mall4cloud-distribution引入mall4cloud-api-distribution模块,新增一些配置
|
||
|
||
|
||
|
||
## 1. 新增项目
|
||
|
||
a.在mall4cloud上右键 New -> Module
|
||
|
||

|
||
|
||
b.我们要新增一个可以带启动类的项目,这里我们选择用springboot快速创建一个项目,选择Spring Initialiar
|
||
|
||

|
||
|
||
c.点击 Next 下一步
|
||
|
||
```
|
||
Group: com.mall4j.cloud
|
||
|
||
Artifact: mall4cloud-distribution
|
||
|
||
Type: Maven
|
||
|
||
Language: Java
|
||
|
||
Packaging: Jar
|
||
|
||
Java version: 8
|
||
|
||
Version: 0.0.1-SNAPSHOT
|
||
|
||
Name: distribution
|
||
|
||
Description: mall4cloud 分销服务
|
||
|
||
Package: com.mall4j.cloud.distribution
|
||
```
|
||
|
||
|
||
|
||

|
||
|
||
d.点击 Next 下一步,这里我们不需要选择任何依赖,我们需要在项目新增完成后,手动添加修改依赖
|
||
|
||

|
||
|
||
e.点击 Next 下一步
|
||
|
||
```
|
||
Module Name: mall4cloud-distribution
|
||
Content root: 这个会根据 Module Name 自动修改。
|
||
Module file location: 这个会根据 Module Name 自动修改
|
||
```
|
||
|
||
|
||
|
||

|
||
|
||
最后点击 Finish 等待项目创建,创建完成的项目如下所示
|
||
|
||

|
||
|
||
## 2.修改项目mall4cloud-distribution
|
||
|
||
a.我们需要删除掉一些文件和.mvn、test文件夹:
|
||
|
||
```
|
||
.mvn
|
||
application.properties
|
||
test
|
||
.gitignore
|
||
HELP.md
|
||
mvnw
|
||
mvnw.cmd
|
||
```
|
||
|
||
删除完成后的结果如下图所示
|
||
|
||

|
||
|
||
b.修改mall4cloud-distribution的pom.xml文件, 依赖可根据实际需要引入或者删除,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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||
<parent>
|
||
<artifactId>mall4cloud</artifactId>
|
||
<groupId>com.mall4j.cloud</groupId>
|
||
<version>0.0.1</version>
|
||
</parent>
|
||
<modelVersion>4.0.0</modelVersion>
|
||
|
||
<artifactId>mall4cloud-distribution</artifactId>
|
||
<description>mall4cloud 分销服务</description>
|
||
<packaging>jar</packaging>
|
||
|
||
|
||
<dependencies>
|
||
<!--注册中心客户端-->
|
||
<dependency>
|
||
<groupId>com.alibaba.cloud</groupId>
|
||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||
</dependency>
|
||
<dependency>
|
||
<groupId>com.alibaba.cloud</groupId>
|
||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||
</dependency>
|
||
<dependency>
|
||
<groupId>com.mall4j.cloud</groupId>
|
||
<artifactId>mall4cloud-common-database</artifactId>
|
||
<version>${project.version}</version>
|
||
</dependency>
|
||
<dependency>
|
||
<groupId>com.mall4j.cloud</groupId>
|
||
<artifactId>mall4cloud-common-security</artifactId>
|
||
<version>${project.version}</version>
|
||
</dependency>
|
||
<dependency>
|
||
<groupId>com.mall4j.cloud</groupId>
|
||
<artifactId>mall4cloud-api-product</artifactId>
|
||
<version>${project.version}</version>
|
||
</dependency>
|
||
<dependency>
|
||
<groupId>com.mall4j.cloud</groupId>
|
||
<artifactId>mall4cloud-api-search</artifactId>
|
||
<version>${project.version}</version>
|
||
</dependency>
|
||
<dependency>
|
||
<groupId>com.mall4j.cloud</groupId>
|
||
<artifactId>mall4cloud-api-platform</artifactId>
|
||
<version>${project.version}</version>
|
||
</dependency>
|
||
<dependency>
|
||
<groupId>com.mall4j.cloud</groupId>
|
||
<artifactId>mall4cloud-api-multishop</artifactId>
|
||
<version>${project.version}</version>
|
||
</dependency>
|
||
<dependency>
|
||
<groupId>com.mall4j.cloud</groupId>
|
||
<artifactId>mall4cloud-api-order</artifactId>
|
||
<version>${project.version}</version>
|
||
</dependency>
|
||
<dependency>
|
||
<groupId>com.mall4j.cloud</groupId>
|
||
<artifactId>mall4cloud-api-delivery</artifactId>
|
||
<version>${project.version}</version>
|
||
</dependency>
|
||
<dependency>
|
||
<groupId>com.mall4j.cloud</groupId>
|
||
<artifactId>mall4cloud-common-rocketmq</artifactId>
|
||
<version>${project.version}</version>
|
||
</dependency>
|
||
<dependency>
|
||
<groupId>com.xuxueli</groupId>
|
||
<artifactId>xxl-job-core</artifactId>
|
||
</dependency>
|
||
</dependencies>
|
||
|
||
<build>
|
||
<plugins>
|
||
<plugin>
|
||
<groupId>org.springframework.boot</groupId>
|
||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||
</plugin>
|
||
<plugin>
|
||
<groupId>io.fabric8</groupId>
|
||
<artifactId>docker-maven-plugin</artifactId>
|
||
<configuration>
|
||
<skip>false</skip>
|
||
</configuration>
|
||
</plugin>
|
||
</plugins>
|
||
</build>
|
||
|
||
</project>
|
||
|
||
```
|
||
|
||
在mall4cloud的pom.xml的modules里面添加mall4cloud-distribution模块
|
||
|
||
```
|
||
<modules>
|
||
<module>mall4cloud-distribution</module>
|
||
</modules>
|
||
```
|
||
|
||
|
||
|
||
|
||
|
||
c. 新增bootstrap.yml文件,并且配置好项目的端口、名称以及nacos注册中心的地址
|
||
|
||

|
||
|
||
d. 修改启动类上的启动配置
|
||
|
||
```
|
||
@SpringBootApplication(scanBasePackages = { "com.mall4j.cloud" },exclude = UserDetailsServiceAutoConfiguration.class)
|
||
@EnableFeignClients(basePackages = {"com.mall4j.cloud.**.feign"})
|
||
```
|
||
|
||
|
||
|
||

|
||
|
||
|
||
|
||
e. 先去nacos上面新增一个mall4cloud-distribution.yml配置文件,并且配置好数据源
|
||
|
||
```yaml
|
||
Data ID: mall4cloud-distribution.yml
|
||
Group: DEFAULT_GROUP
|
||
配置格式: YAML
|
||
|
||
配置内容:
|
||
#数据源
|
||
spring:
|
||
datasource:
|
||
url: jdbc:mysql://192.168.193.128:3306/mall4cloud_distribution?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=FALSE&useJDBCCompliantTimezoneShift=TRUE&useLegacyDatetimeCode=FALSE&serverTimezone=GMT%2B8&allowMultiQueries=true&rewriteBatchedStatements=true&allowPublicKeyRetrieval=TRUE&useAffectedRows=TRUE
|
||
username: root
|
||
PASSWORD: root
|
||
```
|
||
|
||
|
||
|
||

|
||
|
||
|
||
|
||
f.新增完成后,我们需要在我们的网关mall4cloud-gateway,配置一下mall4cloud-distribution的路由,在nacos上找到我们的mall4cloud-gateway.yml文件,新增mall4cloud-distribution路由配置
|
||
|
||
```yaml
|
||
- id: mall4cloud-distribution
|
||
uri: lb://mall4cloud-distribution
|
||
predicates:
|
||
- Path=/mall4cloud_distribution/**
|
||
filters:
|
||
- RewritePath=/mall4cloud_distribution(?<segment>/?.*), $\{segment}
|
||
```
|
||
|
||
|
||
|
||

|
||
|
||
|
||
|
||
j.我们引入了seata,所以 还需要在nacos的application.yml上配置seata
|
||
|
||
```
|
||
seata:
|
||
enableAutoDataSourceProxy: false
|
||
config:
|
||
type: nacos
|
||
nacos:
|
||
namespace: nacos中seata的命名空间id
|
||
dataId: "seataServer.properties"
|
||
server-addr: ${spring.cloud.nacos.discovery.server-addr}
|
||
password: ${spring.cloud.nacos.discovery.password}
|
||
username: ${spring.cloud.nacos.discovery.username}
|
||
registry:
|
||
type: nacos
|
||
nacos:
|
||
server-addr: ${spring.cloud.nacos.discovery.server-addr}
|
||
password: ${spring.cloud.nacos.discovery.password}
|
||
username: ${spring.cloud.nacos.discovery.username}
|
||
namespace: ${seata.config.nacos.namespace}
|
||
```
|
||
|
||
|
||
|
||

|
||
|
||
我们尝试启动,如果启动成功且服务没有打印错误日志,我们这个微服务基本上是创建成功了。
|
||
|
||
接下来,我们需要完善一些服务需要的其他内容
|
||
|
||
|
||
|
||
## 3.新增远程调用api
|
||
|
||
```
|
||
就我们的项目结构设计而言,我们把feign远程调用的接口都抽取了出来,放在了mall4cloud-api这个模块下面,所以我们要新增一个mall4cloud-api-distribution模块
|
||
```
|
||
|
||
a.在mall4cloud-api上右键 New -> Module
|
||
|
||

|
||
|
||
b.我们只是新增一个模块,这个地方不需要启动类,所以我们这里创建一个maven就好了
|
||
|
||

|
||
|
||
c.点击 Next 下一步
|
||
|
||
```
|
||
Parent: mall4cloud-api // 选择mall4cloud-api
|
||
Name: mall4cloud-api-distribution
|
||
Lacation: 自动填写
|
||
GroupId: com.mall4j.cloud.api
|
||
Artifactld: mall4cloud-api-distribution
|
||
Version: 0.01
|
||
```
|
||
|
||
|
||
|
||

|
||
|
||
4.点击 Finish ,创建完成后如图所示
|
||
|
||

|
||
|
||
删除test、resource文件夹,修改pom.xml文件,新增mall4cloud-api-distribution的feign接口
|
||
|
||

|
||
|
||
修改后的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>mall4cloud-api</artifactId>
|
||
<groupId>com.mall4j.cloud</groupId>
|
||
<version>0.0.1</version>
|
||
</parent>
|
||
<modelVersion>4.0.0</modelVersion>
|
||
|
||
<artifactId>mall4cloud-api-distribution</artifactId>
|
||
<packaging>jar</packaging>
|
||
<description>分销对内接口</description>
|
||
|
||
<dependencies>
|
||
<dependency>
|
||
<groupId>com.mall4j.cloud</groupId>
|
||
<artifactId>mall4cloud-common-order</artifactId>
|
||
<version>${project.version}</version>
|
||
</dependency>
|
||
<dependency>
|
||
<groupId>com.mall4j.cloud</groupId>
|
||
<artifactId>mall4cloud-common-product</artifactId>
|
||
<version>${project.version}</version>
|
||
</dependency>
|
||
</dependencies>
|
||
</project>
|
||
```
|
||
|
||
新增的DistributionFeignClient.class类
|
||
|
||
接口上要加上注解@FeignClient(value = "mall4cloud-distribution",contextId ="distribution")
|
||
|
||
```
|
||
package com.mall4j.cloud.api.distribution.feign;
|
||
|
||
import org.springframework.cloud.openfeign.FeignClient;
|
||
|
||
/**
|
||
* @author cl
|
||
* @date 2021-08-05 16:07:34
|
||
*/
|
||
@FeignClient(value = "mall4cloud-distribution",contextId ="distribution")
|
||
public interface DistributionFeignClient {
|
||
}
|
||
|
||
```
|
||
|
||
|
||
|
||
## 4.完善mall4cloud-distribution的配置和feign接口实现
|
||
|
||
在mall4cloud-distribution项目引入mall4cloud-api-distribution
|
||
|
||
```xml
|
||
<dependency>
|
||
<groupId>com.mall4j.cloud</groupId>
|
||
<artifactId>mall4cloud-api-distribution</artifactId>
|
||
<version>${project.version}</version>
|
||
</dependency>
|
||
```
|
||
|
||
DistributionFeignController来实现DistributionFeignClient接口
|
||
|
||
```
|
||
public class DistributionFeignController implements DistributionFeignClient {
|
||
}
|
||
|
||
```
|
||
|
||
|
||
|
||

|
||
|
||
|
||
|
||
添加Swagger文档配置SwaggerConfiguration.class
|
||
|
||
```java
|
||
/**
|
||
* Swagger文档,只有在测试环境才会使用
|
||
*
|
||
* @author FrozenWatermelon
|
||
*/
|
||
@Configuration
|
||
@EnableSwagger2
|
||
@EnableKnife4j
|
||
public class SwaggerConfiguration {
|
||
|
||
@Bean
|
||
public Docket baseRestApi() {
|
||
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
|
||
.apis(RequestHandlerSelectors.basePackage("com.mall4j.cloud.distribution.controller")).paths(PathSelectors.any())
|
||
.build();
|
||
}
|
||
|
||
@Bean
|
||
public ApiInfo apiInfo() {
|
||
return new ApiInfoBuilder().title("mall4cloud商城接口文档").description("mall4cloud商城接口文档Swagger版").termsOfServiceUrl("")
|
||
.contact(new Contact("广州蓝海创新科技有限公司", "", "")).version("1.0").build();
|
||
}
|
||
|
||
}
|
||
```
|
||
|
||
到这里微服务就创建完成了,相关基本配置也搭建好了。
|