Nacos官方简介
Nacos致力于帮助您发现,配置和管理微服务。它提供了一组简单有用的功能,使您能够实现动态服务发现,服务配置,服务元数据和流量管理。
Nacos使构建,交付和管理微服务平台变得更容易,更快捷。它是通过微服务或云原生方法支持以服务为中心的现代应用程序体系结构的基础架构。
Nacos安装
官方提供了Nacos的服务端供我们下载使用,我们启动Nacos后将我们的微服务注册进入Nacos即可。
下载地址:Releases · alibaba/nacos · GitHub
下载后解压即可,
注意:路径不能有中文
然后去他的bin目录下的startup.cmd的启动文件,以文本方式打开
将MODE改成standalone,因为他默认是集群模式启动,一个人使用的时候不是集群,然后去conf文件下的application.properties的文件里
将这几个解开,user和password就是你连接数据库的账号和密码,在去数据库创一个nacos的库,将conf文件里的nacos-mysql.sql导进去,启动启动项即可,再去网页访问:localhost:8848,成功如下
能出来就说明成功了
项目中使用
搭建父项目
导包
<!--SpringCloud-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
UserServer子项目
子项目导包
<!-- 服务发现 -->
<dependency>
<groupId>com.alibaba.cloud </groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--加入WEB依赖是为了方便后面写Controller-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
启动类
@SpringBootApplication
@EnableDiscoveryClient
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
@EnableDiscoveryClient注解开启服务发现功能,注册到nacos
yaml文件
server:
port: 10010
spring:
application:
name: server-user
cloud:
sentinel:
transport:
dashboard: localhost:1111
nacos:
discovery:
server-addr: 127.0.0.1:8848 #注册中心地址
启动项目
成功注册到nacos,如下:
Nacos配置管理
在nacos的配置管理的配置列表点击“+”号添加配置文件
填写Data ID,选择YAML将UserServer的配置类内容复制到这里面,
再导包
<!-- 配置中心客户端-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
删掉原本的application.yml文件,添加一个bootstrap.yml文件,配置如下
server:
port: 10010
spring:
profiles:
active: dev
application:
name: server-user
cloud:
nacos:
discovery:
server-addr: localhost:8848 #注册中心
config:
server-addr: localhost:8848 #配置中心
file-extension: yaml #配置文件格式
prefix: application-user #配置前缀 ,默认使用sring.application.name
group: DEFAULT_GROUP #默认分组
他回去找以application-user开头的yaml文件配置,profiles:active是环境,都是在前面Data ID里面写了的。这样就可以将配置交给nacos管理,nacos里面的配置修改,不需要重启项目。以后只需要维护bootstrap配置文件即可。
Sentinel
Sentinel诞生于阿里巴巴,其主要目标是流量控制和服务熔断,2018年,Sentinel演变为一个开源项目现如今成为了Spring Cloud Alibaba的一个子项目。Sentinel是通过限制并发线程的数量来减少不稳定资源的影响,而不是使用线程池,省去了线程切换的性能开销。
当资源的响应时间变长时,线程将开始被占用。当线程数累积到一定数量时,新的传入请求将被拒绝。反之亦然,当资源恢复并变得稳定时,占用的线程也将被释放,新请求将被接受。
除了限制并发性外,Sentinel可以根据响应时间降级不稳定资源也是保证可靠性的有效方法。当资源的响应时间太大时,将在指定的时间窗口中拒绝所有对该资源的访问。-- 熔断机制
安装
可以去网上搜一下安装包,下载之后通过命令行启动
java -jar -Dserver.port=1111 sentinel-dashboard-1.6.0.jar
启动,网上访问localhost:1111
默认账号密码都是sentinel
限流
导包
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
配置文件
spring:
cloud:
sentinel:
transport:
dashboard: localhost:1111
代码
Sentinel为我们提供了 @SentinelResource 注解标记需要限流的资源。 修改UserController,代码如下:
@RestController
public class UserController {
@GetMapping("/user/{id}")
@SentinelResource(value="user",blockHandler="getUserById")
public User getById(@PathVariable("id")Long id){
// int i = 1 / 0;
return new User(id,"zs");
}
public User getUserById(@PathVariable("id")Long id, BlockException exception){
exception.printStackTrace();
System.out.println("限流了");
return new User(-1L,"限流了");
}
}
Sentinel设置限流策略
启动应用 springcloudalibaba-user-server-1010 ,然后通过浏览器访问 http://localhost:10010/user/11 ,然后登录Sentinel控制台,在“实时监控”列表中可以看到资源的相关监控信息的,要先访一遍http://localhost:10010/user/11,sentinel才会有监控。
点击左边的“簇点连接” ,选择右上角列表视图,
就可以看到每个选项都有有流控按钮,选择自己想要的路径,添加流控
我这里填的是 2 ,意思是,再一秒内访问了两次 user 就会走 之前代码写的限流方法。
Gateway使用Sentinel限流(重要)
导包
<!-- 限流和gataway使用-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
yaml配置
spring:
cloud:
sentinel:
transport:
dashboard: localhost:1111
限流降级
@Configuration
public class SentinelConfig {
public SentinelConfig(){
GatewayCallbackManager.setBlockHandler(new BlockRequestHandler() {
@Override
public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
return ServerResponse.ok().body(Mono.just("限流啦,请求太频繁"),String.class);
}
});
}
}
熔断
第一种代码
@RestController
public class UserController {
@GetMapping("/user/{id}")
@SentinelResource(value="user",blockHandler="getUserById", fallback = "getByIdfallback")
public User getById(@PathVariable("id")Long id){
// int i = 1 / 0;
return new User(id,"zs");
}
public User getUserById(@PathVariable("id")Long id, BlockException exception){
exception.printStackTrace();
System.out.println("限流了");
return new User(-1L,"限流了");
}
public User getByIdfallback(@PathVariable("id")Long id){
System.out.println("熔断了");
return new User(-1L,"熔断了");
}
}
第二种sentinel设置
这里的降级策略为“RT”,大概意思是:如果并发数大于5 (QPS > 5) ,然后平均响应时间大于200毫秒,那么接下来的2秒钟之内对该资源的请求会被熔断降级。
区别
这个熔断和再sentinel配置的不一样,这个fallback是在你userServer出问题的才会执行,而sentinel是根据你的设置执行
Feign整合Sentinel熔断(重要)
Spring Cloud Alibaba是Spring Cloud的一个子项目,OpenFeign是Spring Cloud的客户端负载均衡器,使用Spring Cloud Alibaba依然可以很方便的集成OpenFeign,如果要使用OpenFeign作为服务客户端负载均衡,那么我们需要考虑OpenFeign开启Sentinel进行服务熔断降级。
开启Sentinel
feign:
sentinel:
enabled: true #熔断
4.2.给Feign接口降级
这里跟Feign开启Hystrix降级一样,还是可以使用fallback属性,写一个接口
@FeignClient(value = "user-server",fallback = UserClientFallback.class)
public interface UserClient {
@GetMapping("/user/{id}")
User getById(@PathVariable Long id);
}
4.3.编写降级类
@Component
public class UserClientFallback implements UserClient {
@Override
public User getById(Long id) {
return new User(-1L,"无此用户","无此用户");
}
}
到这里我们已经完成了Feign和Sentinel的兼容使用,是不是很简单呢,因为它的集成方式和Hystrix简直一模一样