Skip to content

Commit

Permalink
update xds router demo
Browse files Browse the repository at this point in the history
Signed-off-by: daizhenyu <1449308021@qq.com>
  • Loading branch information
daizhenyu committed Nov 25, 2024
1 parent 70784ac commit ead5ee2
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 29 deletions.
41 changes: 41 additions & 0 deletions xds-router-demo/router-product/script/mysql.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions xds-router-demo/router-product/script/spring-client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions xds-router-demo/spring-cloud-client-xds/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<httpclient.version>4.5.13</httpclient.version>
<okhttp3.version>4.9.3</okhttp3.version>
<httpclient.async.version>4.1.5</httpclient.async.version>
<mariadb.version>3.3.2</mariadb.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -47,6 +48,11 @@
<artifactId>httpasyncclient</artifactId>
<version>${httpclient.async.version}</version>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>${mariadb.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,6 +29,7 @@
* @since 2024-09-23
**/
@SpringBootApplication
@EnableConfigurationProperties(MySqlConfig.class)
public class SpringCloudClientApplication {
/**
* main
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

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;

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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -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 "";
Expand All @@ -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;
Expand All @@ -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 "";
Expand Down Expand Up @@ -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 "";
Expand All @@ -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<HttpResponse> 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 "";
Expand All @@ -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);
Expand All @@ -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 "";
Expand All @@ -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());
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,6 +36,7 @@ public class RestTemplateConfig {
* @return RestTemplate bean
*/
@Bean
@ConditionalOnMissingBean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
Expand Down
Loading

0 comments on commit ead5ee2

Please sign in to comment.