织梦的网站关键词,广州建模培训,工程招标,线下推广的好处本文所使用的组件#xff1a;Nacos#xff08;服务中心和注册中心#xff09;、OpenFeign#xff08;服务调用#xff09;、Sentinel#xff08;限流、降级#xff09;、Hystrix#xff08;熔断#xff09;
项目结构#xff1a; service-provider#xff1a;提供服…本文所使用的组件Nacos服务中心和注册中心、OpenFeign服务调用、Sentinel限流、降级、Hystrix熔断
项目结构 service-provider提供服务的微服务。 service-consumer消费服务的微服务。 1. 添加依赖
在两个服务的pom.xml文件中添加Spring Cloud Alibaba、Nacos、Sentinel、Hystrix和OpenFeign的依赖。
dependencies!-- Spring Cloud Alibaba Nacos Discovery --dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependency!-- Spring Cloud Alibaba Sentinel --dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactId/dependency!-- Hystrix --dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-hystrix/artifactId/dependency!-- Spring Cloud OpenFeign --dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId/dependency!-- Spring Boot Web Starter --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency
/dependencies 2. Docker-compose.yml文件
创建docker-compose.yml文件启动Nacos和Sentinel。
version: 3
services:nacos-server:image: nacos/nacos-server:latestcontainer_name: nacos-serverports:- 8848:8848environment:- MODEstandalone- SPRING_DATASOURCE_PLATFORMmysqlsentinel-server:image: apache/incubating-sentinel-dashboard:latestcontainer_name: sentinel-serverports:- 8080:8080- 8719:8719 部署在云服务中需要打开8080、8848端口端口冲突可以更换端口。 3. service-provider提供服务的微服务
3.1 启动类
创建启动类并添加nacos注册客户端。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;SpringBootApplication
EnableFeignClients
public class ServiceProviderApplication {public static void main(String[] args) {SpringApplication.run(ServiceProviderApplication.class, args);}
} 3.2 控制器
创建控制器提供服务。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;RestController
public class ServiceController {GetMapping(/provider)public String provider() {return Hello from Service Provider;}
} 4. service-consumer消费服务的微服务
4.1 OpenFeign客户端
创建Feign客户端用于调用service-provider。
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;FeignClient(name service-provider, fallback ServiceFallback.class)
public interface ServiceClient {GetMapping(/provider)String provider();
}Component
public class ServiceFallback implements ServiceClient {Overridepublic String provider() {// 服务降级逻辑return Fallback response from Service Consumer;}
} 4.2 控制器
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;RestController
public class ConsumerController {private final ServiceClient serviceClient;public ConsumerController(ServiceClient serviceClient) {this.serviceClient serviceClient;}GetMapping(/consumer)HystrixCommand(commandKey consumerCommand, fallbackMethod fallback)SentinelResource(value serviceClientCall, blockHandler blockHandler)public String consumer() {return serviceClient.provider();}public String fallback() {// 熔断降级逻辑return Service is down, hystrix fallback;}public String blockHandler(BlockException ex) {// Sentinel降级逻辑return Service is blocked by Sentinel;}
} HystrixCommand注解用于指定熔断器的命令键和降级方法SentinelResource注解用于指定资源名和降级方法。 5. application.yml
在两个服务添加application.yml文件。
spring:cloud:nacos:discovery:server-addr: nacos-server:8848config:server-addr: nacos-server:8848sentinel:transport:dashboard: sentinel-server:8080datasource:ds1:type: filefile:filePath: /path/to/your/sentinel/ruleshystrix:command:default:execution:isolation:strategy: THREADthread:timeoutInMilliseconds: 3000 filePath:文件路径 6. 配置规则
在Sentinel Dashboardhttp://localhost:8080中添加限流规则
资源名serviceClientCall
限流模式QPS
阈值10 总结上述为一套完整的服务治理流程对于某些场景下可以使用guava框架去实现单体限流。主要学习思想本文所使用的组件可替换为其他具有相关功能的组件。 不积跬步无以至千里 --- xiaokai