From e0c63ab31f185d65ee5521fd1e2d5eb040efdb4a Mon Sep 17 00:00:00 2001 From: YunaiV <> Date: Sun, 2 Feb 2020 00:36:20 +0800 Subject: [PATCH] =?UTF-8?q?spring=20boot=20=E8=87=AA=E5=8A=A8=E5=8C=96?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 +++---- lab-47/lab-47-demo/pom.xml | 22 +++++++++++++ .../lab47/demo/DemoApplication.java | 13 ++++++++ lab-47/pom.xml | 20 ++++++++++++ .../yunai-server-spring-boot-starter/pom.xml | 22 +++++++++++++ .../YunaiServerAutoConfiguration.java | 32 +++++++++++++++++++ .../autoconfigure/YunaiServerProperties.java | 31 ++++++++++++++++++ .../main/resources/META-INF/spring.factories | 2 ++ pom.xml | 1 + 9 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 lab-47/lab-47-demo/pom.xml create mode 100644 lab-47/lab-47-demo/src/main/java/cn/iocoder/springboot/lab47/demo/DemoApplication.java create mode 100644 lab-47/pom.xml create mode 100644 lab-47/yunai-server-spring-boot-starter/pom.xml create mode 100644 lab-47/yunai-server-spring-boot-starter/src/main/java/cn/iocoder/springboot/lab47/yunaiserver/autoconfigure/YunaiServerAutoConfiguration.java create mode 100644 lab-47/yunai-server-spring-boot-starter/src/main/java/cn/iocoder/springboot/lab47/yunaiserver/autoconfigure/YunaiServerProperties.java create mode 100644 lab-47/yunai-server-spring-boot-starter/src/main/resources/META-INF/spring.factories diff --git a/README.md b/README.md index 2b17e35d..d2e8a6e8 100644 --- a/README.md +++ b/README.md @@ -65,13 +65,13 @@ ## 配置中心 -* [《芋道 Spring Boot 配置文件》](http://www.iocoder.cn/Spring-Boot/config-file/?github) 对应 [lab-43](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-43) 。 -* [《芋道 Spring Boot 配置中心 Apollo》](http://www.iocoder.cn/Spring-Boot/config-apollo/?github) 对应 [lab-45](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-45) 。 -* [《芋道 Spring Boot 配置中心 Nacos》](http://www.iocoder.cn/Spring-Boot/config-nacos/?github) 对应 [lab-44](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-44) 。 +* [《芋道 Spring Boot 配置文件入门》](http://www.iocoder.cn/Spring-Boot/config-file/?github) 对应 [lab-43](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-43) 。 +* [《芋道 Spring Boot 配置中心 Apollo 入门》](http://www.iocoder.cn/Spring-Boot/config-apollo/?github) 对应 [lab-45](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-45) 。 +* [《芋道 Spring Boot 配置中心 Nacos 入门》](http://www.iocoder.cn/Spring-Boot/config-nacos/?github) 对应 [lab-44](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-44) 。 ## 注册中心 -* [《芋道 Spring Boot 注册中心 Nacos》](http://www.iocoder.cn/Spring-Boot/registry-nacos/?github) 对应 [lab-44](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-44) 。 +* [《芋道 Spring Boot 注册中心 Nacos 入门》](http://www.iocoder.cn/Spring-Boot/registry-nacos/?github) 对应 [lab-44](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-44) 。 ## 持续交付 @@ -81,8 +81,8 @@ ## 服务容错 -* [《芋道 Spring Boot 服务容错 Sentinel》](http://www.iocoder.cn/Spring-Boot/Sentinel/?github) 对应 [lab-46](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-46) 。 -* [《芋道 Spring Boot 服务容错 Hystrix》]()计划中... +* [《芋道 Spring Boot 服务容错 Sentinel 入门》](http://www.iocoder.cn/Spring-Boot/Sentinel/?github) 对应 [lab-46](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-46) 。 +* 《芋道 Spring Boot 服务容错 Hystrix 入门》计划中... ## 监控管理 diff --git a/lab-47/lab-47-demo/pom.xml b/lab-47/lab-47-demo/pom.xml new file mode 100644 index 00000000..63d63dd9 --- /dev/null +++ b/lab-47/lab-47-demo/pom.xml @@ -0,0 +1,22 @@ + + + + lab-47 + cn.iocoder.springboot.labs + 1.0-SNAPSHOT + + 4.0.0 + + lab-47-demo + + + + cn.iocoder.springboot.labs + yunai-server-spring-boot-starter + 1.0-SNAPSHOT + + + + diff --git a/lab-47/lab-47-demo/src/main/java/cn/iocoder/springboot/lab47/demo/DemoApplication.java b/lab-47/lab-47-demo/src/main/java/cn/iocoder/springboot/lab47/demo/DemoApplication.java new file mode 100644 index 00000000..b43bf568 --- /dev/null +++ b/lab-47/lab-47-demo/src/main/java/cn/iocoder/springboot/lab47/demo/DemoApplication.java @@ -0,0 +1,13 @@ +package cn.iocoder.springboot.lab47.demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DemoApplication { + + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } + +} diff --git a/lab-47/pom.xml b/lab-47/pom.xml new file mode 100644 index 00000000..df4e6693 --- /dev/null +++ b/lab-47/pom.xml @@ -0,0 +1,20 @@ + + + + labs-parent + cn.iocoder.springboot.labs + 1.0-SNAPSHOT + + 4.0.0 + + lab-47 + pom + + yunai-server-spring-boot-starter + lab-47-demo + + + + diff --git a/lab-47/yunai-server-spring-boot-starter/pom.xml b/lab-47/yunai-server-spring-boot-starter/pom.xml new file mode 100644 index 00000000..9f6b9fbe --- /dev/null +++ b/lab-47/yunai-server-spring-boot-starter/pom.xml @@ -0,0 +1,22 @@ + + + + lab-47 + cn.iocoder.springboot.labs + 1.0-SNAPSHOT + + 4.0.0 + + yunai-server-spring-boot-starter + + + + org.springframework.boot + spring-boot-starter + 2.2.2.RELEASE + + + + diff --git a/lab-47/yunai-server-spring-boot-starter/src/main/java/cn/iocoder/springboot/lab47/yunaiserver/autoconfigure/YunaiServerAutoConfiguration.java b/lab-47/yunai-server-spring-boot-starter/src/main/java/cn/iocoder/springboot/lab47/yunaiserver/autoconfigure/YunaiServerAutoConfiguration.java new file mode 100644 index 00000000..eafdb1dd --- /dev/null +++ b/lab-47/yunai-server-spring-boot-starter/src/main/java/cn/iocoder/springboot/lab47/yunaiserver/autoconfigure/YunaiServerAutoConfiguration.java @@ -0,0 +1,32 @@ +package cn.iocoder.springboot.lab47.yunaiserver.autoconfigure; + +import com.sun.net.httpserver.HttpServer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.io.IOException; +import java.net.InetSocketAddress; + +@Configuration +@EnableConfigurationProperties(YunaiServerProperties.class) +public class YunaiServerAutoConfiguration { + + private Logger logger = LoggerFactory.getLogger(getClass()); + + @Bean + @ConditionalOnClass(HttpServer.class) + public HttpServer httpServer(YunaiServerProperties serverProperties) throws IOException { + // 创建 HttpServer 对象,并启动 + HttpServer server = HttpServer.create(new InetSocketAddress(serverProperties.getPort()), 0); + server.start(); + logger.info("[httpServer][启动服务器成功,端口为:{}]", serverProperties.getPort()); + + // 返回 + return server; + } + +} diff --git a/lab-47/yunai-server-spring-boot-starter/src/main/java/cn/iocoder/springboot/lab47/yunaiserver/autoconfigure/YunaiServerProperties.java b/lab-47/yunai-server-spring-boot-starter/src/main/java/cn/iocoder/springboot/lab47/yunaiserver/autoconfigure/YunaiServerProperties.java new file mode 100644 index 00000000..a9ec84e7 --- /dev/null +++ b/lab-47/yunai-server-spring-boot-starter/src/main/java/cn/iocoder/springboot/lab47/yunaiserver/autoconfigure/YunaiServerProperties.java @@ -0,0 +1,31 @@ +package cn.iocoder.springboot.lab47.yunaiserver.autoconfigure; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "yunai.server") +public class YunaiServerProperties { + + /** + * 默认端口 + */ + private static final Integer DEFAULT_PORT = 8000; + + /** + * 端口 + */ + private Integer port = DEFAULT_PORT; + + public static Integer getDefaultPort() { + return DEFAULT_PORT; + } + + public Integer getPort() { + return port; + } + + public YunaiServerProperties setPort(Integer port) { + this.port = port; + return this; + } + +} diff --git a/lab-47/yunai-server-spring-boot-starter/src/main/resources/META-INF/spring.factories b/lab-47/yunai-server-spring-boot-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 00000000..f2a16e95 --- /dev/null +++ b/lab-47/yunai-server-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +cn.iocoder.springboot.lab47.yunaiserver.autoconfigure.YunaiServerAutoConfiguration diff --git a/pom.xml b/pom.xml index b51b6784..787a491a 100644 --- a/pom.xml +++ b/pom.xml @@ -55,6 +55,7 @@ lab-44 lab-45 lab-46 + lab-47