From 98d02e5c3867c6486cd49927af626597b2a74270 Mon Sep 17 00:00:00 2001 From: hexiaofeng Date: Thu, 16 Jan 2025 11:31:34 +0800 Subject: [PATCH] Add tomcat war demo --- .../bootstrap/env/AbstractEnvSupplier.java | 14 ++- .../pom.xml | 83 +++++++++++++++++ .../provider/SpringCloud2021Provider.java | 48 ++++++++++ .../aspect/GlobalExceptionHandler.java | 45 ++++++++++ .../v2021/provider/config/EchoConfig.java | 44 +++++++++ .../provider/controller/EchoController.java | 90 +++++++++++++++++++ .../src/main/resources/application.properties | 23 +++++ .../src/main/resources/nacos-logback.xml | 10 +++ .../src/main/resources/static/index.html | 1 + .../joylive-demo-springcloud2021/pom.xml | 1 + .../config/nacos/NacosConfigService.java | 4 +- .../META-INF/MANIFEST.MF | 4 + 12 files changed, 365 insertions(+), 2 deletions(-) create mode 100644 joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/pom.xml create mode 100644 joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/java/com/jd/live/agent/demo/springcloud/v2021/provider/SpringCloud2021Provider.java create mode 100644 joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/java/com/jd/live/agent/demo/springcloud/v2021/provider/aspect/GlobalExceptionHandler.java create mode 100644 joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/java/com/jd/live/agent/demo/springcloud/v2021/provider/config/EchoConfig.java create mode 100644 joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/java/com/jd/live/agent/demo/springcloud/v2021/provider/controller/EchoController.java create mode 100755 joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/resources/application.properties create mode 100644 joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/resources/nacos-logback.xml create mode 100644 joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/resources/static/index.html create mode 100644 out/artifacts/joylive_demo_springcloud2021_provider_war_war_exploded/META-INF/MANIFEST.MF diff --git a/joylive-core/joylive-core-framework/src/main/java/com/jd/live/agent/core/bootstrap/env/AbstractEnvSupplier.java b/joylive-core/joylive-core-framework/src/main/java/com/jd/live/agent/core/bootstrap/env/AbstractEnvSupplier.java index 8c1c51e75..cf1a40a33 100644 --- a/joylive-core/joylive-core-framework/src/main/java/com/jd/live/agent/core/bootstrap/env/AbstractEnvSupplier.java +++ b/joylive-core/joylive-core-framework/src/main/java/com/jd/live/agent/core/bootstrap/env/AbstractEnvSupplier.java @@ -89,7 +89,7 @@ protected Map loadConfigs(String resource, String prefix) { String ext = pos > 0 ? resource.substring(pos + 1) : ""; ConfigParser parser = parsers.get(ext); if (parser != null) { - URL url = ClassLoader.getSystemClassLoader().getResource(resource); + URL url = getResource(resource); if (url != null) { try { Map result = parse(url.openStream(), parser); @@ -104,6 +104,18 @@ protected Map loadConfigs(String resource, String prefix) { return null; } + protected URL getResource(String resource) { + ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + URL url = contextClassLoader.getResource(resource); + if (url == null) { + ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); + if (systemClassLoader != contextClassLoader) { + url = systemClassLoader.getResource(resource); + } + } + return url; + } + /** * Parses the input stream using the specified parser. * diff --git a/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/pom.xml b/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/pom.xml new file mode 100644 index 000000000..bfcc6c7a6 --- /dev/null +++ b/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/pom.xml @@ -0,0 +1,83 @@ + + + 4.0.0 + + com.jd.live + joylive-demo-springcloud2021 + ${revision} + + + war + + joylive-demo-springcloud2021-provider-war + + + + + com.jd.live + joylive-demo-api + ${revision} + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-tomcat + provided + + + + org.springframework.boot + spring-boot-starter-web + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + org.springframework.boot + spring-boot-starter-actuator + + + + com.fasterxml.jackson.core + jackson-databind + + + com.fasterxml.jackson.core + jackson-core + + + com.fasterxml.jackson.core + jackson-annotations + + + + + ${basedir}/target + joylive-demo-springcloud2021-provider + + + org.springframework.boot + spring-boot-maven-plugin + + com.jd.live.agent.demo.springcloud.v2021.provider.SpringCloud2021Provider + + + + org.apache.maven.plugins + maven-war-plugin + 3.4.0 + + + + + \ No newline at end of file diff --git a/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/java/com/jd/live/agent/demo/springcloud/v2021/provider/SpringCloud2021Provider.java b/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/java/com/jd/live/agent/demo/springcloud/v2021/provider/SpringCloud2021Provider.java new file mode 100644 index 000000000..c167d1980 --- /dev/null +++ b/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/java/com/jd/live/agent/demo/springcloud/v2021/provider/SpringCloud2021Provider.java @@ -0,0 +1,48 @@ +/* + * Copyright © ${year} ${owner} (${email}) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jd.live.agent.demo.springcloud.v2021.provider; + +import com.alibaba.nacos.client.config.utils.SnapShotSwitch; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +@EnableDiscoveryClient +@SpringBootApplication +public class SpringCloud2021Provider extends SpringBootServletInitializer { + + public static void main(String[] args) { + SnapShotSwitch.setIsSnapShot(false); + SpringApplication.run(SpringCloud2021Provider.class, args); + } + + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { + return builder.sources(SpringCloud2021Provider.class); + } + + @RequestMapping(value = "/", method = RequestMethod.GET) + @ResponseBody + Object index() { + return "

service-provider-2021

"; + } + +} diff --git a/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/java/com/jd/live/agent/demo/springcloud/v2021/provider/aspect/GlobalExceptionHandler.java b/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/java/com/jd/live/agent/demo/springcloud/v2021/provider/aspect/GlobalExceptionHandler.java new file mode 100644 index 000000000..f5c7a61d4 --- /dev/null +++ b/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/java/com/jd/live/agent/demo/springcloud/v2021/provider/aspect/GlobalExceptionHandler.java @@ -0,0 +1,45 @@ +/* + * Copyright © ${year} ${owner} (${email}) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jd.live.agent.demo.springcloud.v2021.provider.aspect; + +import com.jd.live.agent.demo.response.LiveLocation; +import com.jd.live.agent.demo.response.LiveResponse; +import com.jd.live.agent.demo.response.LiveTrace; +import com.jd.live.agent.demo.response.LiveTransmission; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import javax.servlet.http.HttpServletRequest; + +@RestControllerAdvice +public class GlobalExceptionHandler { + + @Value("${spring.application.name}") + private String applicationName; + + @ExceptionHandler(Exception.class) + @ResponseStatus(HttpStatus.OK) + public LiveResponse handleException(Exception e, HttpServletRequest request) { + LiveResponse response = new LiveResponse(500, "Internal Server Error: " + e.getMessage()); + response.addFirst(new LiveTrace(applicationName, LiveLocation.build(), + LiveTransmission.build("header", request::getHeader))); + return response; + } + +} \ No newline at end of file diff --git a/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/java/com/jd/live/agent/demo/springcloud/v2021/provider/config/EchoConfig.java b/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/java/com/jd/live/agent/demo/springcloud/v2021/provider/config/EchoConfig.java new file mode 100644 index 000000000..b94c900a6 --- /dev/null +++ b/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/java/com/jd/live/agent/demo/springcloud/v2021/provider/config/EchoConfig.java @@ -0,0 +1,44 @@ +/* + * Copyright © ${year} ${owner} (${email}) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jd.live.agent.demo.springcloud.v2021.provider.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConfigurationProperties(prefix = "echo") +public class EchoConfig { + + private int sleepTime; + + private int randomTime = 1000; + + public int getSleepTime() { + return sleepTime; + } + + public void setSleepTime(int sleepTime) { + this.sleepTime = sleepTime; + } + + public int getRandomTime() { + return randomTime; + } + + public void setRandomTime(int randomTime) { + this.randomTime = randomTime; + } +} diff --git a/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/java/com/jd/live/agent/demo/springcloud/v2021/provider/controller/EchoController.java b/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/java/com/jd/live/agent/demo/springcloud/v2021/provider/controller/EchoController.java new file mode 100644 index 000000000..5226c34a6 --- /dev/null +++ b/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/java/com/jd/live/agent/demo/springcloud/v2021/provider/controller/EchoController.java @@ -0,0 +1,90 @@ +/* + * Copyright © ${year} ${owner} (${email}) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jd.live.agent.demo.springcloud.v2021.provider.controller; + +import com.jd.live.agent.demo.response.LiveLocation; +import com.jd.live.agent.demo.response.LiveResponse; +import com.jd.live.agent.demo.response.LiveTrace; +import com.jd.live.agent.demo.response.LiveTransmission; +import com.jd.live.agent.demo.springcloud.v2021.provider.config.EchoConfig; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.concurrent.ThreadLocalRandom; + +@RestController +public class EchoController { + + private final String applicationName; + + private final EchoConfig config; + + @Value("${echo.suffix}") + private String echoSuffix; + + public EchoController(@Value("${spring.application.name}") String applicationName, EchoConfig config) { + this.applicationName = applicationName; + this.config = config; + } + + @GetMapping("/echo/{str}") + public LiveResponse echo(@PathVariable String str, HttpServletRequest request) { + int sleepTime = config.getSleepTime(); + if (sleepTime > 0) { + if (config.getRandomTime() > 0) { + sleepTime = sleepTime + ThreadLocalRandom.current().nextInt(config.getRandomTime()); + } + try { + Thread.sleep(sleepTime); + } catch (InterruptedException ignore) { + } + } + LiveResponse response = new LiveResponse(echoSuffix == null ? str : str + echoSuffix); + configure(request, response); + return response; + } + + @RequestMapping(value = "/status/{code}", method = {RequestMethod.GET, RequestMethod.PUT, RequestMethod.POST}) + public LiveResponse status(@PathVariable int code, HttpServletRequest request, HttpServletResponse response) { + response.setStatus(code); + LiveResponse lr = new LiveResponse(code, null, code); + configure(request, lr); + return lr; + } + + @RequestMapping(value = "/sleep/{millis}", method = {RequestMethod.GET, RequestMethod.PUT, RequestMethod.POST}) + public LiveResponse sleep(@PathVariable int millis, HttpServletRequest request, HttpServletResponse response) throws InterruptedException { + if (millis > 0) { + Thread.sleep(millis); + } + LiveResponse lr = new LiveResponse(200, null, millis); + configure(request, lr); + return lr; + } + + @RequestMapping(value = "/exception", method = {RequestMethod.GET, RequestMethod.PUT, RequestMethod.POST}) + public LiveResponse exception(HttpServletRequest request, HttpServletResponse response) { + throw new RuntimeException("RuntimeException happened!"); + } + + private void configure(HttpServletRequest request, LiveResponse response) { + response.addFirst(new LiveTrace(applicationName, LiveLocation.build(), + LiveTransmission.build("header", request::getHeader))); + } + +} diff --git a/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/resources/application.properties b/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/resources/application.properties new file mode 100755 index 000000000..04cbc6d99 --- /dev/null +++ b/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/resources/application.properties @@ -0,0 +1,23 @@ +server.port=${SERVER_PORT:${random.int[11000,11999]}} +spring.application.name=service-provider +spring.cloud.nacos.discovery.server-addr=${NACOS_ADDR:${AP_SD_INTERNAL_ADDRESS}:${AP_SD_INTERNAL_HTTP_PORT}} +spring.cloud.nacos.discovery.enabled=true +#spring.cloud.nacos.discovery.instance-enabled=true +#register IPv6 +#spring.cloud.nacos.discovery.ip-type=IPv6 +#spring.cloud.nacos.discovery.ip=::1 +spring.cloud.nacos.username=${NACOS_USERNAME:nacos} +spring.cloud.nacos.password=${NACOS_PASSWORD:nacos} +spring.cloud.nacos.discovery.namespace=${NACOS_NAMESPACE:public} +spring.cloud.nacos.discovery.username=${NACOS_USERNAME:nacos} +spring.cloud.nacos.discovery.password=${NACOS_PASSWORD:nacos} +spring.jackson.serialization.indent_output=true +spring.jackson.date-format=yyyy-MM-dd'T'HH:mm:ss.SSSZ +spring.jackson.default-property-inclusion=non_null +logging.level.root=${LIVE_LOG_LEVEL:INFO} +management.endpoints.web.exposure.include=* +management.endpoint.health.show-details=always +spring.web.resources.static-locations=classpath:/static/ + +echo.sleepTime=${RESPONSE_WAIT_TIME:2000} +echo.suffix= diff --git a/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/resources/nacos-logback.xml b/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/resources/nacos-logback.xml new file mode 100644 index 000000000..8fc27e166 --- /dev/null +++ b/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/resources/nacos-logback.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/resources/static/index.html b/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/resources/static/index.html new file mode 100644 index 000000000..a4e5dfffd --- /dev/null +++ b/joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/src/main/resources/static/index.html @@ -0,0 +1 @@ +hello world \ No newline at end of file diff --git a/joylive-demo/joylive-demo-springcloud2021/pom.xml b/joylive-demo/joylive-demo-springcloud2021/pom.xml index 0d925b6cb..c5196ddff 100644 --- a/joylive-demo/joylive-demo-springcloud2021/pom.xml +++ b/joylive-demo/joylive-demo-springcloud2021/pom.xml @@ -23,6 +23,7 @@ joylive-demo-springcloud2021-provider + joylive-demo-springcloud2021-provider-war joylive-demo-springcloud2021-provider-reactive joylive-demo-springcloud2021-consumer joylive-demo-springcloud2021-gateway diff --git a/joylive-implement/joylive-service/joylive-service-nacos/src/main/java/com/jd/live/agent/implement/service/config/nacos/NacosConfigService.java b/joylive-implement/joylive-service/joylive-service-nacos/src/main/java/com/jd/live/agent/implement/service/config/nacos/NacosConfigService.java index 6ae6b72c5..1de2cc6ce 100644 --- a/joylive-implement/joylive-service/joylive-service-nacos/src/main/java/com/jd/live/agent/implement/service/config/nacos/NacosConfigService.java +++ b/joylive-implement/joylive-service/joylive-service-nacos/src/main/java/com/jd/live/agent/implement/service/config/nacos/NacosConfigService.java @@ -86,7 +86,9 @@ protected CompletableFuture doStart() { client = NacosClientFactory.create(properties); client.connect(); configurator = getConfigurator(configName); - configurator.subscribe(); + if (configurator != null) { + configurator.subscribe(); + } return CompletableFuture.completedFuture(null); } catch (Throwable e) { return Futures.future(e); diff --git a/out/artifacts/joylive_demo_springcloud2021_provider_war_war_exploded/META-INF/MANIFEST.MF b/out/artifacts/joylive_demo_springcloud2021_provider_war_war_exploded/META-INF/MANIFEST.MF new file mode 100644 index 000000000..2a634d69b --- /dev/null +++ b/out/artifacts/joylive_demo_springcloud2021_provider_war_war_exploded/META-INF/MANIFEST.MF @@ -0,0 +1,4 @@ +Manifest-Version: 1.0 +Created-By: IntelliJ IDEA +Built-By: hexiaofeng +Build-Jdk: Azul Zulu 17.0.11 - aarch64