前言
使用sentinel实现服务间的调用,sentinel在这里面作为了是流量防护组件。他结合openfeign实现了服务降级等作用。 以下我们创建结合sentinel和feign的创建2个应用,服务消费方,服务提供方。
内容
服务提供方:provider创建
- 1.创建spring项目
- 2.引入cloud依赖
|
|
@RestController
public class DemoController {
@GetMapping(“/feign”)
public String feign(){
return “hello sentinel feign”;
}
}
@SpringCloudApplication
public class SentinelFeignProviderApplication {
public static void main(String[] args) {
SpringApplication.run(SentinelFeignProviderApplication.class, args);
}
}
server:
port: 18082
spring:
application:
name: sentinel-feign-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
@FeignClient(value = “sentinel-feign-provider”,fallback =DemoFeignClientFallback.class)
public interface DemoFeginClient {
@GetMapping(“/feign”)
String feign();
}
@Component
public class DemoFeignClientFallback implements DemoFeginClient {
@Override
public String feign() {
return “error”;
}
}
@RestController
public class DemoController {
@Autowired
private DemoFeginClient demoFeginClient;
@GetMapping(“/hello”)
public String feign(){
return demoFeginClient.feign();
}
}
```
测试:
http://localhost:18083/hello
结果:
hello sentinel feign