创建服务Maven模块
从git上拉取项目之后,创建自己的本地分支(git操作详见《git客户端工具基本配置》),在对项目进行coding。
1、在已导入到工作空间的项目中,找到 smartedu-projects
点击项目然后右键-》New -》Module-》maven -》然后直接下一步
2、创建 Maven Module,点击 “Next”
3、选择GroupId填写 “ArtifactId”,点击 “Next”
4、填写 “Module name”,点击 “Next”
5、packaging 类型为pom时,表示该模块可以继续分拆;打开pom.xml并修改父级模块信息
删除多余的 src
目录,并修改pom.xml
<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>smartedu-projects</artifactId>
<groupId>com.knowway.smartedu</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>smartedu-test</artifactId>
<packaging>pom</packaging>
</project>
需要继续添加子模块,比如创建一个 smartedu-test-admin
最终结构如下:
6、完成模块创建后,开始工程代码构建
6.1、修改pom.xml,添加Maven依赖,下面的代码主要注意 smartedu-core
依赖和 mainClass
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>smartedu-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- For Embed Tomcat -->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency> -->
<!-- For Embed Jetty -->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency> -->
<!-- For Embed Undertow -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
<mainClass>com.knowway.smartedu.test.admin.DemoAdminApplication</mainClass>
</configuration>
</plugin>
<!-- 第一步:生成可执行的启动脚本 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
</plugin>
<!-- 第二步:通过antrun插件拷贝文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
</plugin>
<!-- 第三步:生成离线文档 -->
<!-- http://blog.didispace.com/swagger2markup-asciidoc/ -->
<!-- First, use the swagger2markup plugin to generate asciidoc -->
<!-- <plugin> <groupId>io.github.swagger2markup</groupId> <artifactId>swagger2markup-maven-plugin</artifactId>
</plugin> -->
<!-- Run the generated asciidoc through Asciidoctor to generate other
documentation types, such as PDFs or HTML5 -->
<!-- <plugin> <groupId>org.asciidoctor</groupId> <artifactId>asciidoctor-maven-plugin</artifactId>
</plugin> -->
</plugins>
</build>
6.2、创建 mainClass,例如:DemoAdminApplication
在 src/main/java 目录上右键创建 package,如: com.knowway.smartedu.test.admin
在 com.knowway.smartedu.demo.admin 包下创建对象:DemoAdminApplication
参考已有其他项目修改 DemoAdminApplication
,最终结果如下:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.web.client.RestTemplate;
import com.knowway.cloud.autoconfigure.EnableServiceConfiguration;
import com.knowway.cloud.autoconfigure.EnableWebMvcConfiguration;
import com.spring4all.swagger.EnableSwagger2Doc;
import io.micrometer.core.instrument.MeterRegistry;
@EnableCaching(proxyTargetClass = true)
@EnableSwagger2Doc
@EnableFeignClients
@EnableServiceConfiguration
@EnableWebMvcConfiguration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@SpringCloudApplication
public class DemoAdminApplication implements CommandLineRunner {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
public MeterRegistryCustomizer<MeterRegistry> configurer(
@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.config().commonTags("application", applicationName);
}
public static void main(String[] args) {
SpringApplication.run(DemoAdminApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
System.err.println("Spring Cloud Application(Smartedu-Test-Admin) Started !");
}
}
6.3、从已有项目拷贝配置文件到 src/main/resources
目录
6.4、从DemoAdminApplication启动服务,当看到下面红色框中的信息时,则说明服务创建完成。
更新时间:2023-12-20 16:58