XXL-RPC 是一个分布式服务框架,提供稳定高性能的RPC远程服务调用功能。拥有"高性能、分布式、注册中心、负载均衡、服务治理"等特性。
官网地址:http://www.xuxueli.com/xxl-rpc/#/
引入xxl-rpc-core核心依赖与公共api接口依赖
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-rpc-core</artifactId>
<version>${parent.version}</version>
</dependency>
xxl-rpc.remoting.port=7080 // 提供服务的端口
xxl-rpc.registry.zk.zkaddress=127.0.0.1:2181 //注册中心zk的地址
xxl-rpc.registry.zk.zkdigest=
xxl-rpc.env=test
书写spring配置类
@Configuration
public class XxlRpcProviderConfig {
private Logger logger = LoggerFactory.getLogger(XxlRpcProviderConfig.class);
@Value("${xxl-rpc.remoting.port}")
private int port;
@Value("${xxl-rpc.registry.zk.zkaddress}")
private String zkaddress;
@Value("${xxl-rpc.registry.zk.zkdigest}")
private String zkdigest;
@Value("${xxl-rpc.env}")
private String env;
@Bean
public XxlRpcSpringProviderFactory xxlRpcSpringProviderFactory() {
XxlRpcSpringProviderFactory providerFactory = new XxlRpcSpringProviderFactory();
providerFactory.setPort(port);
if (zkaddress != null) {
providerFactory.setServiceRegistryClass(ZkServiceRegistry.class);
providerFactory.setServiceRegistryParam(new HashMap<String, String>(){{
put(Environment.ZK_ADDRESS, zkaddress);
put(Environment.ZK_DIGEST, zkdigest);
put(Environment.ENV, env);
}});
}
logger.info(">>>>>>>>>>> xxl-rpc provider config init finish.");
return providerFactory;
}
}
将需要提供服务的类上添加 @XxlRpcService 注解
@XxlRpcService
@Service
public class DemoServiceImpl implements DemoService {}
注意:该类需要spring管理
原因:在 2.2 中 实例化的 XxlRpcSpringProviderFactory 类实现了spring的 ApplicationContextAware 接口 重写了 setApplicationContext() 方法
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// 去spring 的ioc 容器中查找带有XxlRpcService 注解的所有类
Map<String, Object> serviceBeanMap = applicationContext.getBeansWithAnnotation(XxlRpcService.class);
if (serviceBeanMap!=null && serviceBeanMap.size()>0) {
for (Object serviceBean : serviceBeanMap.values()) {
// valid
//判断一下看看实现接口了没有
if (serviceBean.getClass().getInterfaces().length ==0) {
throw new XxlRpcException("xxl-rpc, service(XxlRpcService) must inherit interface.");
}
// add service
XxlRpcService xxlRpcService = serviceBean.getClass().getAnnotation(XxlRpcService.class);
//找到类上有注解 对应接口全类名
String iface = serviceBean.getClass().getInterfaces()[0].getName();
// 找到注解上面的版本信息
String version = xxlRpcService.version();
super.addService(iface, version, serviceBean);
}
}
// TODO,addServices by api + prop
}
引入xxl-rpc-core核心依赖与公共api接口依赖
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-rpc-core</artifactId>
<version>${parent.version}</version>
</dependency>
application文件配置
xxl-rpc.registry.zk.zkaddress=127.0.0.1:2181 // 注册中心zk地址
xxl-rpc.registry.zk.zkdigest=
xxl-rpc.env=test
书写spring配置类
@Configuration
public class XxlRpcInvokerConfig {
private Logger logger = LoggerFactory.getLogger(XxlRpcInvokerConfig.class);
@Value("${xxl-rpc.registry.zk.zkaddress}")
private String zkaddress;
@Value("${xxl-rpc.registry.zk.zkdigest}")
private String zkdigest;
@Value("${xxl-rpc.env}")
private String env;
@Bean
public XxlRpcSpringInvokerFactory xxlJobExecutor() {
XxlRpcSpringInvokerFactory invokerFactory = new XxlRpcSpringInvokerFactory();
if (zkaddress != null) {
invokerFactory.setServiceRegistryClass(ZkServiceRegistry.class);
invokerFactory.setServiceRegistryParam(new HashMap<String, String>(){{
put(Environment.ZK_ADDRESS, zkaddress);
put(Environment.ZK_DIGEST, zkdigest);
put(Environment.ENV, env);
}});
}
logger.info(">>>>>>>>>>> xxl-rpc invoker config init finish.");
return invokerFactory;
}
}
只需要在需要注入的成员上面添加 @XxlRpcReference 注解即可
@Controller
public class IndexController {
@XxlRpcReference
private DemoService demoService;
}