This project is still in development
Setup.
use cli to create a project.
npm i bpframework-cli -g
create a project.
bpframework init
Example.
see directory ./examples
Feature.
feature | supports |
---|
config | bootstrap.yml SpringCloudConfig |
discovery | nacos |
scheduling | @Scheduled |
api routers | @RestController |
Configure.
The appropriate configuration is required to enable the corresponding feature: config
@FindMicroserviceConfigure
By default, nacos is used to find micro-service; You can customize it by @FindMicroserviceConfigure
.
@Service()
class Configure {
@FindMicroserviceConfigure
async onFindMicroservice(serviceName: string, excludeHost: string): Promise<ServiceInfo> {
return {
ip,
port,
serviceName,
metadata,
}
}
}
@RestControllerConfigure
定义RestController的默认headers等信息, 使用如下方式.
@Service()
class Configure {
@RestControllerConfigure
onConfigure(): bp.RestControllerConfigureInfo {
return {
defaultHeaders: {'content-type': 'application/json;charset=utf-8'},
}
}
}
@FeignClientConfigure
定义FeignClient的默认headers等信息, 使用如下方式.
@Service()
class Configure {
@FeignClientConfigure
onConfigure(): bp.FeignClientConfigureInfo {
return {
defaultHeaders: {'content-type': 'application/json;charset=utf-8'},
filterResponseCallback: (data: FeignClientFilterResponseData) => {
},
filterRequestCallback: (data: FeignClientFilterRequestData, feignData: FeignDataType) => {
}
}
}
}
@Value
使用 @Value 注解设置初始值或获取配置值.
@Service()
class Demo {
@Value("Miss A")
teacher1Name: string;
@Value("${teacherName2}")
teacher2Name: string;
@Value("${teacherName3:defaultName}")
teacher3Name: string;
}
Middleware.
see https://github.com/bpcloud/middleware.git
Event Listener.
@ContextRefreshedEventListener
本地配置加载完成, 系统service对象初始化完成.
@Service()
class ApplicationEvent {
@ContextRefreshedEventListener
async onContextRefreshed(ev:ContextRefreshedEvent):void {
}
}
@RefreshRemoteEventListener
远程配置动态刷新事件.
@Service()
class ApplicationEvent {
@RefreshRemoteEventListener
async onRefreshRemote(ev:RefreshRemoteEvent):void {
}
}
@InstanceRegisteredEventListener
实例注册到注册中心后的事件.
@Service()
class ApplicationEvent {
@InstanceRegisteredEventListener
async onInstanceRegistered(ev:InstanceRegisteredEvent):void {
}
}
Scheduling
@Scheduled
使用此注解可以开启一个定时任务.
@Service()
class Demo {
@Scheduled({cron:'* * * * * *'})
async onTick(): Promise<false|void> {
return false;
}
}
- Start task: 当类实例被创建后, task即按照时间间隔运行
- Stop task: 当@Scheduled修饰的方法明确返回false时, task将停止
Bean
@Service
可以使用此注解实例化对象
export function finishBeans(): Promise<void>;
export function getServiceInstances(key: any): ServiceInstanceType;
export function ImmediatelyService(name: string): ClassDecorator;
export function ImmediatelyService(cfg?: { singleton?: boolean, name?: string }): ClassDecorator;
export function Service(name: string): ClassDecorator;
export function Service(cfg?: { singleton?: boolean, name?: string }): ClassDecorator;
示例:
@Service()
class Example {
constructor() {}
}
@ImmediatelyService()
class Example {
constructor() {}
}
@Bean
export function Bean(name: string): MethodDecorator;
export function Bean(cfg?: { singleton?: boolean, name?: string }): MethodDecorator;
@Autowired
export function Autowired(type: Function|string): PropertyDecorator;
@Value
export function Value(value: any): PropertyDecorator;