diff --git a/spring-cloud-alibaba-examples/pom.xml b/spring-cloud-alibaba-examples/pom.xml index 4e9a155ee..9ea947892 100644 --- a/spring-cloud-alibaba-examples/pom.xml +++ b/spring-cloud-alibaba-examples/pom.xml @@ -20,6 +20,8 @@ sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api + sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example + sentinel-example/sentinel-feign-example/sentinel-feign-provider-example sentinel-example/sentinel-webflux-example sentinel-example/sentinel-spring-cloud-gateway-example sentinel-example/sentinel-zuul-example diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/pom.xml new file mode 100644 index 000000000..4166151bb --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/pom.xml @@ -0,0 +1,63 @@ + + + + + com.alibaba.cloud + spring-cloud-alibaba-examples + 0.9.1.BUILD-SNAPSHOT + ../../../pom.xml + + 4.0.0 + + + sentinel-feign-consumer-example + jar + Example demonstrating how to use sentinel with feign + + + + + org.springframework.boot + spring-boot-starter-web + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + org.springframework.boot + spring-boot-starter-actuator + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-sentinel + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-deploy-plugin + ${maven-deploy-plugin.version} + + true + + + + + + diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/ConsumerApplication.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/ConsumerApplication.java new file mode 100644 index 000000000..ff0364774 --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/ConsumerApplication.java @@ -0,0 +1,20 @@ +package com.alibaba.cloud.examples; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.SpringCloudApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.openfeign.EnableFeignClients; + +/** + * @author lengleng + */ +@EnableFeignClients +@SpringCloudApplication +public class ConsumerApplication { + + public static void main(String[] args) { + SpringApplication.run(ConsumerApplication.class, args); + } + +} diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/controller/TestController.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/controller/TestController.java new file mode 100644 index 000000000..f90fe1077 --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/controller/TestController.java @@ -0,0 +1,23 @@ +package com.alibaba.cloud.examples.controller; + +import com.alibaba.cloud.examples.service.EchoService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author lengleng + */ +@RestController +public class TestController { + + @Autowired + private EchoService echoService; + + @GetMapping(value = "/echo-feign/{str}") + public String feign(@PathVariable String str) { + return echoService.echo(str); + } + +} diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/fallback/EchoServiceFallback.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/fallback/EchoServiceFallback.java new file mode 100644 index 000000000..6d4fd77de --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/fallback/EchoServiceFallback.java @@ -0,0 +1,28 @@ +package com.alibaba.cloud.examples.fallback; + +import com.alibaba.cloud.examples.service.EchoService; + +/** + * @author lengleng + * @date 2019-08-01 + *

+ * sentinel 降级处理 + */ +public class EchoServiceFallback implements EchoService { + private Throwable throwable; + + EchoServiceFallback(Throwable throwable) { + this.throwable = throwable; + } + + /** + * 调用服务提供方的输出接口 + * + * @param str 用户输入 + * @return + */ + @Override + public String echo(String str) { + return "consumer-fallback-default-str" + throwable.getMessage(); + } +} diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/fallback/EchoServiceFallbackFactory.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/fallback/EchoServiceFallbackFactory.java new file mode 100644 index 000000000..6d33d7136 --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/fallback/EchoServiceFallbackFactory.java @@ -0,0 +1,16 @@ +package com.alibaba.cloud.examples.fallback; + +import feign.hystrix.FallbackFactory; +import org.springframework.stereotype.Component; + +/** + * @author lengleng + * @date 2019-08-01 + */ +@Component +public class EchoServiceFallbackFactory implements FallbackFactory { + @Override + public EchoServiceFallback create(Throwable throwable) { + return new EchoServiceFallback(throwable); + } +} diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/service/EchoService.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/service/EchoService.java new file mode 100644 index 000000000..2dcbdc967 --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/service/EchoService.java @@ -0,0 +1,25 @@ +package com.alibaba.cloud.examples.service; + +import com.alibaba.cloud.examples.fallback.EchoServiceFallbackFactory; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; + +/** + * @author lengleng + * @date 2019-08-01 + *

+ * example feign client + */ +@FeignClient(name = "service-provider", fallbackFactory = EchoServiceFallbackFactory.class) +public interface EchoService { + + /** + * 调用服务提供方的输出接口 + * + * @param str 用户输入 + * @return + */ + @GetMapping(value = "/echo/{str}") + String echo(@PathVariable("str") String str); +} diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/resources/application.yml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/resources/application.yml new file mode 100644 index 000000000..219c38d02 --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/resources/application.yml @@ -0,0 +1,20 @@ +server: + port: 18087 + +spring: + application: + name: service-consumer + cloud: + nacos: + discovery: + server-addr: 127.0.0.1:8848 + +feign: + sentinel: + enabled: true + +management: + endpoints: + web: + exposure: + include: '*' diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/pom.xml new file mode 100644 index 000000000..1bb1875fb --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/pom.xml @@ -0,0 +1,52 @@ + + + + + com.alibaba.cloud + spring-cloud-alibaba-examples + 0.9.1.BUILD-SNAPSHOT + ../../../pom.xml + + 4.0.0 + + + sentinel-feign-provider-example + jar + Example demonstrating how to use sentinel with feign + + + + + org.springframework.boot + spring-boot-starter-web + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + org.springframework.boot + spring-boot-starter-actuator + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-deploy-plugin + ${maven-deploy-plugin.version} + + true + + + + + + diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/src/main/java/com/alibaba/cloud/examples/ProviderApplication.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/src/main/java/com/alibaba/cloud/examples/ProviderApplication.java new file mode 100644 index 000000000..cdc17dd81 --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/src/main/java/com/alibaba/cloud/examples/ProviderApplication.java @@ -0,0 +1,17 @@ +package com.alibaba.cloud.examples; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +/** + * @author lengleng + */ +@EnableDiscoveryClient +@SpringBootApplication +public class ProviderApplication { + + public static void main(String[] args) { + SpringApplication.run(ProviderApplication.class, args); + } +} diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/src/main/java/com/alibaba/cloud/examples/controller/EchoController.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/src/main/java/com/alibaba/cloud/examples/controller/EchoController.java new file mode 100644 index 000000000..6d99f33dc --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/src/main/java/com/alibaba/cloud/examples/controller/EchoController.java @@ -0,0 +1,19 @@ +package com.alibaba.cloud.examples.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author lengleng + * @date 2019-08-01 + */ +@RestController +public class EchoController { + + @GetMapping("/echo/{str}") + public String echo(@PathVariable String str) { + return "provider-" + str; + } + +} diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/src/main/resources/application.yml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/src/main/resources/application.yml new file mode 100644 index 000000000..f6098ec77 --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/src/main/resources/application.yml @@ -0,0 +1,16 @@ +server: + port: 18088 + +spring: + application: + name: service-provider + cloud: + nacos: + discovery: + server-addr: 127.0.0.1:8848 + +management: + endpoints: + web: + exposure: + include: '*'