【dubbo系列】dubbo与Sentinel整合篇

阿里巴巴中间件公众号了解的Sentinel框架,sentinel字面意思为哨兵,开始以为是redis的sentinel哨兵,了解后才发现并不是。
微服务流行,相信很多团队拆分服务,进行服务和服务之间调用,Sentinel是分布式架构体系中流量控制框架,主要以流量为切入点,熔断降级,系统保护等功能额,来保护系统稳定性。

Sentinel简要介绍:
流量控制功能:
提供服务负载能力有限,为防止某个服务流量过大,导致拖垮了整个系统的其它服务,可以对其接口流量监控,对其进行处理。sentinel具体的怎么进行流量控制,下篇文章进行介绍。

熔断降级
某个服务突然流量很大,调用料率特别不稳定,会导致不可用,占用很大内存,cpc,jvm,导致整个系统的服务都会不可用。Hystrix后期也会陆续介绍一下。

系统负载保护
在熔断降级的时候,说了,分布式架构系统中,某个节点某个服务压力特别大,如果处理不及时会拖垮这个节点,系统负载保护,言外之意就是如果此节点此服务压力特别大,可把到达这个请求直接转移到其它的无压力能处理的请求的节点,可减轻此节点的压力,起到高可用服务的设计。

本应用需要关键技术、dubbo、spring、sentinel、zookeeper、maven
环境下面开始准备、

Sentinel目前已经公开的最新的版本1.3.0-GA版本,本系列篇也是根据这个版本来做使用。

pom.xml关键引用

<!-- https://mvnrepository.com/artifact/com.alibaba.csp/sentinel-dubbo-adapter -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-dubbo-adapter</artifactId>
            <version>1.3.0-GA</version>
        </dependency>

        <!-- 客户端接入sentinel控制台需要的jar –> --> 
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-transport-simple-http</artifactId>
            <version>1.3.0-GA</version>
        </dependency>

解释:
sentinel-dubbo-adapter包是来适配dubbo的jar包,sentinel目前是适配http、dubbo、springcloud、grpc、springboot,每个都有适配的jar包。
sentinel-transport-simple-http是用来和Sentinel控制台通信的jar包,往控制台发送心跳包。控制台是什么?下面会介绍使用启动方式。

可能有些私服会下载不了这两个jar包文件,目前我用的是maven的settings.xml文件是下面的网址,此网址可下载sentinel的有关jar包,当然,有同学有更好的网址也可使用你们的,随意。

 <mirror>  
    <id>repo2</id>  
    <mirrorOf>central</mirrorOf>  
    <name>Human Readable Name for this Mirror.</name>  
    <url>http://repo2.maven.org/maven2</url>
     <!--<url>https://mvnrepository.com/</url>  -->

    </mirror>

项目已上传到我的github上,地址是:
https://github.com/growup818/dubbo-monitor/tree/master/sentinel-monitor

下面是项目架构图,采用dubbo分布式分层的架构,进行使用,spring配置文件,并不是官方用的springboot版本,此版本是用来测试和使用,目前精简版本,可以直接使用。

系统架构图:
【dubbo系列】dubbo与Sentinel整合篇

服务端层
sentinel-dubbo-plat-model是传统使用的model层,存放param,vo,model类
sentinel-dubbo-plat-dao层存放数据层,也就是和数据库交互的(本篇不设计数据库的方面,无此层)
sentinel-dubbo-plat-rpc-interface层是接口层,存放接口类
sentinel-dubbo-plat-rpc-interface-impl是接口实现层,存放接口实现类,spring配置文件,是服务端的web层,具体可下载下来进行查看。

客户端层:
sentinel-dubbo-plat-rpc-web,是客户端层,用来和前端页面通信的层,是dubbo消费者层,来调用服务端也就是dubbo提供者服务的。

上面介绍了概念,以及一些本系统的架构,下面开始此篇的启动服务篇。

按顺序进行下面步骤启动。
zookeeper启动
由于启动dubbo服务需要zookeeper,可以谷歌一下,搜索一下安装教程吧,不在这里多说了。

服务端启动
sentinel-dubbo-plat-rpc-interface-impl一个war包,需要一个tomcat,进行启动,注意,此项目是采用了分环境的,
【dubbo系列】dubbo与Sentinel整合篇

三套环境,online,qatest,rdtest三套
本人采用的是eclipse进行开发的,在tomcat里配置。

图片一
【dubbo系列】dubbo与Sentinel整合篇

第二种办法:
找到spring-applicationContext.xml 文件,

<!-- 集中加载配置文件 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:properties/dubbo/${sys.server.type}/dubbo.properties</value>
            </list>
        </property>
    </bean>

${sys.server.type} 改成rdtest或qatest或online即可

直接启动,启动日志:

十一月 17, 2018 8:06:55 下午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'dispatcherServlet'
十一月 17, 2018 8:06:58 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-nio-8083"]
十一月 17, 2018 8:06:58 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-nio-8012"]
十一月 17, 2018 8:06:58 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 14582 ms

好了,服务端可以启动了。

客户端启动:
客户端启动也是同理,参照服务端启动即可,注意服务端和客户端启动的tomcat端口是否一样,修改成不一样,否则端口占用。

客户端启动需要加入进去几个参数:
也是跟服务端一样的路径下加入配置,
eclipse->servers->双击后按照下面的步骤进行配置
参考图片一的配置,加入进去这几个参数,或在tomcat的catalina.sh 文件里加入这些参数也可以,本人是在eclipse环境下

-Dsys.server.type="rdtest"  (环境)
-Djava.net.preferIPv4Stack=true 
-Dcsp.sentinel.api.port=8088  
-Dcsp.sentinel.dashboard.server=127.0.0.1:8081 控制台ip和地址
-Dproject.name=dubbo-service-demo  工程名字

客户端启动日志:

INFO: log base dir is: C:\Users\sdc\logs\csp\
INFO: log name use pid is: false
十一月 17, 2018 8:08:25 下午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'dispatcherServlet'
十一月 17, 2018 8:08:29 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-nio-8088"]
十一月 17, 2018 8:08:29 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-nio-8023"]

INFO: log base dir is: C:\Users\sdc\logs\csp\

这个路径是sentinel的初始化路径,里面会存放一些重要文件,用于推送信息,和sentinel控制台交互的,启动后注意一下。

现在dubbo服务端和客户端都已经启动了,开始启动Sentinel控制台。

在你的本地库找到
sentinel-dashboard-1.3.0.jar这个jar包,这个包是sentinel控制台jar包,springboot的简易的一个服务。

控制台启动命令:
java -Dserver.port=8081 -jar sentinel-dashboard-1.3.0.jar
8081是你的控制台端口

日志如下:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.5.RELEASE)

2018-11-17 20:24:00 [main] INFO  c.t.c.s.d.DashboardApplication - Starting DashboardApplication on DESKTOP-V5QV6BS with PID 2788 (C:\Users\sdc\Desktop\sentinel\sentinel-dashboard-1.3.0.jar started by sdc in C:\Users\sdc\Desktop\sentinel)
2018-11-17 20:24:00 [main] INFO  c.t.c.s.d.DashboardApplication - No active profile set, falling back to default profiles: default
2018-11-17 20:24:00 [main] INFO  o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@46f5f779: startup date [Sat Nov 17 20:24:00 CST 2018]; root of context hierarchy

启动成功后,访问:
控制台地址:http://127.0.0.1:8081 8081是你的启动的端口

访问成功会看到如下页面。

【dubbo系列】dubbo与Sentinel整合篇

红色框框是Sentinel统计的服务。

初始化的时候是无数据的,dubbo客户端启动成功后,浏览器输入地址:
http://127.0.0.1:8088/sentinel-monitor/demo/test
多输入几次就可以看见控制台有数据了。

项目已上传到我的github上,地址是:
https://github.com/growup818/dubbo-monitor/tree/master/sentinel-monitor

参考
阿里巴巴Sentinel开源地址:
https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D

本人原创,如有雷同,请告知,谢谢!

猜你喜欢

转载自blog.51cto.com/shangdc/2318331