You can also publish and reference services by using the Annotation mode in SOFABoot.Similar to the XML mode, we provide @SofaService
and @SofaReference
. When multiple protocols are used, you can use @SofaServiceBinding
and @SofaReferenceBinding
annotations.
Publish a service
To publish an RPC service, add the @SofaService
annotation to a Bean object to specify the interface and protocol types.
@SofaService(interfaceType = AnnotationService.class, bindings = { @SofaServiceBinding(bindingType = "bolt") })
@Component
public class AnnotationServiceImpl implements AnnotationService {
@Override
public String sayAnnotation(String stirng) {
return stirng;
}
}
Reference a service
If the Bean object needs to reference a remote service, you just need to add the Reference annotation to the attribute or method. Bolt, Dubbo, and REST protocols are supported.
@Component
public class AnnotationClientImpl {
@SofaReference(interfaceType = AnnotationService.class, binding = @SofaReferenceBinding(bindingType = "bolt"))
private AnnotationService annotationService;
public String sayClientAnnotation(String str) {
String result = annotationService.sayAnnotation(str);
return result;
}
}