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"?>
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.启动服务
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();
}
}