Skip to main content

dawdler-remote-service-core

Module introduction​

The RemoteService core module, used to mark an injected service as a remote service.

1. Add the dependency to the pom​

<groupId>club.dawdler</groupId>
<artifactId>dawdler-remote-service-core</artifactId>

2. About @RemoteService​

@RemoteService marks an injected service as a remote service.

@Retention(value = RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD })
public @interface RemoteService {
//The class name of the service; defaults to empty, in which case the fully qualified name of the class or interface where the annotation is placed is used (class first). Corresponds to serviceName in @Service
String serviceName() default "";
}

3. About @RemoteServiceAssistant​

@RemoteServiceAssistant applies to methods of a service interface. It configures method-specific settings for asynchronous execution, timeout, fuzzy matching, and load-balancing strategy.

It is generally not needed, so configure it only when there are special requirements.

@Retention(value = RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface RemoteServiceAssistant {
boolean async() default false;// Effective on the client side; whether to execute asynchronously.

int timeout() default 120;// Effective on the caller side; the timeout for calling the remote service, in seconds, 120 seconds by default.

boolean fuzzy() default true;// Effective on the caller side; whether to match the method fuzzily, true by default; fuzzy matching matches by method name and parameter count, while non-fuzzy matching matches exactly by method name and parameter types. Fuzzy matching is more efficient; if a service implementation class contains methods with the same name and the same parameter count, set this parameter to false for exact matching.

String loadBalance() default "roundRobin";// Effective on the caller side; load-balancing strategy.
}

4. Usage example​

4.1 Create a Controller​

@Controller
@RequestMapping("/order")
public class OrderController {

@RemoteService
private OrderService orderService;

@ResponseBody
@RequestMapping(value = "/list", method = RequestMethod.GET)
public PageResult<List<Order>> list(Integer pageOn, Order order) {
int row = 10;
return orderService.selectPageList(order, pageOn, row);
}

}

4.2 Create the service interface​

@Service("order-service")
public interface OrderService {
@RemoteServiceAssistant(timeout = 30) //Set the timeout to 30 seconds
BaseResult<Order> selectByPrimaryKey(Integer orderId);

}

4.3 Write the service implementation layer (using MyBatis as an example)​

public class OrderServiceImpl implements OrderService {
@Repository
private OrderMapper orderMapper;

@Override
@DBTransaction(mode = MODE.readOnly)
public BaseResult<Order> selectByPrimaryKey(Integer orderId) {
Order order = orderMapper.selectByPrimaryKey(orderId);
BaseResult<Order> baseResult = new BaseResult<>(order);
return baseResult;
}
}