JSF服务框架初识

京东的JSF

1.是什么?

JSF(杰夫)是一个高性能的服务框架,特性如下:

  • 可以进行高效RPC(远程过程)调用
  • 有高可用的注册中心,完备的容灾特性
  • 服务端口同时支持TCP与HTTP协议调用,支持跨语言调用
  • 支持msgpack、json等多种序列化格式,支持数据压缩
  • 提供黑白名单、负载均衡、provider动态分组、动态切换调用分组等服务治理功能
  • 提供对接口-方法的调用次数、平均耗时等在线监控报表功能
  • 兼容SAF协议,可以调用SAF1.X接口
  • 全部模块均为自主研发,自主设计应用层JSF协议;各模块功能可控,可扩展性较好

2.用在哪?

  • 适合于分布式架构
  • 服务与服务之间的同步调用
  • 数据量小(单个请求小于20K)并发大的场景

3.怎么用?

引入依赖:

    com.jd

    jsf

    版本

发布服务:

1.新建服务接口类:

public interface HelloService {
    public String echoStr(String str);
}
 
2.新建服务实现类:
public class HelloServiceImpl implements HelloService {
    private static Logger logger = LoggerFactory .getLogger(HelloServiceImpl. class );
    @Override
    public String echoStr(String str) {
        logger.info( "server get request : {}" , str);
        return str;
    }
}
 

3. 配置一下spring xml文件 (注意,文件头部要引入jsf.xsd相关配置

xml version="1.0" encoding="UTF-8"?>

http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jsf="http://jsf.jd.com/schema/jsf"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

    http://jsf.jd.com/schema/jsf http://jsf.jd.com/schema/jsf/jsf.xsd">

   

   

   

   

   

   

   

   

                 ref="helloServiceImpl" server="jsf" >

   

 

4.启动服务

public class ServerMain {
    private final static Logger LOGGER = LoggerFactory.getLogger(ServerMain.class);
    public static void main(String[] args) {
        ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("/jsf-provider.xml");
        LOGGER.info("服务端启动完成!");
        // 启动本地服务,然后hold住本地服务
        synchronized (ServerMain.class) {
            while (true) {
                try {
                    ServerMain.class.wait();
                } catch (InterruptedException e) {
                }
            }
        }
    }
}
 
5.去注册中心查看服务是否启动成功

调用服务:

1.引用接口类:服务匹配是依靠 interface(接口) 以及 alias(别名)两个配置来完成的:

   因此调用端(Service Consumer)必须与服务端(Service Provider)的interface 与alias 属性配置必须完全一致,否则调用时会收到一个No Provider(没有服务提供者)的错误!

 

2. 配置一下spring 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:jsf = "http://jsf.jd.com/schema/jsf"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://jsf.jd.com/schema/jsf http://jsf.jd.com/schema/jsf/jsf.xsd">
   
    < jsf:registry id = "jsfRegistry" protocol = "jsfRegistry" index = "i.jsf.jd.com" />
   
    < jsf:consumer id = "helloService" interface = "com.jd.testjsf.HelloService"
                   protocol = "jsf" alias = "CHANGE-IT" timeout = "10000" >
    jsf:consumer >
beans >
 
3. 注入到代码中进行调用
 
   有三种调用方式:
  • 通过applicationContext.getBean(name)获取
  • 使用spring的getter/setter注入
  • 使用@AutoWired或者@Resource注入

用第一种方式调用:

public class ClientMain {
    private final static Logger LOGGER = LoggerFactory.getLogger(ClientMain. class );
    public static void main(String[] args) {
        ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext( "/jsf-consumer.xml" );
        HelloService service = (HelloService) appContext.getBean( "helloService" );
        LOGGER.info( "得到调用端代理:{}" , service);
        while ( true ) {
            try {
                String result = service.echoStr( "zhanggeng put" );
                LOGGER.info( "response msg from server :{}" , result);
            } catch (Exception e) {
                LOGGER.error(e.getMessage(), e);
            }
            try {
                Thread.sleep( 2000 );
            } catch (Exception e) {
            }
        }
        // JSFContext.destroy();
    }
}
 
 4.看到打印如下日志,说明发布调用服务成功。
  服务端: server get request : zhanggeng put
 客户端:response msg from server : zhanggeng put

你可能感兴趣的:(企业型开发)