init
This commit is contained in:
47
sentinel/sentinel-demo/sentinel-demo-okhttp/pom.xml
Normal file
47
sentinel/sentinel-demo/sentinel-demo-okhttp/pom.xml
Normal file
@@ -0,0 +1,47 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<artifactId>sentinel-demo</artifactId>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<version>1.8.3</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>sentinel-demo-okhttp</artifactId>
|
||||
|
||||
<properties>
|
||||
<spring.boot.version>2.1.3.RELEASE</spring.boot.version>
|
||||
<okhttp.version>3.6.0</okhttp.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-okhttp-adapter</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>${okhttp.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Add this so that we could display the resources via /tree command -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-transport-simple-http</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 1999-2020 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* 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.alibaba.csp.sentinel.demo.okhttp;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* @author zhaoyuguang
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class OkHttpDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(OkHttpDemoApplication.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 1999-2020 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* 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.alibaba.csp.sentinel.demo.okhttp.controller;
|
||||
|
||||
import com.alibaba.csp.sentinel.adapter.okhttp.SentinelOkHttpConfig;
|
||||
import com.alibaba.csp.sentinel.adapter.okhttp.SentinelOkHttpInterceptor;
|
||||
import com.alibaba.csp.sentinel.adapter.okhttp.fallback.DefaultOkHttpFallback;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author zhaoyuguang
|
||||
*/
|
||||
@RestController
|
||||
public class OkHttpTestController {
|
||||
|
||||
@Value("${server.port}")
|
||||
private Integer port;
|
||||
|
||||
private final OkHttpClient client = new OkHttpClient.Builder()
|
||||
.addInterceptor(new SentinelOkHttpInterceptor(new SentinelOkHttpConfig((request, connection) -> {
|
||||
String regex = "/okhttp/back/";
|
||||
String url = request.url().toString();
|
||||
if (url.contains(regex)) {
|
||||
url = url.substring(0, url.indexOf(regex) + regex.length()) + "{id}";
|
||||
}
|
||||
return request.method() + ":" + url;
|
||||
}, new DefaultOkHttpFallback())))
|
||||
.build();
|
||||
|
||||
@RequestMapping("/okhttp/back")
|
||||
public String back() {
|
||||
return "Welcome Back!";
|
||||
}
|
||||
|
||||
@RequestMapping("/okhttp/back/{id}")
|
||||
public String back(@PathVariable String id) {
|
||||
return "Welcome Back! " + id;
|
||||
}
|
||||
|
||||
@RequestMapping("/okhttp/testcase/{id}")
|
||||
public String testcase(@PathVariable String id) throws Exception {
|
||||
return getRemoteString(id);
|
||||
}
|
||||
|
||||
@RequestMapping("/okhttp/testcase")
|
||||
public String testcase() throws Exception {
|
||||
return getRemoteString(null);
|
||||
}
|
||||
|
||||
private String getRemoteString(String id) throws IOException {
|
||||
Request request = new Request.Builder()
|
||||
.url("http://localhost:" + port + "/okhttp/back" + (id == null ? "" : "/" + id))
|
||||
.build();
|
||||
Response response = client.newCall(request).execute();
|
||||
return response.body().string();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
spring.application.name=sentinel-demo-okhttp
|
||||
server.port=8085
|
||||
Reference in New Issue
Block a user