Skip to content

Commit

Permalink
Add tomcat war demo
Browse files Browse the repository at this point in the history
  • Loading branch information
hexiaofeng committed Jan 16, 2025
1 parent e02edc2 commit 98d02e5
Show file tree
Hide file tree
Showing 12 changed files with 365 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected Map<String, Object> 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<String, Object> result = parse(url.openStream(), parser);
Expand All @@ -104,6 +104,18 @@ protected Map<String, Object> 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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.jd.live</groupId>
<artifactId>joylive-demo-springcloud2021</artifactId>
<version>${revision}</version>
</parent>

<packaging>war</packaging>

<artifactId>joylive-demo-springcloud2021-provider-war</artifactId>

<dependencies>

<dependency>
<groupId>com.jd.live</groupId>
<artifactId>joylive-demo-api</artifactId>
<version>${revision}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
</dependencies>

<build>
<directory>${basedir}/target</directory>
<finalName>joylive-demo-springcloud2021-provider</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.jd.live.agent.demo.springcloud.v2021.provider.SpringCloud2021Provider</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.4.0</version>
</plugin>
</plugins>

</build>
</project>
Original file line number Diff line number Diff line change
@@ -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 "<h1>service-provider-2021</h1>";
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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)));
}

}
Original file line number Diff line number Diff line change
@@ -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=
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan="false">
<!-- Console log output -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender"/>

<!-- Level: TRACE < DEBUG < INFO < WARN < ERROR < FATAL -->
<root level="info">
<appender-ref ref="console"/>
</root>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!doctype html><html lang="en"><body>hello world</body></html>
1 change: 1 addition & 0 deletions joylive-demo/joylive-demo-springcloud2021/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

<modules>
<module>joylive-demo-springcloud2021-provider</module>
<module>joylive-demo-springcloud2021-provider-war</module>
<module>joylive-demo-springcloud2021-provider-reactive</module>
<module>joylive-demo-springcloud2021-consumer</module>
<module>joylive-demo-springcloud2021-gateway</module>
Expand Down
Loading

0 comments on commit 98d02e5

Please sign in to comment.