From 6684f9d0a54d52b7a6aad1f465922006329fe5d5 Mon Sep 17 00:00:00 2001 From: xuwujing <1060589146@qq.com> Date: Sat, 27 Jan 2024 14:23:52 +0800 Subject: [PATCH] add springboot-nacos --- springboot-nacos/.gitignore | 1 + springboot-nacos/pom.xml | 87 +++++++++++++++++++ .../src/main/java/com/pancm/App.java | 28 ++++++ .../java/com/pancm/web/ConfigController.java | 42 +++++++++ .../com/pancm/web/DiscoveryController.java | 26 ++++++ .../src/main/resources/bootstrap.properties | 28 ++++++ .../src/main/resources/logback.xml | 32 +++++++ .../src/main/resources/pcm.properties | 1 + 8 files changed, 245 insertions(+) create mode 100644 springboot-nacos/.gitignore create mode 100644 springboot-nacos/pom.xml create mode 100644 springboot-nacos/src/main/java/com/pancm/App.java create mode 100644 springboot-nacos/src/main/java/com/pancm/web/ConfigController.java create mode 100644 springboot-nacos/src/main/java/com/pancm/web/DiscoveryController.java create mode 100644 springboot-nacos/src/main/resources/bootstrap.properties create mode 100644 springboot-nacos/src/main/resources/logback.xml create mode 100644 springboot-nacos/src/main/resources/pcm.properties diff --git a/springboot-nacos/.gitignore b/springboot-nacos/.gitignore new file mode 100644 index 0000000..b83d222 --- /dev/null +++ b/springboot-nacos/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/springboot-nacos/pom.xml b/springboot-nacos/pom.xml new file mode 100644 index 0000000..0ae10f0 --- /dev/null +++ b/springboot-nacos/pom.xml @@ -0,0 +1,87 @@ + + 4.0.0 + + 1.0.0 + springboot-nacos + 0.0.1-SNAPSHOT + jar + + springboot-nacos + http://maven.apache.org + + + UTF-8 + UTF-8 + 1.8 + 5.1.44 + 1.2.3 + 1.7.25 + 1.8 + 1.8 + + + + org.springframework.boot + spring-boot-starter-parent + 2.2.1.RELEASE + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + 2.2.1.RELEASE + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + 2.2.1.RELEASE + + + + + + org.slf4j + slf4j-api + ${slf4j} + + + + + ch.qos.logback + logback-classic + ${logback} + + + + ch.qos.logback + logback-core + ${logback} + + + + + + + + + + + + + diff --git a/springboot-nacos/src/main/java/com/pancm/App.java b/springboot-nacos/src/main/java/com/pancm/App.java new file mode 100644 index 0000000..bd96876 --- /dev/null +++ b/springboot-nacos/src/main/java/com/pancm/App.java @@ -0,0 +1,28 @@ +package com.pancm; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + + +/** + * @Author pancm + * @Description springboot集成nacos配置中心 + * @Date 2024/1/26 + * @Param + * @return + **/ +@SpringBootApplication +@EnableDiscoveryClient +public class App +{ + private static final Logger logger = LoggerFactory.getLogger(App.class); + public static void main( String[] args ) + { + // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件 + SpringApplication.run(App.class, args); + logger.info("程序启动成功!"); + } +} diff --git a/springboot-nacos/src/main/java/com/pancm/web/ConfigController.java b/springboot-nacos/src/main/java/com/pancm/web/ConfigController.java new file mode 100644 index 0000000..166de74 --- /dev/null +++ b/springboot-nacos/src/main/java/com/pancm/web/ConfigController.java @@ -0,0 +1,42 @@ +package com.pancm.web; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import static org.springframework.web.bind.annotation.RequestMethod.GET; + +/** + * @Author pancm + * @Description 官方示例文档 + * @Date 2024/1/26 + * @Param + * @return + **/ +@RestController +@RequestMapping("config") +@RefreshScope +public class ConfigController { + + + + @Value("${pcm.name:pcm-1001}") + private String name; + + @Value("${pcm.age:18}") + private String age; + + + @RequestMapping(value = "/getAge", method = GET) + public String getAge() { + return age; + } + + @RequestMapping(value = "/getName", method = GET) + public String getName() { + return name; + } + + +} \ No newline at end of file diff --git a/springboot-nacos/src/main/java/com/pancm/web/DiscoveryController.java b/springboot-nacos/src/main/java/com/pancm/web/DiscoveryController.java new file mode 100644 index 0000000..1f823c6 --- /dev/null +++ b/springboot-nacos/src/main/java/com/pancm/web/DiscoveryController.java @@ -0,0 +1,26 @@ +package com.pancm.web; + +import com.alibaba.nacos.api.annotation.NacosInjected; +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.naming.NamingService; +import com.alibaba.nacos.api.naming.pojo.Instance; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +import static org.springframework.web.bind.annotation.RequestMethod.GET; + +@RestController +@RequestMapping("discovery") +public class DiscoveryController { + + @NacosInjected + private NamingService namingService; + + @RequestMapping(value = "/get", method = GET) + public List get(@RequestParam String serviceName) throws NacosException { + return namingService.getAllInstances(serviceName); + } +} \ No newline at end of file diff --git a/springboot-nacos/src/main/resources/bootstrap.properties b/springboot-nacos/src/main/resources/bootstrap.properties new file mode 100644 index 0000000..1b063f6 --- /dev/null +++ b/springboot-nacos/src/main/resources/bootstrap.properties @@ -0,0 +1,28 @@ +spring.banner.charset=UTF-8 +server.tomcat.uri-encoding=UTF-8 +spring.http.encoding.charset=UTF-8 +spring.http.encoding.enabled=true +spring.http.encoding.force=true +spring.messages.encoding=UTF-8 +server.port=8299 +spring.application.name=springboot-nacos +spring.profiles.active=dev + + +# nacos?? +nacos.discovery.autoRegister=true +spring.cloud.nacos.discovery.server-addr= http://127.0.0.1:8848 +spring.cloud.nacos.config.server-addr= http://127.0.0.1:8848 +spring.cloud.nacos.config.namespace= pcm-namespace--id +spring.cloud.nacos.config.group= dev +spring.cloud.nacos.config.fileExtension= properties + + +# ?????? +##???????????dataId +spring.cloud.nacos.config.extension-configs[0].data-id=pcm-dev.properties +##????????????? +spring.cloud.nacos.config.extension-configs[0].group=dev + + + diff --git a/springboot-nacos/src/main/resources/logback.xml b/springboot-nacos/src/main/resources/logback.xml new file mode 100644 index 0000000..583ef76 --- /dev/null +++ b/springboot-nacos/src/main/resources/logback.xml @@ -0,0 +1,32 @@ + + + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + + + + + + + ${LOG_HOME}/mylog-%d{yyyy-MM-dd}.%i.txt + + 10MB + 31 + 10GB + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + + + + + + + + + + \ No newline at end of file diff --git a/springboot-nacos/src/main/resources/pcm.properties b/springboot-nacos/src/main/resources/pcm.properties new file mode 100644 index 0000000..89f7155 --- /dev/null +++ b/springboot-nacos/src/main/resources/pcm.properties @@ -0,0 +1 @@ +pcm.age = 24 \ No newline at end of file