Skip to content

流量防护demo #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

@Api(value = "/", tags = {"入口应用"})
@RestController
Expand Down Expand Up @@ -55,6 +60,7 @@ class AController {

private String currentZone;


@PostConstruct
private void init() {
try {
Expand Down Expand Up @@ -96,6 +102,44 @@ public String a(HttpServletRequest request) throws ExecutionException, Interrupt
"[config=" + configValue + "]" + " -> " + result;
}

@ApiOperation(value = "测试防护规则" , tags = {"流量防护"})
@GetMapping("/flow")
public String flow(HttpServletRequest request) throws ExecutionException, InterruptedException {

ResponseEntity<String> responseEntity = loadBalancedRestTemplate.getForEntity("http://sc-B/flow", String.class);
HttpStatus status = responseEntity.getStatusCode();
String result = responseEntity.getBody() + status.value();

return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
"[config=" + configValue + "]" + " -> " + result;
}


@ApiOperation(value = "测试热点规则", tags = {"流量防护"})
@GetMapping("/params/{hot}")
public String params(HttpServletRequest request,@PathVariable("hot") String hot) throws ExecutionException, InterruptedException {
ResponseEntity<String> responseEntity = loadBalancedRestTemplate.getForEntity("http://sc-B/params/"+hot, String.class);

HttpStatus status = responseEntity.getStatusCode();
String result = responseEntity.getBody() + status.value();

return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
"[config=" + configValue + "]" + " -> " + result;
}

@ApiOperation(value = "测试隔离规则", tags = { "流量防护"})
@GetMapping("/isolate")
public String isolate(HttpServletRequest request) throws ExecutionException, InterruptedException {
ResponseEntity<String> responseEntity = loadBalancedRestTemplate.getForEntity("http://sc-B/isolate", String.class);

HttpStatus status = responseEntity.getStatusCode();
String result = responseEntity.getBody() + status.value();

return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" +
"[config=" + configValue + "]" + " -> " + result;
}


@GetMapping("/spring_boot")
public String spring_boot(HttpServletRequest request) {
String result = restTemplate.getForObject("http://sc-b:20002/spring_boot", String.class);
Expand Down Expand Up @@ -138,11 +182,65 @@ public String dubbo(HttpServletRequest request) {
helloServiceB.hello("A");
}

@ApiOperation(value = "Dubbo 全链路灰度入口", tags = {"入口应用"})
@GetMapping("/dubbo-flow")
public String dubbo_flow(HttpServletRequest request) {
StringBuilder headerSb = new StringBuilder();
Enumeration<String> enumeration = request.getHeaderNames();
while (enumeration.hasMoreElements()) {
String headerName = enumeration.nextElement();
Enumeration<String> val = request.getHeaders(headerName);
while (val.hasMoreElements()) {
String headerVal = val.nextElement();
headerSb.append(headerName + ":" + headerVal + ",");
}
}
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
helloServiceB.hello("A");
}

@ApiOperation(value = "Dubbo 全链路灰度入口", tags = {"入口应用"})
@GetMapping("/dubbo-params/{hot}")
public String dubbo_params(HttpServletRequest request, @PathVariable("hot") String hot) {
StringBuilder headerSb = new StringBuilder();
Enumeration<String> enumeration = request.getHeaderNames();
while (enumeration.hasMoreElements()) {
String headerName = enumeration.nextElement();
Enumeration<String> val = request.getHeaders(headerName);
while (val.hasMoreElements()) {
String headerVal = val.nextElement();
headerSb.append(headerName + ":" + headerVal + ",");
}
}
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
helloServiceB.hello(hot);
}

@ApiOperation(value = "Dubbo 全链路灰度入口", tags = {"入口应用"})
@GetMapping("/dubbo-isolate")
public String dubbo_isolate(HttpServletRequest request) {
StringBuilder headerSb = new StringBuilder();
Enumeration<String> enumeration = request.getHeaderNames();
while (enumeration.hasMoreElements()) {
String headerName = enumeration.nextElement();
Enumeration<String> val = request.getHeaders(headerName);
while (val.hasMoreElements()) {
String headerVal = val.nextElement();
headerSb.append(headerName + ":" + headerVal + ",");
}
}
return "A" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
helloServiceB.hello("isolate");
}


@GetMapping("swagger-demo")
@ApiOperation(value = "这是一个演示swagger的接口 ", tags = {"首页操作页面"})
public String swagger(@ApiParam(name = "name", value = "我是姓名", required = true) String name,
@ApiParam(name = "age", value = "我是年龄", required = true) int age,
@ApiParam(name = "aliware-products", value = "我是购买阿里云原生产品列表", required = true) List<String> aliwareProducts) {
return "hello swagger";
}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.alibabacloud.mse.demo;

import com.alibabacloud.mse.demo.service.HelloServiceC;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
Expand All @@ -10,11 +12,15 @@
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

@RestController
class BController {
Expand All @@ -27,6 +33,9 @@ class BController {
@Qualifier("restTemplate")
private RestTemplate restTemplate;

@Reference(application = "${dubbo.application.id}", version = "1.1.0")
private HelloServiceC helloServiceC;

@Autowired
InetUtils inetUtils;

Expand All @@ -35,6 +44,8 @@ class BController {

private String currentZone;

private static final Random RANDOM = new Random();

@PostConstruct
private void init() {
try {
Expand All @@ -53,6 +64,55 @@ private void init() {
}
}


@GetMapping("/flow")
public String flow(HttpServletRequest request) throws ExecutionException, InterruptedException {
long sleepTime = 5 + RANDOM.nextInt(5);
silentSleep(sleepTime);

return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " + sleepTime;
}

@GetMapping("/params/{hot}")
public String params(HttpServletRequest request,@PathVariable("hot") String hot) throws ExecutionException, InterruptedException {
long sleepTime = 5 + RANDOM.nextInt(5);
silentSleep(sleepTime);
helloServiceC.hello(hot);
return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " + sleepTime+":"+hot;
}

@GetMapping("/isolate")
public String isolate(HttpServletRequest request) throws ExecutionException, InterruptedException {
long sleepTime = 20 + RANDOM.nextInt(5);
silentSleep(sleepTime);
return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " + sleepTime;
}


@GetMapping("/flow-c")
public String flow_c(HttpServletRequest request) throws ExecutionException, InterruptedException {
return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
helloServiceC.hello("B");
}

@GetMapping("/params-c/{hot}")
public String params_c(HttpServletRequest request,@PathVariable("hot") String hot) throws ExecutionException, InterruptedException {
return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
helloServiceC.hello(hot);
}

@GetMapping("/isolate-c")
public String isolate_c(HttpServletRequest request) throws ExecutionException, InterruptedException {
return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
helloServiceC.hello("B");
}

@GetMapping("/rpc-c")
public String rpc_c(HttpServletRequest request) throws ExecutionException, InterruptedException {
return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
helloServiceC.world("B");
}

@GetMapping("/b")
public String b(HttpServletRequest request) {
return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
Expand All @@ -70,4 +130,13 @@ public String spring_boot(HttpServletRequest request) {
return "B" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
restTemplate.getForObject("http://sc-c:20003/spring_boot", String.class);
}


private void silentSleep(long ms) {
try {
TimeUnit.MILLISECONDS.sleep(ms);
} catch (InterruptedException ignored) {
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

public interface HelloServiceC {
String hello(String name);
String world(String name);
}
25 changes: 25 additions & 0 deletions mse-simple-demo/C/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,31 @@
<version>4.5.13</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.199</version>
</dependency>

<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>spring-boot-starter-ahas-sentinel-client</artifactId>
<version>1.10.11</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
Loading