SAF是 Service Assembly Framework(服务组装框架)的缩写。它提供基于服务注册订阅中心的高效点对点服务调用,它具有如下特性:
高性能的Service调用框架
长链接协议SAF、dubbo(基于nio和长连接的远程调用实现);
多协议支持,如RMI、Hessian、WebServices等;
支持FailOver,在服务调用出现异常后,自动切换Provider重试;
支持多种Load balance策略 如:权重、随机或轮询算法;
2.0将提供多语言客户端;
提供基础的服务治理功能
解决服务Provider和服务Consumer之间的匹配问题(根据接口、版本、group分组 来区分);
服务注册和订阅功能;
灵活的服务路由规则设置;
服务列表动态推送功能;
使用SAF框架时调用示意图:
架构设计
SAF配置
2.1 在项目中引入SAF
当使用SAF服务架构框架时,只需引入SAF提供的jar包即可,引入方式:在pom.xml文件中加入如下代码:
<dependency>
<groupId>com.jd</groupId>
<artifactId>saf</artifactId>
<version>版本号,目前最新正式版1.0.9</version>
</dependency>
如果需要发布Webservices等,还得在项目中单独引入cxf的jar包。
如果rest,则单独引入rest相关的jar包
2.2 spring集成的注册中心配置
2.2.1 发布服务
spring 配置文件provider.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:saf="http://code.360buy.com/schema/saf"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd_
http://code.360buy.com/schema/saf " target="_blank">http://code.360buy.com/schema/saf/saf.xsd">
<!-- 业务实现类 -->
<bean id="helloService" class="com.jd.bk.saf.config.test.HelloServiceImpl"/>
<!-- 注册中心配置,服务提供者连接注册中心-->
<saf:registry protocol="jdZooKeeper" id="jdZooKeeper" address="192.168.229.53:2181,192.168.229.54:2181,192.168.229.55:2181" />
<!-- 指定发布的协议 -->
<saf:protocol id="mydubbo" name="dubbo" port="20880" />
<!-- 暴露及注册服务 -->
<saf:service id="helloIns" interface="com.jd.bk.saf.config.test.HelloService" ref="helloService" group="ZG1" protocol="mydubbo" version="0.0.0" registry="jdZooKeeper" >
</saf:service>
</beans>
启动服务。
package com.jd.bk.saf.config.test;
public class ProviderDemo{
public static void main(String[] args) {
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("/provider.xml");
appContext.start();
// hold住服务
synchronized (ConsumerDemo.class) {
while(true) {
try {
ConsumerDemo.class.wait();
} catch (InterruptedException e) {
}
}
}
}
}
2.2.2 调用服务
首先服务消费者要连接注册中心,在相应的xml文件中进行配置
注意: 在连接注册中心时, 消费者和服务者xml文件中的 version,group,protocol必须相同.
consumer.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:saf="http://code.360buy.com/schema/saf"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd_
http://code.360buy.com/schema/saf " target="_blank">http://code.360buy.com/schema/saf/saf.xsd">
<!-- 连接注册中心-->
<saf:registry id="jdZooKeeper" protocol="jdZooKeeper" address="192.168.229.53:2181,192.168.229.54:2181,192.168.229.55:2181" />
<!-- 引用远程服务 -->
<saf:reference id="helloRefer" interface="com.jd.bk.saf.config.test.HelloService" version="0.0.0" group="ZG1"
protocol="dubbo" timeout="5000" check="false" />
</beans>
可以直接getBean得到服务,也可以@Autowired 注入。
package com.jd.bk.saf.config.test;
public class ConsumerDemo {
public static void main(String[] args) {
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("/consumer.xml");
HelloService helloBean = (HelloService)appContext.getBean("helloRefer");
System.out.println(helloBean.hello("world"));
}
}
3. 注册中心和管理端地址
4. 最佳实践
1.在开发阶段,采用URL直连方式,在测试发布阶段以及正式上线的时候再使用注册中心;使用直连方式时,在服务引用的声明中指明相应URL:
<saf:reference ...url="dubbo://10.20.130.230:20880/com.xxx.XxxService".... />
2.在开发阶段debug时,将reties参数设为0,避免自动重试干扰debug:
<saf:reference ... retries='0'.../>
3.如想配置SAF使用随机端口,请将port的值置为-1。
4.服务器多网卡多IP时,可能会取外网IP用来注册,为了解决这个问题,可在<saf:protocol>中设置host,配置如下:
<saf:protocol id="mydubbo" name="dubbo" host="${webservice.url.ip}" />
5.开发环境和测试环境使用不同的group,防止调用错了。
5. SAF管理端报警功能
功能说明: SAF管理端目前实现了如下报警方式
(1) SAF服务的provider数量报警. 当一个服务的provider数量小于设定的阀值时, 则发送邮件和短信通知相关负责人.
(2) SAF服务的consumer数量报警. 当一个服务的consumer数量小于设定的阀值时, 则发送邮件和短信通知相关负责人.
(3) SAF服务服务上下线报警. 当一个服务下线后, 如果三分钟之内没有重新上线, 则发送邮件和短信通知相关负责人,否则不与报警; 如果下线三分钟后又重新上线, 则发送邮件和短信通知重新上线.
一,点击服务注册管理中心--左边菜单栏的 管理服务 菜单,进入界面: 即可添加需要监控的接口,界面显示为:
6. FAQ
虚拟机部署问题:
1.虚拟机由于资源有限,请缩小线程池为20(默认200),配置 <saf:protocol threads="20" />;
配置每线程所使用栈内存大小,设置JVM参数 -Xss=256K,并查看ulimt -u ulimit -s确认是否满足需求;
43.启动时提示 Failed to start NettyClient BJXX-LIUSULI-T/10.8.6.20 connect to the server /10.8.1.51:36072
更多问题解决方案,请下载文件,看详情:
链接:https://pan.baidu.com/s/1KFwoL0xH-zXI-5VtaJSjhQ
提取码:8g5u