Author Avatar
Jafir Mar 01, 2024

第一步(创建项目,以及对应的model)

创建项目

  1. 选择最普通的模板,(maven-archetype-site)
  2. 输入微服务名
  3. 选择自己仓库的maven配置文件,记得配置文件添加私服依赖
    1
    2
    3
    4
    5
    6
    <server>
    <!--根据id来取用账号密码-->
    <id>maven_xiaoxi</id>
    <username>admin</username>
    <password>xiaoxi</password>
    </server>

创建model

无需任何选择,根据自己业务而定

  • 有接口暴露的,创建两个model(xxx-service、xxx-interface)
  • 没有接口暴露的,创建一个model(xxx-service)

    第二步,分别修改两个model的pom文件

    xxx-interface的pom文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    <?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>
    <groupId>com.xiaoxi</groupId>
    <artifactId>common-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <dependencies>
    <dependency>
    <groupId>com.xiaoxi</groupId>
    <artifactId>common</artifactId>
    <version>1.0-SNAPSHOT</version>
    </dependency>
    </dependencies>


    <artifactId>xxx-interface</artifactId>

    <properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <repositories>
    <repository>
    <id>maven_xiaoxi</id>
    <url>http://192.168.148.237:8081/repository/maven-public/</url>
    <releases>
    <enabled>true</enabled>
    <updatePolicy>always</updatePolicy>
    </releases>
    <snapshots>
    <updatePolicy>always</updatePolicy>
    <enabled>true</enabled>
    </snapshots>
    </repository>
    </repositories>


    </project>

其中,parent、dependencies、repositories不能缺少

xxx-service的pom文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?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>
<groupId>com.xiaoxi</groupId>
<artifactId>common-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>xxx-service</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.xiaoxi</groupId>
<artifactId>xxx-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.example</groupId>
<artifactId>app-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>



<!-- 通过maven的profile来 动态修改spring的profile 达到多套配置切换使用的效果 -->
<profiles>
<profile>
<!-- 本地环境 -->
<id>local</id>
<properties>
<profiles.active>local</profiles.active>
<skipDocker>true</skipDocker>
<dockerHost></dockerHost>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 开发环境 -->
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
<skipDocker>true</skipDocker>
<dockerHost>http://192.168.148.236:2375</dockerHost>
</properties>
</profile>
<profile>
<!-- 测试环境 -->
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
<skipDocker>false</skipDocker>
<dockerHost>http://192.168.148.235:2375</dockerHost>
</properties>
</profile>
<profile>
<!-- 生产环境 -->
<id>prod</id>
<properties>
<profiles.active>prod</profiles.active>
<skipDocker>false</skipDocker>
<dockerHost>http://xxxx:2375</dockerHost>
</properties>
</profile>
</profiles>

<!-- maven插件发布到docker中 -->
<build>
<!-- 引用我们的项目名字 -->
<finalName>${project.artifactId}</finalName>

<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>


</plugins>

</build>

<repositories>
<repository>
<id>maven_xiaoxi</id>
<url>http://192.168.148.237:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</releases>
<snapshots>
<updatePolicy>always</updatePolicy>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

</project>

其中,parent、dependencies、profiles、build、repositories不能缺少

  • dependencies:如果有interface的,需要依赖interface
  • profiles:主要控制当前项目,引用那个yml文件

第三步(引入MybatisPlus-generator、yml)

MybatisPlus-generator的使用参考仓库代码Mybatis-Plus自动生成模板

yml配置,参考wiki的(yml相关)

第四步 xxx-interface代码编写

写相应接口,但是需要打包到私服(生命周期clean,然后deploy)

第五步 xxx-service代码编写

  1. 编写异常拦截器,继承固定的类

    1
    2
    3
    4
    5
    6
    @ControllerAdvice
    @ResponseBody
    public class ExceptionHandler extends BaseExceptionHandler {


    }
  2. 编写Swagger配置类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Import;
    import org.springframework.core.annotation.Order;
    import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.service.Contact;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;


    @Configuration
    @EnableSwagger2WebMvc
    @EnableKnife4j
    @Import(BeanValidatorPluginsConfiguration.class)
    public class SwaggerConfig {
    @Bean(value = "xxxApi")
    @Order(value = 1)
    public Docket groupProjectRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
    .apiInfo(groupApiInfo())
    .select()
    .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
    .paths(PathSelectors.any())
    .build();
    }


    private ApiInfo groupApiInfo() {
    Contact contact = new Contact("contract", null, "");
    return new ApiInfoBuilder()
    .title("AppRanking-Demo管理-xxx服务")
    .description("<div style='font-size:14px;color:red;'>AppRanking-Demo管理-xxx服务</div>")
    .termsOfServiceUrl("http://www.group.com/")
    .contact(contact)
    .version("1.0")
    .build();
    }
    }
  3. 编写MetaObjectHandler配置类

1
2
3
@Component
public class MetaObjectHandler extends BaseMetaObjectHandler {
}
  1. 编写MybatisPlusConfig配置类
1
2
3
@Configuration
public class MybatisPlusConfig extends BaseMybatisPlusConfig {
}
  1. 编写XxxApplication启动类
1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableFeignClients
public class XxxxApplication {
public static void main(String[] args) {
SpringApplication.run(XxxxApplication.class, args);
}

}