基于上文,SOFARPC 是近期蚂蚁金服开源的一个高可扩展性、高性能、生产级的 Java RPC 框架。在蚂蚁金服 SOFARPC 已经经历了十多年的发展。SOFARPC 致力于简化应用之间的 RPC 调用,为应用提供方便透明、稳定高效的点对点远程服务调用方案。为了用户和开发者方便的进行功能扩展,SOFARPC 提供了丰富的模型抽象和可扩展接口,包括过滤器、路由、负载均衡等等。
SOFA RPC 可以集成多种注册中心实现,其中一种就是常用的 ZooKeeper。
ZooKeeper 作为一个开源的分布式应用协调系统,已经用到了许多分布式项目中,用来完成统一命名服务、状态同步服务、集群管理、分布式应用配置项的管理等工作。
本文将介绍 SOFARPC 使用 ZooKeeper 作为注册中心的用法。
1、下载zookeeper
第一步:去官网下载 zookeeper.apache.org/releases.ht…
,我们下载压缩包zookeeper-3.6.2.tar.gz,然后解压到文件夹下,例如 /home/admin/zookeeper-3.6.2。
第二步:设置配置文件,可以直接从样例复制一份。
$ cd /home/admin/zookeeper-3.6.2
$ cp conf/zoo_sample.cfg conf/zoo.cfg
第三步:到 Zookeeper 安装目录下直接启动Zookeeper。
$ cd /home/admin/zookeeper-3.6.2
$ sh bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /Users/zhanggeng/dev/zookeeper/bin/../conf/zoo.cfg
-n Starting zookeeper ...
STARTED
第四步:如果需要查看数据,直接运行 zkCli.sh,连接后执行 ls /即可。
$ sh bin/zkCli.sh
Connecting to localhost:2181
......
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
2、SOFARPC 集成 Zookeeper 注册中心
SOFABoot 是蚂蚁金服开源的基于 Spring Boot 的研发框架,它在增强了 Spring Boot 的同时,SOFABoot 提供了让用户可以在 Spring Boot 中非常方便地使用 SOFAStack 相关中间件的能力。
SOFARPC 也实现以一个 rpc-sofa-boot-starter 可以方便的集成到 SOFABoot 应用。目前只支持Spring XML 方式发布和引用服务,下一个版本将支持 Annotation 方式发布和引用服务。
2.1 创建 SpringBoot 工程
SOFABoot 运行需要 JDK 7 及以上、 Maven 3.2.5 以上。
2.2 引入 SOFABoot 和 rpc-sofa-boot-starter
我们将工程导入到 IDE 中,然后在 pom.xml 将 Spring Boot 工程转为一个 SOFABoot 工程,很简单,只要加入依赖管控即可。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofaboot-dependencies</artifactId>
<version>2.3.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
然后再在 pom.xml 中引入 rpc-sofa-boot-starter 的依赖:
<dependencies>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>rpc-sofa-boot-starter</artifactId>
<version>5.3.1</version>
</dependency>
</dependencies>
2.3 编写服务提供端
第一步:创建接口
package org.howtimeflies.sofa.rpc;
public interface HelloService {
public String sayHello(String name);
}
第二步:创建接口实现
package org.howtimeflies.sofa.rpc;
public class HelloServiceImpl implements HelloService {
public String sayHello(String name) {
return "hello " + name;
}
}
第三步:发布服务
我们通过 SpringBean 的方式发布服务,新建一个 Spring 的 xml,例如 src/main/resource/rpc-server.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:sofa="http://sofastack.io/schema/sofaboot"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://sofastack.io/schema/sofaboot
http://sofastack.io/schema/sofaboot.xsd"
default-autowire="byName">
<bean id="helloServiceImpl" class="org.howtimeflies.sofa.rpc.HelloServiceImpl"/>
<sofa:service interface="org.howtimeflies.sofa.rpc.HelloService" ref="helloServiceImpl">
<sofa:binding.bolt/>
</sofa:service>
</beans>
2.4 编写服务调用端
同样服务端调用端也通过 SpringBean 的方式引用一个服务。新建一个 Spring 的 xml,例如 src/main/resource/rpc-client.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:sofa="http://sofastack.io/schema/sofaboot"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://sofastack.io/schema/sofaboot
http://sofastack.io/schema/sofaboot.xsd"
default-autowire="byName">
<sofa:reference id="helloServiceRef" interface="org.howtimeflies.sofa.rpc.HelloService">
<sofa:binding.bolt/>
</sofa:reference>
</beans>
2.5 指定注册中心地址
我们需要在 src/main/resource/application.properties 里指定我们的应用名和注册中心地址
# 指定应用名
spring.application.name=test
# 指定日志路径
logging.path=./logs
# 注册中心地址
com.alipay.sofa.rpc.registry.address=zookeeper://127.0.0.1:2181
2.6 运行
我们在生成代码里找到了默认的启动类 XXXApplication.java,名字自动生成的,例如本例是为:org.howtimeflies.sofa.rpc.SofaRpcSofaBootZookeeperDemoApplication。
它的原始内容如下:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SofaRpcSofaBootZookeeperDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SofaRpcSofaBootZookeeperDemoApplication.class, args);
}
}
可以看到里面并未指定加载的文件,我们将启动类改造下,引入 Spring XML 的配置,以及我们的调用代码,如下:
package org.howtimeflies.sofa.rpc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource({"rpc-server.xml","rpc-client.xml"}) // 引入加载的 Spring XML
public class SofaRpcSofaBootZookeeperDemoApplication {
public static void main(String[] args) {
ApplicationContext context =
SpringApplication.run(SofaRpcSofaBootZookeeperDemoApplication.class, args);
// 等待ZooKeeper下发地址
try {
Thread.sleep(2000);
} catch (Exception e) {
}
// 拿到调用端 进行 调用
HelloService helloService = (HelloService) context.getBean("helloServiceRef");
String hi = helloService.sayHello("world");
System.out.println(hi);
}
}
直接运行 SofaRpcSofaBootZookeeperDemoApplication,结果如下:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.2.RELEASE)
......
2018-04-17 21:42:13.249 INFO 20211 --- [ main] .SofaRpcSofaBootZookeeperDemoApplication : Started SofaRpcSofaBootZookeeperDemoApplication in 5.958 seconds (JVM running for 6.75)
hello world