Spring Cloud Alibaba 微服务进阶实战 网盘下载

1,247 阅读4分钟

下载地址:Spring Cloud Alibaba 微服务进阶实战 网盘下载

Spring Cloud Alibaba介绍

它为微服务提供了流量控制和融合降解的功能。它具有与hystrix相同的功能,可以有效地解决微服务调用的“雪崩”效应,为微服务系统提供稳定的解决方案。随着hytrxi进入维护期,不再提供新功能,sentinel是一个很好的选择。通常,hystrix使用一个线程池来隔离服务调用,Sentinel使用用户线程来隔离接口。与两者相比,hystrxi是服务级隔离,sentinel提供接口级隔离,sentinel隔离级别更精细,此外sentinel直接使用用户线程来限制,与hystrx的线程池隔离相比,减少了线程切换的开销。此外,sentinel的仪表板提供了更改当前限制规则的在线配置。也更优化。 从官方文献介绍来看,sentinel具有以下特点: 丰富的应用场景:近10年,sentinel一直承担着阿里巴巴双十一大流量推广的核心场景。例如spike(即系统容量范围内的突发流量控制)、消息峰值削峰填谷、实时断开下游不可用应用。完整的实时监控:sentinel还提供实时监控功能。您可以在控制台中看到连接到应用程序的单个计算机的二级数据。聚集运行的集群少于500个单位。广泛的开源生态系统:sentinel提供了与其他开源框架/库的开箱可用的集成模块,例如与spring cloud、dubbo、grpc的集成。您只需要引入相应的依赖项并执行简单的配置就可以快速访问sentinel。完整的spi扩展点:sentinel提供易于使用和完整的spi扩展点。您可以通过实现扩展点、Quick自定义逻辑来实现这一点。例如,自定义规则管理、自适应数据源等。

Spring Cloud Alibaba 微服务进阶实战

如何在spring cloud Alibaba中使用sentinel

sentinel作为阿里巴巴的组成部分之一,将其用于spring cloud项目非常简单。现在让我们以案例的形式解释如何在spring cloud项目中使用sentinel。这个项目是基于之前的nacos教程案例。将sentinel的spring cloud初始依赖项添加到项目的pom文件中,代码如下:

<dependency>
  <groupid>org.springframework.cloud</groupid>
  <artifactid>spring-cloud-starter-alibaba-sentinel</artifactid>
  <version>0.9.0.release</version>
</dependency>
server:
 port:8762
spring:
 application:
 name:nacos-provider
 cloud:
 nacos:
  discovery:
  server-addr:127.0.0.1:8848
 sentinel:
  transport:
  port:8719
dashboard:localhost:8080
@restcontroller
public class providercontroller {
 @getmapping ("/hi")
 @sentinelresource (value="hi")
 public string hi (@requestparam (value="name", defaultvalue="forezp", required=false) string name) {
  return "hi" + name;
 }
}
@restcontroller
public class providercontroller {
 @getmapping ("/hi")
 @sentinelresource (value="hi")
 public string hi (@requestparam (value="name", defaultvalue="forezp", required=false) string name) {
  return "hi" + name;
 }
}@restcontroller
public class providercontroller {
 @getmapping ("/hi")
 @sentinelresource (value="hi")
 public string hi (@requestparam (value="name", defaultvalue="forezp", required=false) string name) {
  return "hi" + name;
 }
}
<dependency>
<groupid>org.springframework.cloud</groupid>
<artifactid>spring-cloud-starter-openfeign</artifactid>
</dependency>
<dependency>
<groupid>org.springframework.cloud</groupid>
<artifactid>spring-cloud-starter-alibaba-sentinel</artifactid>
<version>0.9.0.release</version>
</dependency>
In the configuration file, in addition to the sentinel.transport. Dashboard configuration, you also need to add the feign.sentinel.enabled configuration. The code is as follows:

server:
 port:8763
spring:
 application:
 name:nacos-consumer
 cloud:
 nacos:
  discovery:
  server-addr:127.0.0.1:8848
 sentinel:
  transport:
  port:8719
  dashboard:localhost:8080
feign.sentinel.enabled:true
Write a feignclient and call the/hi interface of nacos-provider:

@feignclient ("nacos-provider")
public interface providerclient {
 @getmapping ("/hi")
 string hi (@requestparam (value="name", defaultvalue="forezp", required=false) string name);
}
Write a restcontroller to call providerclient, the code is as follows:

@restcontroller
public class consumercontroller {
 @autowired
 providerclient providerclient;
 @getmapping ("/hi-feign")
 public string hifeign () {
  return providerclient.hi ("feign");
 }
}

Spring Cloud Alibaba 微服务进阶实战

Spring Cloud Alibaba 微服务进阶实战: 基于Nacos实现Gateway动态网关

动态路由是指路由器可以自动建立自己的路由表,并根据实际情况的变化及时进行调整。动态路由器上的路由表项通过互联路由器相互交换信息,并按照一定的算法进行优化;路由信息在一定的时间间隔内不断更新,以适应不断变化的网络,获得最优的寻路效果。 最初的操作是: 动态路由机制的运行依赖于两个基本功能

  1. Nacos环境准备 不引入特定的Nacos配置。你可以参考阿里巴巴的官方介绍。在这里,您可以通过windows直接启动单机模式,并登录到Nacos Console。创建名称空间dev,并在dev下默认分组下创建网关-路由器的dataId Spring Cloud Alibaba 微服务进阶实战:Nacos环境准备

[{    "id": "consumer-router",    "order": 0,    "predicates": [{        "args": {            "pattern": "/consume/**"        },        "name": "Path"    }],
    "uri": "lb://nacos-consumer"
},{
    "id": "provider-router",
    "order": 2,
    "predicates": [{
        "args": {
            "pattern": "/provide/**"
        },
        "name": "Path"
    }],
    "uri": "lb://nacos-provider"
}]
  1. 连接到Nacos配置中心 通常,项目中“配置中心”的配置通常是在引导中配置的。propertis (yaml),以确保从Nacos Config读取项目中的路由配置。
# nacosConfiguration Center configuration is recommended to be configured in bootstrap.properties
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
#spring.cloud.nacos.config.file-extension=properties
 # Configuration center namespace: dev namespace (environment)
spring.cloud.nacos.config.namespace=08ecd1e5-c042-410a-84d5-b0a8fbeed8ea
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication
{
    public static void main( String[] args )
    {
        SpringApplication.run(GatewayApplication.class, args);
    }
}