Spring 整合
适用于 JDK 17 或以上版本、Spring 6 / Spring Boot 3 项目。提供者与消费者使用各自的启动类和配置文件。
引入依赖
- Spring Boot 项目
- Spring 项目
<dependency>
<groupId>net.hasor</groupId>
<artifactId>meteor-spring-starter</artifactId>
<version>5.0.0-SNAPSHOT</version>
</dependency>
implementation 'net.hasor:meteor-spring-starter:5.0.0-SNAPSHOT'
已有 Boot 项目保留当前使用的 Spring Boot Starter 版本。
<dependency>
<groupId>net.hasor</groupId>
<artifactId>meteor-spring</artifactId>
<version>5.0.0-SNAPSHOT</version>
</dependency>
implementation 'net.hasor:meteor-spring:5.0.0-SNAPSHOT'
配置方式
普通 Spring 选择以下一种方式;Spring Boot 由 Starter 自动配置。
- 基于 application.properties 配置文件
- 基于注解配置
- 基于 XML 配置
# 服务接口扫描包
meteor.scan-packages=com.example.rpc
meteor.application=spring-echo-provider
meteor.connectors.rsf.host=127.0.0.1
meteor.connectors.rsf.port=28880
import net.hasor.meteor.EnableMeteor;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ComponentScan("com.example.provider")
@EnableMeteor(basePackages = "com.example.rpc")
@PropertySource("classpath:application.properties")
public class MeteorConfig {
}
import net.hasor.meteor.EnableMeteor;
import net.hasor.meteor.bootstrap.MetConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.example.provider")
@EnableMeteor(basePackages = "com.example.rpc")
public class MeteorConfig {
@Bean
public MetConfiguration meteorConfiguration() {
// 配置应用
MetConfiguration configuration = new MetConfiguration();
configuration.setApplicationName("spring-echo-provider");
// 配置协议
MetConfiguration.ConnectorSettings connector = configuration.connector("tcp");
connector.bind("127.0.0.1", 28880);
connector.protocol("rsf");
return configuration;
}
}
# 服务接口扫描包
meteor.scan-packages=com.example.rpc
meteor.application=spring-echo-provider
meteor.connectors.rsf.host=127.0.0.1
meteor.connectors.rsf.port=28880
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.example.provider"/>
<bean class="net.hasor.meteor.ServiceScannerConfigurer">
<property name="basePackage" value="com.example.rpc"/>
<property name="configLocation" value="classpath:application.properties"/>
</bean>
</beans>
在 XML 中显式声明消费接口
可以通过 ServiceBean 将消费接口声明为 Spring Bean。先配置由 Spring 管理启动和关闭的 meteorContext,再声明接口 Bean:
<bean id="echoService" class="net.hasor.meteor.ServiceBean">
<property name="context" ref="meteorContext"/>
<property name="serviceInterface" value="com.example.rpc.EchoService"/>
<property name="address" value="rsf://127.0.0.1:18880"/>
</bean>
其他 Bean 可以按 EchoService 类型注入或通过 ref="echoService" 引用。指定 address 时,代理直接调用该端点。省略此属性时,沿用上下文的默认调用方式,远程地址可由地址本或注册发现提供。接口尚未登记时,ServiceBean 会在该上下文注册消费者。
完整配置见 XML 显式注册示例。
服务定义
定义服务接口,并提供实现类。
import net.hasor.meteor.annotation.MetService;
import org.springframework.stereotype.Service;
@MetService(name = "EchoService", group = "default", version = "1.0.0")
public interface EchoService {
String echo(String text);
}
@Service
public class EchoServiceImpl implements EchoService {
@Override
public String echo(String text) {
return "Hello, " + text;
}
}
服务发布
# 服务接口扫描包
meteor.scan-packages=com.example.rpc
# 配置应用
meteor.application=spring-echo-provider
# 配置协议
meteor.connectors.rsf.host=127.0.0.1
meteor.connectors.rsf.port=28880
@Service 实现类由 Spring 扫描。Spring Boot 的 Starter 自动扫描服务接口并发布服务;普通 Spring 通过 @EnableMeteor 启用。
- Spring Boot 项目
- Spring 项目
import java.util.concurrent.CountDownLatch;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@PropertySource("classpath:meteor-provider.properties")
public class ProviderMain {
public static void main(String[] args) throws InterruptedException {
// 启动 Spring Boot,自动初始化并启动 Meteor
try (ConfigurableApplicationContext app = SpringApplication.run(ProviderMain.class, args)) {
System.out.println("服务已启动");
// 保持提供者运行
CountDownLatch latch = new CountDownLatch(1);
latch.await();
}
}
}
import java.util.concurrent.CountDownLatch;
import net.hasor.meteor.EnableMeteor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ComponentScan
@EnableMeteor(basePackages = "com.example.rpc")
@PropertySource("classpath:meteor-provider.properties")
public class ProviderMain {
public static void main(String[] args) throws InterruptedException {
// 启动 Spring 容器,自动初始化并启动 Meteor
try (AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(ProviderMain.class)) {
app.registerShutdownHook();
System.out.println("服务已启动");
// 保持提供者运行
CountDownLatch latch = new CountDownLatch(1);
latch.await();
}
}
}
服务调用
直连时用 @MetAddress 指定地址;使用地址本或注册中心时,通过 Spring 注入服务接口。
- 直连(代码)
- 直连(地址本)
- 服务发现
用 @MetAddress 指定提供者地址 rsf://127.0.0.1:28880,直接调用注入的服务接口。
@MetAddress 用于服务接口字段,无需叠加其他注入注解。字段不能是 static 或 final,构造方法内尚未完成注入。
# 服务接口扫描包
meteor.scan-packages=com.example.rpc
# 配置应用
meteor.application=spring-echo-consumer
# 配置协议
meteor.connectors.rsf.host=127.0.0.1
meteor.connectors.rsf.port=0
import com.example.rpc.EchoService;
import net.hasor.meteor.annotation.MetAddress;
import org.springframework.stereotype.Component;
@Component
public class EchoClient {
@MetAddress("rsf://127.0.0.1:28880")
private EchoService echo;
public String call() {
return this.echo.echo("Meteor!");
}
}
从 address-book.xml 加载提供者地址,再注入服务接口发起调用。
# 服务接口扫描包
meteor.scan-packages=com.example.rpc
# 配置应用
meteor.application=spring-echo-consumer
# 配置协议
meteor.connectors.rsf.host=127.0.0.1
meteor.connectors.rsf.port=0
# 直连:从地址本读取提供者地址
meteor.address-book=classpath:address-book.xml
<address-book xmlns="https://www.hasor.net/schema/meteor-address-book">
<service group="default" name="EchoService" version="1.0.0">
<address>rsf://127.0.0.1:28880</address>
</service>
</address-book>
import com.example.rpc.EchoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class EchoClient {
@Autowired
private EchoService echo;
public String call() {
return this.echo.echo("Meteor!");
}
}
以 ZooKeeper 为例,添加对应依赖,提供者和消费者连接同一注册中心。
# 服务接口扫描包
meteor.scan-packages=com.example.rpc
# 配置应用
meteor.application=spring-echo-consumer
# 配置协议
meteor.connectors.rsf.host=127.0.0.1
meteor.connectors.rsf.port=0
# 配置注册中心:提供者使用相同配置
meteor.registry.provider=zookeeper
meteor.registry.address=127.0.0.1:2181
# 将 address 替换为实际的 ZooKeeper 集群地址
meteor.registry.root=/meteor-echo
meteor.registry.publish-host=127.0.0.1
import com.example.rpc.EchoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class EchoClient {
@Autowired
private EchoService echo;
public String call() {
return this.echo.echo("Meteor!");
}
}
配置项说明
调用超时
Spring Boot 使用 application.properties;普通 Spring 使用已通过 @PropertySource 或 XML configLocation 加载的属性文件。
meteor.request-timeout=2000
meteor.connect-timeout=1000
| 配置 | 说明 |
|---|---|
meteor.request-timeout | 请求等待上限,示例为 2000 毫秒。 |
meteor.connect-timeout | 建立连接的等待上限,示例为 1000 毫秒。 |
meteor.default-timeout | 请求超时未设为正数时的默认 RPC 超时。 |
若已声明 MetConfiguration Bean,通过它的 setRequestTimeout(2000)、setConnectTimeout(1000) 设置,属性文件不会覆盖该 Bean。单个服务可在接口的 @MetService 上设置 clientTimeout = 1000,或在手工发布、订阅时调用 Builder 的 .timeout(1000)。具体效果见超时和异常。
注册拦截器
将下面的类放在 Spring 扫描的包中,启动时注册日志拦截器:
import net.hasor.meteor.MetContext;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class RpcFilters implements InitializingBean {
@Autowired
private MetContext meteor;
@Override
public void afterPropertiesSet() {
this.meteor.publisher().bindFilter("rpc-log", new RpcLogFilter());
}
}
仅声明 MetFilter Bean 不会自动生效,需要调用 bindFilter。业务方法可通过 MetRequestLocal 读取隐式参数,无需额外注册拦截器。Spring 会创建、启动和关闭 MetContext。
application.properties
| 配置项 | 说明 |
|---|---|
meteor.enabled | 仅用于 Boot 自动配置,默认 true;设为 false 关闭。 |
meteor.scan-packages | 服务接口所在的包。Boot 默认使用启动类所在包及子包;普通 Spring 默认使用 @EnableMeteor 所在配置类的包。 |
扫描设置
| 配置项 | 说明 |
|---|---|
@EnableMeteor.basePackages | 服务接口所在的包,优先于 meteor.scan-packages。 |
ServiceScannerConfigurer.basePackage | XML 中指定扫描包,支持逗号、分号或空白分隔;优先于 meteor.scan-packages。 |
ServiceScannerConfigurer.configLocation | 加载属性文件,已有 Spring 环境配置优先。 |
MetConfiguration Bean | 使用此 Bean,不再根据属性文件创建配置对象。 |