-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e02edc2
commit 98d02e5
Showing
12 changed files
with
365 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
joylive-demo/joylive-demo-springcloud2021/joylive-demo-springcloud2021-provider-war/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
48 changes: 48 additions & 0 deletions
48
.../main/java/com/jd/live/agent/demo/springcloud/v2021/provider/SpringCloud2021Provider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>"; | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
...java/com/jd/live/agent/demo/springcloud/v2021/provider/aspect/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...ar/src/main/java/com/jd/live/agent/demo/springcloud/v2021/provider/config/EchoConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...ain/java/com/jd/live/agent/demo/springcloud/v2021/provider/controller/EchoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...d2021/joylive-demo-springcloud2021-provider-war/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
10 changes: 10 additions & 0 deletions
10
...gcloud2021/joylive-demo-springcloud2021-provider-war/src/main/resources/nacos-logback.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
1 change: 1 addition & 0 deletions
1
...gcloud2021/joylive-demo-springcloud2021-provider-war/src/main/resources/static/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!doctype html><html lang="en"><body>hello world</body></html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.