From ead5ee2cd328d42007135ae64273669b70fc0298 Mon Sep 17 00:00:00 2001
From: daizhenyu <1449308021@qq.com>
Date: Mon, 25 Nov 2024 16:35:31 +0800
Subject: [PATCH] update xds router demo
Signed-off-by: daizhenyu <1449308021@qq.com>
---
.../router-product/script/mysql.yaml | 41 ++++++++
.../router-product/script/spring-client.yaml | 2 +
.../spring-cloud-client-xds/pom.xml | 6 ++
.../client/SpringCloudClientApplication.java | 4 +
.../spring/client/SpringRouterController.java | 93 +++++++++++++------
.../xds/spring/client/config/MySqlConfig.java | 70 ++++++++++++++
.../client/config/RestTemplateConfig.java | 2 +
.../src/main/resources/application.yml | 7 +-
.../spring/server/RouterServerController.java | 22 ++++-
9 files changed, 218 insertions(+), 29 deletions(-)
create mode 100644 xds-router-demo/router-product/script/mysql.yaml
create mode 100644 xds-router-demo/spring-cloud-client-xds/src/main/java/io/sermant/demo/xds/spring/client/config/MySqlConfig.java
diff --git a/xds-router-demo/router-product/script/mysql.yaml b/xds-router-demo/router-product/script/mysql.yaml
new file mode 100644
index 0000000..98e4bc8
--- /dev/null
+++ b/xds-router-demo/router-product/script/mysql.yaml
@@ -0,0 +1,41 @@
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+ name: mysql
+ namespace: default
+spec:
+ replicas: 1
+ selector:
+ matchLabels:
+ app: mysql
+ template:
+ metadata:
+ labels:
+ app: mysql
+ spec:
+ containers:
+ - name: mysql
+ image: mysql:8.0.40
+ env:
+ # please fill in the password of the root user
+ - name: MYSQL_ROOT_PASSWORD
+ value: ""
+ ports:
+ - containerPort: 3306
+ securityContext:
+ privileged: true
+---
+apiVersion: v1
+kind: Service
+metadata:
+ name: mysql
+ namespace: default
+spec:
+ selector:
+ app: mysql
+ ports:
+ - protocol: TCP
+ port: 3306
+ targetPort: 3306
+ nodePort: 30036
+ type: NodePort
\ No newline at end of file
diff --git a/xds-router-demo/router-product/script/spring-client.yaml b/xds-router-demo/router-product/script/spring-client.yaml
index 4793666..612f4ee 100644
--- a/xds-router-demo/router-product/script/spring-client.yaml
+++ b/xds-router-demo/router-product/script/spring-client.yaml
@@ -27,6 +27,8 @@ spec:
value: "true"
- name: ZOOKEEPER_ENABLED
value: "false"
+ - name: MYSQL_ENABLED
+ value: "false"
- name: JAVA_TOOL_OPTIONS
value: "-javaagent:/home/agent/sermant-agent.jar"
imagePullSecrets:
diff --git a/xds-router-demo/spring-cloud-client-xds/pom.xml b/xds-router-demo/spring-cloud-client-xds/pom.xml
index 9ced2fc..c9d28d7 100644
--- a/xds-router-demo/spring-cloud-client-xds/pom.xml
+++ b/xds-router-demo/spring-cloud-client-xds/pom.xml
@@ -17,6 +17,7 @@
4.5.13
4.9.3
4.1.5
+ 3.3.2
@@ -47,6 +48,11 @@
httpasyncclient
${httpclient.async.version}
+
+ org.mariadb.jdbc
+ mariadb-java-client
+ ${mariadb.version}
+
diff --git a/xds-router-demo/spring-cloud-client-xds/src/main/java/io/sermant/demo/xds/spring/client/SpringCloudClientApplication.java b/xds-router-demo/spring-cloud-client-xds/src/main/java/io/sermant/demo/xds/spring/client/SpringCloudClientApplication.java
index 1e2e460..52fc5bd 100644
--- a/xds-router-demo/spring-cloud-client-xds/src/main/java/io/sermant/demo/xds/spring/client/SpringCloudClientApplication.java
+++ b/xds-router-demo/spring-cloud-client-xds/src/main/java/io/sermant/demo/xds/spring/client/SpringCloudClientApplication.java
@@ -16,8 +16,11 @@
package io.sermant.demo.xds.spring.client;
+import io.sermant.demo.xds.spring.client.config.MySqlConfig;
+
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
/**
* SpringCloudClientApplication
@@ -26,6 +29,7 @@
* @since 2024-09-23
**/
@SpringBootApplication
+@EnableConfigurationProperties(MySqlConfig.class)
public class SpringCloudClientApplication {
/**
* main
diff --git a/xds-router-demo/spring-cloud-client-xds/src/main/java/io/sermant/demo/xds/spring/client/SpringRouterController.java b/xds-router-demo/spring-cloud-client-xds/src/main/java/io/sermant/demo/xds/spring/client/SpringRouterController.java
index 31b3f57..4054c76 100644
--- a/xds-router-demo/spring-cloud-client-xds/src/main/java/io/sermant/demo/xds/spring/client/SpringRouterController.java
+++ b/xds-router-demo/spring-cloud-client-xds/src/main/java/io/sermant/demo/xds/spring/client/SpringRouterController.java
@@ -16,6 +16,7 @@
package io.sermant.demo.xds.spring.client;
+import io.sermant.demo.xds.spring.client.config.MySqlConfig;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import okhttp3.Request;
@@ -23,6 +24,7 @@
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
@@ -33,6 +35,7 @@
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
import org.apache.http.nio.reactor.IOReactorException;
import org.apache.http.util.EntityUtils;
+import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
@@ -47,6 +50,11 @@
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
@@ -59,7 +67,7 @@
**/
@RequestMapping("router")
@RestController
-public class SpringRouterController {
+public class SpringRouterController implements InitializingBean {
private static final String VERSION = "version";
private static final int SUCCEED_CODE = 200;
@@ -76,15 +84,26 @@ public class SpringRouterController {
private static final int KEEP_ALIVE_TIME = 10;
+ private static final int SLEEP_TIME = 5000;
+
+ private static final String DATABASE_TABLE_NAME = "table_test";
+
+ private static final int ITERATION_COUNT = 13000;
+
private static CloseableHttpClient httpClient;
private static CloseableHttpAsyncClient httpAsyncClient;
private static OkHttpClient okClient;
+ private Connection mySqlConnection;
+
@Autowired
private RestTemplate restTemplate;
+ @Autowired
+ private MySqlConfig mySqlConfig;
+
static {
System.setProperty("http.keepAlive", "true");
System.setProperty("http.maxConnections", "200");
@@ -139,15 +158,14 @@ public String checkStatus() {
*/
@RequestMapping("httpClient")
public String testHttpClientRouting(String host, String version) {
+ mockDataBase(DATABASE_TABLE_NAME);
String url = buildUrl(host);
- try {
- HttpGet request = new HttpGet(url);
- request.addHeader(VERSION, version);
- HttpResponse response = httpClient.execute(request);
- int statusCode = response.getStatusLine().getStatusCode();
- if (statusCode == HttpStatus.SC_OK) {
+ HttpGet request = new HttpGet(url);
+ request.addHeader(VERSION, version);
+ try (CloseableHttpResponse response = httpClient.execute(request)) {
+ if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity());
- mockRealLogic(result);
+ mockRealLogic();
return result;
} else {
return "";
@@ -166,6 +184,7 @@ public String testHttpClientRouting(String host, String version) {
*/
@RequestMapping("jdkHttp")
public String testJdkHttpRouting(String host, String version) {
+ mockDataBase(DATABASE_TABLE_NAME);
String url = buildUrl(host);
HttpURLConnection connection = null;
BufferedReader reader = null;
@@ -183,7 +202,7 @@ public String testJdkHttpRouting(String host, String version) {
response.append(line);
}
String result = response.toString();
- mockRealLogic(result);
+ mockRealLogic();
return result;
} else {
return "";
@@ -213,16 +232,16 @@ public String testJdkHttpRouting(String host, String version) {
*/
@RequestMapping("okHttp3")
public String testOkHttp3Routing(String host, String version) {
+ mockDataBase(DATABASE_TABLE_NAME);
String url = buildUrl(host);
Request request = new okhttp3.Request.Builder()
.url(url)
.addHeader(VERSION, version)
.build();
try (okhttp3.Response response = okClient.newCall(request).execute()) {
- int statusCode = response.code();
- if (statusCode == SUCCEED_CODE) {
+ if (response.code() == SUCCEED_CODE) {
String result = response.body().string();
- mockRealLogic(result);
+ mockRealLogic();
return result;
} else {
return "";
@@ -241,16 +260,16 @@ public String testOkHttp3Routing(String host, String version) {
*/
@RequestMapping("httpAsyncClient")
public String testHttpAsyncClientRouting(String host, String version) {
+ mockDataBase(DATABASE_TABLE_NAME);
String url = buildUrl(host);
try {
HttpGet request = new HttpGet(url);
request.setHeader(VERSION, version);
Future future = httpAsyncClient.execute(request, null);
HttpResponse response = future.get();
- int statusCode = response.getStatusLine().getStatusCode();
- if (statusCode == HttpStatus.SC_OK) {
+ if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity());
- mockRealLogic(result);
+ mockRealLogic();
return result;
} else {
return "";
@@ -269,6 +288,7 @@ public String testHttpAsyncClientRouting(String host, String version) {
*/
@RequestMapping("restTemplate")
public String testRestTemplateRouting(String host, String version) {
+ mockDataBase(DATABASE_TABLE_NAME);
String url = buildUrl(host);
HttpHeaders headers = new HttpHeaders();
headers.add(VERSION, version);
@@ -281,7 +301,7 @@ public String testRestTemplateRouting(String host, String version) {
);
if (response.getStatusCode().is2xxSuccessful()) {
String result = response.getBody();
- mockRealLogic(result);
+ mockRealLogic();
return result;
}
return "";
@@ -295,17 +315,38 @@ private String buildUrl(String host) {
return urlBuilder.toString();
}
- private void mockRealLogic(String result) {
- int i = 300;
- String tempString = "";
- while (i >= 0) {
- if (result.contains("message")) {
- tempString = "getMessage";
- } else {
- tempString = "getTime";
+ private void mockRealLogic() {
+ double result = 0.0;
+ for (int i = 0; i < ITERATION_COUNT; i++) {
+ result += Math.sin(i) * Math.cos(i);
+ }
+ }
+
+ private void mockDataBase(String table) {
+ if (mySqlConfig.isEnabled()) {
+ selectData(table);
+ }
+ }
+
+ private void selectData(String table) {
+ try (Statement statement = mySqlConnection.createStatement()) {
+ String selectQuery = "SELECT * FROM " + table + " WHERE id > 5001 AND age > 30 LIMIT 5";
+ ResultSet resultSet = statement.executeQuery(selectQuery);
+ while (resultSet.next()) {
+ resultSet.getInt("id");
+ resultSet.getString("name");
+ resultSet.getInt("age");
}
- tempString.split("");
- i--;
+ } catch (SQLException e) {
+ }
+ }
+
+ @Override
+ public void afterPropertiesSet() throws Exception {
+ if (mySqlConfig.isEnabled()) {
+ Thread.sleep(SLEEP_TIME);
+ mySqlConnection = DriverManager.getConnection(mySqlConfig.getAddress(),
+ mySqlConfig.getUser(), mySqlConfig.getPassword());
}
}
}
diff --git a/xds-router-demo/spring-cloud-client-xds/src/main/java/io/sermant/demo/xds/spring/client/config/MySqlConfig.java b/xds-router-demo/spring-cloud-client-xds/src/main/java/io/sermant/demo/xds/spring/client/config/MySqlConfig.java
new file mode 100644
index 0000000..78c2bf7
--- /dev/null
+++ b/xds-router-demo/spring-cloud-client-xds/src/main/java/io/sermant/demo/xds/spring/client/config/MySqlConfig.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2024-2024 Sermant Authors. All rights reserved.
+ *
+ * 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 io.sermant.demo.xds.spring.client.config;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+/**
+ * mysql config
+ *
+ * @author daizhenyu
+ * @since 2024-11-25
+ **/
+@Component
+@ConfigurationProperties(prefix = "mysql")
+public class MySqlConfig {
+ private String address;
+
+ private String user;
+
+ private String password;
+
+ private boolean enabled;
+
+ public String getAddress() {
+ return address;
+ }
+
+ public void setAddress(String address) {
+ this.address = address;
+ }
+
+ public String getUser() {
+ return user;
+ }
+
+ public void setUser(String user) {
+ this.user = user;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+ public boolean isEnabled() {
+ return enabled;
+ }
+
+ public void setEnabled(boolean enabled) {
+ this.enabled = enabled;
+ }
+}
diff --git a/xds-router-demo/spring-cloud-client-xds/src/main/java/io/sermant/demo/xds/spring/client/config/RestTemplateConfig.java b/xds-router-demo/spring-cloud-client-xds/src/main/java/io/sermant/demo/xds/spring/client/config/RestTemplateConfig.java
index 0a6dc2b..043c51a 100644
--- a/xds-router-demo/spring-cloud-client-xds/src/main/java/io/sermant/demo/xds/spring/client/config/RestTemplateConfig.java
+++ b/xds-router-demo/spring-cloud-client-xds/src/main/java/io/sermant/demo/xds/spring/client/config/RestTemplateConfig.java
@@ -16,6 +16,7 @@
package io.sermant.demo.xds.spring.client.config;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -35,6 +36,7 @@ public class RestTemplateConfig {
* @return RestTemplate bean
*/
@Bean
+ @ConditionalOnMissingBean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
diff --git a/xds-router-demo/spring-cloud-client-xds/src/main/resources/application.yml b/xds-router-demo/spring-cloud-client-xds/src/main/resources/application.yml
index 62b2a81..f978743 100644
--- a/xds-router-demo/spring-cloud-client-xds/src/main/resources/application.yml
+++ b/xds-router-demo/spring-cloud-client-xds/src/main/resources/application.yml
@@ -6,4 +6,9 @@ spring:
connect-string: ${ZOOKEEPER_IP}:2181
enabled: ${ZOOKEEPER_ENABLED:false}
server:
- port: 8080
\ No newline at end of file
+ port: 8080
+mysql:
+ address: jdbc:mariadb://mysql.default.svc.cluster.local:3306/test?useSSL=false&allowPublicKeyRetrieval=true
+ user: ${MYSQL_USER:root}
+ password: ${MYSQL_PASSWORD:}
+ enabled: ${MYSQL_ENABLED:false}
\ No newline at end of file
diff --git a/xds-router-demo/spring-cloud-server-xds/src/main/java/io/sermant/demo/xds/spring/server/RouterServerController.java b/xds-router-demo/spring-cloud-server-xds/src/main/java/io/sermant/demo/xds/spring/server/RouterServerController.java
index 03d0333..baaae6b 100644
--- a/xds-router-demo/spring-cloud-server-xds/src/main/java/io/sermant/demo/xds/spring/server/RouterServerController.java
+++ b/xds-router-demo/spring-cloud-server-xds/src/main/java/io/sermant/demo/xds/spring/server/RouterServerController.java
@@ -20,6 +20,9 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
/**
* RouterServerController
*
@@ -28,6 +31,8 @@
**/
@RestController
public class RouterServerController {
+ private static final int ITERATION_COUNT = 15000;
+
@Value("${SERVER_VERSION:v1}")
private String version;
@@ -37,7 +42,20 @@ public class RouterServerController {
* @return result
*/
@RequestMapping("router")
- public String router() {
- return "spring-server version: " + version;
+ public String router() throws UnknownHostException {
+ StringBuilder builder = new StringBuilder();
+ builder.append("spring-server version: ");
+ builder.append(version);
+ builder.append("; spring-server ip: ");
+ builder.append(InetAddress.getLocalHost().getHostAddress());
+ mockRealLogic();
+ return builder.toString();
+ }
+
+ private void mockRealLogic() {
+ double result = 0.0;
+ for (int i = 0; i < ITERATION_COUNT; i++) {
+ result += Math.sin(i) * Math.cos(i);
+ }
}
}