注解 服务发布/服务引用

除了常规的 xml 方式发布服务外,我们也支持在SOFABoot 环境下,注解方式的发布与引用,同 xml 类似,我们有
@SofaService@SofaReference,同时对于多协议,存在@SofaServiceBinding@SofaReferenceBinding 注解

服务发布

如果要发布一个 RPC 服务. 我们只需要在 Bean 上面打上@SofaService注解.指定接口和协议类型即可

  1. @SofaService(interfaceType = AnnotationService.class, bindings = { @SofaServiceBinding(bindingType = "bolt") })
  2. @Component
  3. public class AnnotationServiceImpl implements AnnotationService {
  4. @Override
  5. public String sayAnnotation(String stirng) {
  6. return stirng;
  7. }
  8. }

服务引用

对于需要引用远程服务的 bean, 只需要在属性,或者方法上,打上Reference 的注解即可.暂时只支持 bolt 协议

  1. @Component
  2. public class AnnotationClientImpl {
  3. @SofaReference(interfaceType = AnnotationService.class, binding = @SofaReferenceBinding(bindingType = "bolt"))
  4. private AnnotationService annotationService;
  5. public String sayClientAnnotation(String str) {
  6. String result = annotationService.sayAnnotation(str);
  7. return result;
  8. }
  9. }

使用演示

可以在sample 工程目录的annotation 子项目中进行验证测试.