创建9003和9004两个服务模块!
创建一个消费者模块(84),
使用nacos+sentinel+rebbon+OpenFeign
这儿就新建一个9003,9004也是一样的配置
1.建model(nacos-sentinel-service9003)
2.引入依赖
<dependencies>
<!-- 这个是自己的抽取公共类.,不加入他也可以-->
<dependency>
<groupId>com.gaoyonglong</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--nacos的包-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- sentinel持久化-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!-- sentinel的依赖包-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</dependency>
</dependencies>
3.改yml
server:
port: 9003
spring:
application:
name: nacos-sentinel-service
cloud:
nacos:
discovery:
#nacos服务注册中心地址
server-addr: localhost:8848
sentinel:
transport:
#配置sentinel 的 dashboard地址
dashboard: localhost:8080
#默认端口为8791,若被占用,则开始+1一次扫描
port: 8791
management:
endpoints:
web:
exposure:
exclude: "*"
4.主启动类
@SpringBootApplication
@EnableDiscoveryClient
public class NacosSentinelService9003 {
public static void main(String[] args) {
SpringApplication.run(NacosSentinelService9003.class,args);
}
}
5.业务类
编写service接口和实现方法
public interface PaymentService {
public String getpayment(int id) ;
}
service的实现方法
@Service
public class PaymentServiceImpl implements PaymentService {
@Override
public String getpayment(@Param("id") int id) {
return "我是9003服务端口"+id;
}
}
//@Service
//public class PaymentServiceImpl implements PaymentService {
// @Override
// public String getpayment( int id) {
// return "我是9003服务端口"+id;
// }
//}
controller层
@RestController
public class PaymentController {
@GetMapping("/payment/get")
@SentinelResource(value = "payment/get",blockHandler ="blockPayment",fallback = "fallPayment")
public String getPayment(@RequestParam(value = "p1",required = false) String p){
return "我是9003服务端口";
}
}
//限流后的兜底方法
public String blockPayment(@RequestParam(value = "p1",required = false) String p){
return "我是9003的限流兜底方法";
}
//降级的兜底方法
public String fallPayment(@RequestParam(value = "p1",required = false) String p){
return "我是9003的服务降级方法!";
}
此时设置流控后,访问localhost:9003/payment/get?p=1,当超过限制后会走fallbock的方法!
创建84消费者模块
1.建model
2.引pom
<dependencies>
<dependency>
<groupId>com.gaoyonglong</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- sentinel持久化-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!-- sentinel的依赖包-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
3.改yml
server:
port: 84
spring:
application:
name: nacos-sentinel-Order84
cloud:
nacos:
discovery:
server-addr: localhost:8848
service-url:
#消费者将要去翻翻微博的微服务名称,cloud-nacos就是在9001中的spring.application.name(微服务名)
nacos-user-service: http://nacos-sentinel-service
4.主启动类
@SpringBootApplication
@EnableDiscoveryClient //开启nacos和Sentinel注解
@EnableFeignClients //开启OpenFeign注解
public class NacosSentinelOrder84 {
public static void main(String[] args) {
SpringApplication.run(NacosSentinelOrder84.class,args);
}
}
5.业务类
创建service接口,调用服务端数据
@FeignClient(value = "nacos-sentinel-service") //要调用的服务在nacos中的服务名
@Service
public interface PaymentService {
@GetMapping("/payment/get")
public String getpayment(@RequestParam("id") int id);
// @GetMapping("/payment/get/{id}")
// public String getpayment(@PathVariable("id") int id);
}
controller层
@Resource
PaymentService paymentService;
@GetMapping("/consumer/payment/get")
// @SentinelResource(value = "get",fallback = "fallPayment",blockHandler ="blockPayment")
public String getPayment(@RequestParam("id") Integer id){
return paymentService.getpayment(id);
}
//限流后的兜底方法
public String blockPayment(@RequestParam("id") Integer id){
return "444444444我是9003的限流兜底方法";
}
//降级的兜底方法
public String fallPayment(@RequestParam("id") Integer id){
return "我是9003的服务降级方法!";
}
此时访问 localhost:84/consumer/payment/get/1就会收到服务端的响应
添加熔断方法
1.在客户端(84)的pom中添加依赖OpenFeign
2.在yml中添加
#开启sentinel对feign的支持
feign:
sentinel:
enabled: true
3.在主启动类上添加@EnableFeignClients注解
4.编写service接口的时候要添加@FeignClient(value=“要调用服务模块在nacos的注册名”)
和上面一样
@FeignClient(value = "nacos-sentinel-service",fallback=PaymentFallbackService.class) //要调用的服务在nacos中的服务名
@Service
public interface PaymentService {
@GetMapping("/payment/get")
public String getpayment(@RequestParam("id") int id);
}
创建一个实现service接口的类!用来放熔断兜底方法!
@Component
public class PaymentFallbackService implements PaymentService {
@Override
//然后在service接口上@FeignClient(value = "nacos-sentinel-service",fallback=PaymentFallbackService.class)
public String getpayment(int id) {
return "我是单独的创建的类外兜底方法";
}
}
当访问http://localhost:84/consumer/payment/get?id=5的时候我们把9003服务端关闭了!这时就会进入到兜底方法中!
1.遇到的错误 找不到方法提示:
在中间我遇到一个错误,因为取值的注解用错了!客户端的service本应该用@PathVariable的我用了@Param,结果一直提示找不到响应的方法!是因为值没有取下来,就把值也当成请求连接的一部分了!系统找不到处理这个请求的方法!再次记一下!以防在犯!
2.nacos需要启动才可以启动服务端service的服务,不然服务端就会报错!因为没地方注册服务!就会一直提示注册失败!