跳到主要内容

Spring 整合

适用于 JDK 17 或以上版本、Spring 6 / Spring Boot 3 项目。提供者与消费者使用各自的启动类和配置文件。

引入依赖​

pom.xml 中的 dependencies
<dependency>
<groupId>net.hasor</groupId>
<artifactId>meteor-spring-starter</artifactId>
<version>5.0.0-SNAPSHOT</version>
</dependency>
build.gradle 中的 dependencies
implementation 'net.hasor:meteor-spring-starter:5.0.0-SNAPSHOT'

已有 Boot 项目保留当前使用的 Spring Boot Starter 版本。

配置方式​

普通 Spring 选择以下一种方式;Spring Boot 由 Starter 自动配置。

application.properties
# 服务接口扫描包
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 {
}

在 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-provider.properties
# 服务接口扫描包
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 启用。

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();
}
}
}

服务调用​

直连时用 @MetAddress 指定地址;使用地址本或注册中心时,通过 Spring 注入服务接口。

用 @MetAddress 指定提供者地址 rsf://127.0.0.1:28880,直接调用注入的服务接口。

@MetAddress 用于服务接口字段,无需叠加其他注入注解。字段不能是 static 或 final,构造方法内尚未完成注入。

meteor-consumer.properties
# 服务接口扫描包
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!");
}
}

更多用法见发布服务、订阅服务和服务发现。

配置项说明​

调用超时​

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.basePackageXML 中指定扫描包,支持逗号、分号或空白分隔;优先于 meteor.scan-packages。
ServiceScannerConfigurer.configLocation加载属性文件,已有 Spring 环境配置优先。
MetConfiguration Bean使用此 Bean,不再根据属性文件创建配置对象。