SpringBoot+SpringCloud+SpringCloudAlibaba【01】:父项目搭建
※、开发工具版本
(1)windows版本:win10
(2)IntelliJ IDEA版本:2021.2.3
(3)JDK:jdk-8u301
(4)maven版本:3.8.3
※、maven依赖版本
(1)springBoot依赖版本:2.4.6
(2)SpringCloud依赖版本:2020.0.4
(3)SpringCloudAlibaba依赖版本:2.2.6.RELEASE
※、新建父工程
※、pom文件引入依赖
<?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>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.4.6version> parent> <modelVersion>4.0.0modelVersion> <groupId>com.noiregroupId> <artifactId>noire-guardian-zeroartifactId> <version>1.0-SNAPSHOTversion> <packaging>pompackaging> <modules> <module>spring-bootmodule> modules> <properties> <maven.compiler.source>8maven.compiler.source> <maven.compiler.target>8maven.compiler.target> <spring.clound.version>2020.0.4spring.clound.version> <alibaba.cloud.version>2.2.6.RELEASEalibaba.cloud.version> properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-dependenciesartifactId> <version>${spring.clound.version}version> <type>pomtype> <scope>importscope> dependency> <dependency> <groupId>com.alibaba.cloudgroupId> <artifactId>spring-cloud-alibaba-dependenciesartifactId> <version>${alibaba.cloud.version}version> <type>pomtype> <scope>importscope> dependency> dependencies> dependencyManagement> <dependencies> <dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.pluginsgroupId> <artifactId>maven-compiler-pluginartifactId> <configuration> <source>1.8source> <target>1.8target> <encoding>UTF-8encoding> configuration> plugin> <plugin> <groupId>org.apache.maven.pluginsgroupId> <artifactId>maven-surefire-pluginartifactId> <configuration> <skipTests>trueskipTests> configuration> plugin> plugins> <resources> <resource> <directory>src/main/resourcesdirectory> <filtering>truefiltering> resource> <resource> <directory>src/main/javadirectory> <includes> <include>**/*.xmlinclude> includes> resource> resources> build> project>※、添加SpringBoot项目测试运行是否成功 (1)新建spring-boot模块,以后spring-boot相关的内容都会放在此模块里
(2)接着在spring-boot模块里再建一个子模块,用于启动测试,方法和上面一样,名字为:spring-boot-start,目录结构如下
(3)添加启动类和测试方法,目录结构如下 (4)Hello控制器代码package com.boot.start.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * Hello 控制器 * * @Author: Noire * @Date: 2021/11/5 23:10 */ @RestController public class HelloController { @GetMapping(value = "/hello") public String testHello() { return "hello Spring Boot!"; } }(5)启动类代码
package com.boot.start; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.Environment; import java.net.InetAddress; import java.net.UnknownHostException; /** * 项目启动类 * * @Author: Noire * @Date: 2021/11/5 23:10 */ @Slf4j @SpringBootApplication public class BootStartApplication { public static void main(String[] args) throws UnknownHostException { ConfigurableApplicationContext application = SpringApplication.run(BootStartApplication.class, args); Environment env = application.getEnvironment(); String ip = InetAddress.getLocalHost().getHostAddress(); String port = env.getProperty("server.port"); log.info("\n----------------------------------------------------------\n\t" + "Application init is running! Access URLs:\n\t" + "Local: \t\thttp://localhost:" + port + "/\n\t" + "External: \thttp://" + ip + ":" + port + "/\n\t" + "----------------------------------------------------------"); } }(6)application.yml配置文件代码
spring:
profiles:
active: dev
(7)application-dev.yml配置文件代码
#设置端口号
server:
port: 60000
(8)pom文件内容
<?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>spring-bootartifactId> <groupId>com.noiregroupId> <version>1.0-SNAPSHOTversion> parent> <modelVersion>4.0.0modelVersion> <artifactId>spring-boot-startartifactId> <version>1.0.1version> <properties> <maven.compiler.source>8maven.compiler.source> <maven.compiler.target>8maven.compiler.target> properties> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> project>
(9)启动项目访问:http://ip地址/hello,如:http://192.168.1.4:60000/hello
※、源码:https://gitee.com/www_xiaochu_com/noire-guardian-zero