mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
update examples
This commit is contained in:
@@ -41,6 +41,11 @@
|
||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
|
@@ -2,6 +2,6 @@
|
||||
n=1
|
||||
while [ $n -le 10 ]
|
||||
do
|
||||
echo `curl -s http://localhost:18083/echo-feign/openfeign`
|
||||
echo `curl -s http://localhost:18083/test`
|
||||
let n++
|
||||
done
|
@@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
n=1
|
||||
while [ $n -le 10 ]
|
||||
do
|
||||
echo `curl -s http://localhost:18083/divide-feign2?a=1`
|
||||
let n++
|
||||
done
|
@@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
n=1
|
||||
while [ $n -le 10 ]
|
||||
do
|
||||
echo `curl -s http://localhost:18083/divide-feign?a=1\&b=0`
|
||||
let n++
|
||||
done
|
@@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
n=1
|
||||
while [ $n -le 10 ]
|
||||
do
|
||||
echo `curl -s http://localhost:18083/index`
|
||||
let n++
|
||||
done
|
@@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
n=1
|
||||
while [ $n -le 10 ]
|
||||
do
|
||||
echo `curl -s http://localhost:18083/sleep`
|
||||
let n++
|
||||
done
|
@@ -19,6 +19,8 @@ package com.alibaba.cloud.examples;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import com.alibaba.cloud.examples.ConsumerSCLBApplication.EchoService;
|
||||
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import reactor.core.publisher.Mono;
|
||||
@@ -28,6 +30,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.cloud.client.loadbalancer.reactive.DefaultResponse;
|
||||
@@ -58,10 +61,18 @@ public class ConsumerSCLBApplication {
|
||||
|
||||
@LoadBalanced
|
||||
@Bean
|
||||
@SentinelRestTemplate(urlCleanerClass = UrlCleaner.class, urlCleaner = "clean")
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
@LoadBalanced
|
||||
@Bean
|
||||
@SentinelRestTemplate
|
||||
public RestTemplate restTemplate1() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ConsumerSCLBApplication.class, args);
|
||||
}
|
||||
@@ -112,7 +123,8 @@ public class ConsumerSCLBApplication {
|
||||
|
||||
}
|
||||
|
||||
@FeignClient(name = "service-provider")
|
||||
@FeignClient(name = "service-provider", fallback = EchoServiceFallback.class,
|
||||
configuration = FeignConfiguration.class)
|
||||
public interface EchoService {
|
||||
|
||||
@GetMapping("/echo/{str}")
|
||||
@@ -136,9 +148,15 @@ public class ConsumerSCLBApplication {
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@Autowired
|
||||
private RestTemplate restTemplate1;
|
||||
|
||||
@Autowired
|
||||
private EchoService echoService;
|
||||
|
||||
@Autowired
|
||||
private DiscoveryClient discoveryClient;
|
||||
|
||||
@GetMapping("/echo-rest/{str}")
|
||||
public String rest(@PathVariable String str) {
|
||||
return restTemplate.getForObject("http://service-provider/echo/" + str,
|
||||
@@ -150,6 +168,76 @@ public class ConsumerSCLBApplication {
|
||||
return echoService.echo(str);
|
||||
}
|
||||
|
||||
@GetMapping("/index")
|
||||
public String index() {
|
||||
return restTemplate1.getForObject("http://service-provider", String.class);
|
||||
}
|
||||
|
||||
@GetMapping("/test")
|
||||
public String test() {
|
||||
return restTemplate1.getForObject("http://service-provider/test",
|
||||
String.class);
|
||||
}
|
||||
|
||||
@GetMapping("/sleep")
|
||||
public String sleep() {
|
||||
return restTemplate1.getForObject("http://service-provider/sleep",
|
||||
String.class);
|
||||
}
|
||||
|
||||
@GetMapping("/notFound-feign")
|
||||
public String notFound() {
|
||||
return echoService.notFound();
|
||||
}
|
||||
|
||||
@GetMapping("/divide-feign")
|
||||
public String divide(@RequestParam Integer a, @RequestParam Integer b) {
|
||||
return echoService.divide(a, b);
|
||||
}
|
||||
|
||||
@GetMapping("/divide-feign2")
|
||||
public String divide(@RequestParam Integer a) {
|
||||
return echoService.divide(a);
|
||||
}
|
||||
|
||||
@GetMapping("/services/{service}")
|
||||
public Object client(@PathVariable String service) {
|
||||
return discoveryClient.getInstances(service);
|
||||
}
|
||||
|
||||
@GetMapping("/services")
|
||||
public Object services() {
|
||||
return discoveryClient.getServices();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class FeignConfiguration {
|
||||
|
||||
@Bean
|
||||
public EchoServiceFallback echoServiceFallback() {
|
||||
return new EchoServiceFallback();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class EchoServiceFallback implements EchoService {
|
||||
|
||||
@Override
|
||||
public String echo(@PathVariable("str") String str) {
|
||||
return "echo fallback";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String divide(@RequestParam Integer a, @RequestParam Integer b) {
|
||||
return "divide fallback";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String notFound() {
|
||||
return "notFound fallback";
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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.cloud.examples;
|
||||
|
||||
public class UrlCleaner {
|
||||
|
||||
public static String clean(String url) {
|
||||
System.out.println("enter urlCleaner");
|
||||
if (url.matches(".*/echo/.*")) {
|
||||
System.out.println("change url");
|
||||
url = url.replaceAll("/echo/.*", "/echo/{str}");
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
}
|
@@ -4,3 +4,16 @@ management.endpoints.web.exposure.include=*
|
||||
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
|
||||
|
||||
spring.cloud.loadbalancer.ribbon.enabled=false
|
||||
|
||||
feign.sentinel.enabled=true
|
||||
|
||||
spring.cloud.sentinel.transport.dashboard=localhost:8080
|
||||
spring.cloud.sentinel.eager=true
|
||||
|
||||
spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json
|
||||
spring.cloud.sentinel.datasource.ds1.file.data-type=json
|
||||
spring.cloud.sentinel.datasource.ds1.file.rule-type=flow
|
||||
|
||||
spring.cloud.sentinel.datasource.ds2.file.file=classpath: degraderule.json
|
||||
spring.cloud.sentinel.datasource.ds2.file.data-type=json
|
||||
spring.cloud.sentinel.datasource.ds2.file.rule-type=degrade
|
||||
|
@@ -0,0 +1,26 @@
|
||||
[
|
||||
{
|
||||
"resource": "GET:http://service-provider/test",
|
||||
"count": 0.5,
|
||||
"grade": 1,
|
||||
"timeWindow": 30
|
||||
},
|
||||
{
|
||||
"resource": "GET:http://service-provider",
|
||||
"count": 0.5,
|
||||
"grade": 1,
|
||||
"timeWindow": 10
|
||||
},
|
||||
{
|
||||
"resource": "GET:http://service-provider/sleep",
|
||||
"count": 20.0,
|
||||
"grade": 0,
|
||||
"timeWindow": 30
|
||||
},
|
||||
{
|
||||
"resource": "GET:http://service-provider/divide",
|
||||
"count": 0.5,
|
||||
"grade": 1,
|
||||
"timeWindow": 30
|
||||
}
|
||||
]
|
@@ -0,0 +1,10 @@
|
||||
[
|
||||
{
|
||||
"resource": "GET:http://service-provider/echo/{str}",
|
||||
"controlBehavior": 0,
|
||||
"count": 2,
|
||||
"grade": 1,
|
||||
"limitApp": "default",
|
||||
"strategy": 0
|
||||
}
|
||||
]
|
Reference in New Issue
Block a user