mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
using sentinel feign in nacos discovery example
This commit is contained in:
parent
6104799a36
commit
be2f52c880
@ -40,6 +40,11 @@
|
|||||||
<artifactId>spring-cloud-starter-feign</artifactId>
|
<artifactId>spring-cloud-starter-feign</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -2,6 +2,7 @@ package org.springframework.cloud.alibaba.cloud.examples;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.alibaba.cloud.examples.ConsumerApplication.EchoService;
|
||||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||||
import org.springframework.cloud.netflix.feign.EnableFeignClients;
|
import org.springframework.cloud.netflix.feign.EnableFeignClients;
|
||||||
@ -10,6 +11,7 @@ import org.springframework.context.annotation.Bean;
|
|||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -20,22 +22,50 @@ import org.springframework.web.client.RestTemplate;
|
|||||||
@EnableFeignClients
|
@EnableFeignClients
|
||||||
public class ConsumerApplication {
|
public class ConsumerApplication {
|
||||||
|
|
||||||
@LoadBalanced
|
@LoadBalanced
|
||||||
@Bean
|
@Bean
|
||||||
public RestTemplate restTemplate() {
|
public RestTemplate restTemplate() {
|
||||||
return new RestTemplate();
|
return new RestTemplate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(ConsumerApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@FeignClient(name = "service-provider", fallback = EchoServiceFallback.class, configuration = FeignConfiguration.class)
|
||||||
|
public interface EchoService {
|
||||||
|
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
|
||||||
|
String echo(@PathVariable("str") String str);
|
||||||
|
|
||||||
|
@RequestMapping(value = "/divide", method = RequestMethod.GET)
|
||||||
|
String divide(@RequestParam("a") Integer a, @RequestParam("b") Integer b);
|
||||||
|
|
||||||
public static void main(String[] args) {
|
@RequestMapping(value = "/notFound", method = RequestMethod.GET)
|
||||||
SpringApplication.run(ConsumerApplication.class, args);
|
String notFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
@FeignClient(name = "service-provider")
|
}
|
||||||
public interface EchoService {
|
|
||||||
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
|
class FeignConfiguration {
|
||||||
String echo(@PathVariable("str") String str);
|
@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";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import org.springframework.cloud.client.discovery.DiscoveryClient;
|
|||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
@ -15,31 +16,42 @@ import org.springframework.web.client.RestTemplate;
|
|||||||
@RestController
|
@RestController
|
||||||
public class TestController {
|
public class TestController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private RestTemplate restTemplate;
|
private RestTemplate restTemplate;
|
||||||
@Autowired
|
@Autowired
|
||||||
private EchoService echoService;
|
private EchoService echoService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DiscoveryClient discoveryClient;
|
||||||
|
|
||||||
@Autowired
|
@RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET)
|
||||||
private DiscoveryClient discoveryClient;
|
public String rest(@PathVariable String str) {
|
||||||
|
return restTemplate.getForObject("http://service-provider/echo/" + str,
|
||||||
|
String.class);
|
||||||
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET)
|
@RequestMapping(value = "/notFound-feign", method = RequestMethod.GET)
|
||||||
public String rest(@PathVariable String str) {
|
public String notFound() {
|
||||||
return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
|
return echoService.notFound();
|
||||||
}
|
}
|
||||||
@RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET)
|
|
||||||
public String feign(@PathVariable String str) {
|
|
||||||
return echoService.echo(str);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@RequestMapping(value = "/divide-feign", method = RequestMethod.GET)
|
||||||
|
public String divide(@RequestParam Integer a, @RequestParam Integer b) {
|
||||||
|
return echoService.divide(a, b);
|
||||||
|
}
|
||||||
|
|
||||||
@RequestMapping(value="/services/{service}",method = RequestMethod.GET)
|
@RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET)
|
||||||
public Object client(@PathVariable String service){
|
public String feign(@PathVariable String str) {
|
||||||
return discoveryClient.getInstances(service);
|
return echoService.echo(str);
|
||||||
}
|
}
|
||||||
@RequestMapping(value="/services",method = RequestMethod.GET)
|
|
||||||
public Object services(){
|
@RequestMapping(value = "/services/{service}", method = RequestMethod.GET)
|
||||||
return discoveryClient.getServices();
|
public Object client(@PathVariable String service) {
|
||||||
}
|
return discoveryClient.getInstances(service);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/services", method = RequestMethod.GET)
|
||||||
|
public Object services() {
|
||||||
|
return discoveryClient.getServices();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,12 @@
|
|||||||
spring.application.name=service-consumer
|
spring.application.name=service-consumer
|
||||||
server.port=18083
|
server.port=18083
|
||||||
management.endpoints.web.exposure.include=*
|
management.endpoints.web.exposure.include=*
|
||||||
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
|
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
|
||||||
|
|
||||||
|
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
|
@ -0,0 +1,10 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"resource": "GET:http://service-provider/echo/{str}",
|
||||||
|
"controlBehavior": 0,
|
||||||
|
"count": 1,
|
||||||
|
"grade": 1,
|
||||||
|
"limitApp": "default",
|
||||||
|
"strategy": 0
|
||||||
|
}
|
||||||
|
]
|
@ -6,6 +6,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
|||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -25,5 +26,10 @@ public class ProviderApplication {
|
|||||||
public String echo(@PathVariable String string) {
|
public String echo(@PathVariable String string) {
|
||||||
return "hello Nacos Discovery " + string;
|
return "hello Nacos Discovery " + string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/divide", method = RequestMethod.GET)
|
||||||
|
public String divide(@RequestParam Integer a, @RequestParam Integer b) {
|
||||||
|
return String.valueOf(a / b);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user