Spring Cloud Nacos as registration and distribution center + Spring Cloud Gateway as a gateway + Sentinel Service as a fuse

Therefore, due to the integration gateway (version selected as the official recommendation spring mix)

springcloud version selection Finchley.SR2

springboot version selection 2.0.6.RELEASE

Here starter nacos choice with the following versions

<nacos.version>0.2.1.RELEASE</nacos.version>
  
  

 

NACOS start

First, download the source code to github, after I downloaded version here choose 0.8.0RELEASE

After a good start to the configuration

 

Registration Center Consolidation

And integration of registries is relatively simple,

First, add dependencies


  
  
  1. <dependency>
  2. <groupId>org.springframework.cloud </groupId>
  3. <artifactId>spring-cloud-starter-alibaba-nacos-discovery </artifactId>
  4. <version>${nacos.version} </version>
  5. </dependency>

Second class on startup add comments 

@EnableDiscoveryClient
  
  

Finally profile configuration


  
  
  1. server.port=12000
  2. spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
  3. management.endpoints.web.exposure.include=*

这样注册要注册中心了http://localhost:8848/nacos/index.html(nacos默认登录用户名是nacos密码也是nacos)

 

整合配置中心

 

这回首先要在nacos中配置相关的配置

加载依赖


  
  
  1. <dependency>
  2. <groupId>org.springframework.cloud </groupId>
  3. <artifactId>spring-cloud-starter-alibaba-nacos-config </artifactId>
  4. <version>${nacos.version} </version>
  5. </dependency>

然后是在配置文件(bootstrap.properties)中加


  
  
  1. # 配置的前缀ata-id
  2. spring.application.name=client
  3. spring.cloud.nacos.config.server-addr=127.0.0.1:8848
  4. # 配置的后缀
  5. spring.cloud.nacos.config.file-extension=properties

最后就和原来使用springcloud一样通过@RefreshScope 和@Value注解即可

 

整合网关

加载依赖


  
  
  1. <dependency>
  2. <groupId>org.springframework.cloud </groupId>
  3. <artifactId>spring-cloud-starter-gateway </artifactId>
  4. </dependency>

加入以下配置


  
  
  1. server:
  2. port: 13000
  3. spring:
  4. application:
  5. name: gateway
  6. cloud:
  7. gateway:
  8. discovery:
  9. locator:
  10. enabled: true #表明gateway开启服务注册和发现的功能,并且spring cloud gateway自动根据服务发现为每一个服务创建了一个router,这个router将以服务名开头的请求路径转发到对应的服务。
  11. lowerCaseServiceId: true #是将请求路径上的服务名配置为小写(因为服务注册的时候,向注册中心注册时将服务名转成大写的了),比如以/service-hi/*的请求路径被路由转发到服务名为service-hi的服务上。
  12. filters:
  13. - StripPrefix=1
  14. nacos:
  15. config:
  16. server-addr: 127.0.0.1:8848
  17. discovery:
  18. server-addr: 127.0.0.1:8848

 

 

结果展示

网关

http://localhost:13000/client/hello

-> client

http://localhost:12000/client/hello

client中的配置属性来自nacos

 

 

返回的结果通过调用client2

结果如下

 

Sentinel 整合

首先下载sentinel的工程,然后找到里面的dashboard启动

依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>${nacos.version}</version>
</dependency>

 

因为目前sentinel没有直接支持gateway,所以这里仅仅在client里进行配置和应用,

具体配置如下

@Bean
@LoadBalanced
@SentinelRestTemplate(blockHandler = "handleException", blockHandlerClass = ExceptionUtil.class)
RestTemplate restTemplate(){
    return new RestTemplate();
}
@RequestMapping(value = "/get", method = GET)
@ResponseBody
@SentinelResource(blockHandler = "handleException", blockHandlerClass = ExceptionUtil.class)
public List<Instance> get(@RequestParam String serviceName) throws NacosException {
    return namingService.getAllInstances(serviceName);
}

这样就能做到服务调用的限流以及服务资源提供的限流,可以再dashboard上看到

 

遇到的坑

在整合过程中遇到比较多的坑, 比如nacos的data-id要配置后缀名,不然找不到配置, 以及在网关配置的时候需要配置成 '*', 不能直接写出*.

当然其实经历过eureka+springcloud config 的搭建以及,用zookeeper作为配置中心的搭建, 所以替换nacos也是比较简单.

如果要开启数据存储配置的话需要在以下文件中配置相关属性

 

 

对比

与eureka对比

从官网来看nacos 的注册的实例数是大于eureka的,然后因为nacos使用的raft协议,所以一致性要远大于eureka.

与springcloud config 

nacos的配置中心感觉和apollo类似,都有使用长轮询, 然后配置变动后通知非常的迅速, 秒杀springcloud原来的config几条街,毕竟原来的config是基于git, 不提供可视化界面, 动态变更还需要依赖bus来通过所有的客户端变化. 

与hystrix

sentinel更加的轻量级,并且支持动态的限流调整,更加友好的界面ui

 

更多的功能等待发掘...

 

github地址: https://github.com/cdy1996/sample-springcloud/tree/nacos

 

 

发布了19 篇原创文章 · 获赞 149 · 访问量 80万+

Guess you like

Origin blog.csdn.net/truelove12358/article/details/103136968