-
Notifications
You must be signed in to change notification settings - Fork 192
zh_quick_start
郑大侠 edited this page Dec 25, 2016
·
3 revisions
- 快速入门
- 简单调用示例
- 集群调用示例
快速入门中会给出一些基本使用场景下的配置方式,更详细的使用文档请参考用户指南.
如果要执行快速入门介绍中的例子,你需要:
JDK 1.7或更高版本。 java依赖管理工具,如Maven或Gradle。
在pom中添加依赖
<dependency>
<groupId>com.zhizus</groupId>
<artifactId>forest-rpc</artifactId>
<version>0.0.2</version>
</dependency>
<dependency>
<groupId>com.zhizus</groupId>
<artifactId>forest-common</artifactId>
<version>0.0.2</version>
</dependency>
1.定义接口
通过注解@ServiceProvider暴露服务,通过@MethodProvider暴露方法默认配置,如:压缩方式,序列化方式,客户端超时时间
@ServiceProvider(serviceName = "sampleService", haStrategyType = HaStrategyType.FAIL_FAST,
loadBalanceType = LoadBalanceType.RANDOM, connectionTimeout = Constants.CONNECTION_TIMEOUT)
public interface SampleService {
@MethodProvider(methodName = "say")
String say(String str);
@MethodProvider(methodName = "echo", serializeType = SerializeType.Hession2, compressType = CompressType.None)
String echo(String msg);
}
2.实现接口
基于注解@ServiceExport发布服务,基于注解 @MethodExport发布方法,可同时支持jersey发布简单的restful服务
@Path("/sample")
@ServiceExport
public class SampleServiceImpl implements SampleService {
/**
* 支持jersey,可以通过配置打开,同时启动http服务
*
* @param str
* @return
*/
@Path("/hello/{str}")
@GET
@Produces("text/plain")
@MethodExport
@Rate(2)
@Override
public String say(@PathParam("str") String str) {
return "say " + str;
}
@Interceptor("metricInterceptor")
@MethodExport
@Override
public String echo(String msg) {
return "echo>>> " + msg;
}
}
3.服务端开发
spring context 配置:
application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.zhizus.forest.demo"/>
<bean id="forestServer" class="com.zhizus.forest.support.spring.ForestServerBean"/>
</beans>
Server开发
public class SampleServer {
public static void main(String[] args) throws Exception {
new ClassPathXmlApplicationContext(new String[]{"application.xml"});
}
}
4.客户端开发
SampleService sampleService = Forest.from(SampleService.class, ServiceProviderConfig.Builder.newBuilder()
.withMethodConfig("say", MethodConfig.Builder.newBuilder()
.withCompressType(CompressType.None)
.withSerializeType(SerializeType.Fastjson)
.build())
.withMethodConfig("echo", MethodConfig.Builder.newBuilder()
.withCompressType(CompressType.None)
.withSerializeType(SerializeType.Hession2)
.build())
.build());
String hello = sampleService.say("hello");
在集群环境下,forest要依赖外部的服务发现组件,目前仅支持zookeeper
ZooKeeper安装与启动 (请参考zookeeper官方文档)
1.在server spring配置xml中定义如下:
<bean id="registry" class="com.zhizus.forest.common.registry.impl.ZkServiceDiscovery">
<property name="connStr" value="localhost:2181"/>
</bean>
<bean id="forestServer" class="com.zhizus.forest.support.spring.ForestServerBean">
<property name="registry" ref="registry"></property>
</bean>
2.client spring配置xml定义如下:
<bean id="zkRegistry" class="com.zhizus.forest.common.registry.impl.ZkServiceDiscovery">
<property name="connStr" value="localhost:2181"/>
</bean>
<bean id="sampleServiceProxy" class="com.zhizus.forest.support.spring.ForestProxyFactoryBean">
<property name="serviceInterface" value="com.zhizus.forest.demo.api.SampleService"/>
<!--注册本地-->
<property name="discovery" ref="zkRegistry"/>
</bean>