preface

Hessian is a lightweight RPC invocation framework for binary RPC protocol. Compared to Dubbo, Spring Cloud is easy and concise to use.

Service publishing and service consumption in the form of Hessian annotation based on Spring IOC. Learn more about the principle of Spring dependency injection from the implementation process.

Hessian use

HessianServiceProxyExporter

public class HessianServiceProxyExporter extends HessianServiceExporter { @Override public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// Here you can do the unified data check log.info("HessianServiceProxyExporter get request at {}", LocalDateTime.now()); super.handleRequest(request ,response); }}Copy the code

The Hessian service is exposed

@Bean(name = "/userService")
public HessianServiceExporter initHessian(){
    HessianServiceExporter exporter = new HessianServiceProxyExporter();
    exporter.setService(new UserServiceImpl());
    exporter.setServiceInterface(IUserService.class);
    return exporter;
}
Copy the code

Hessian service consumption

@Bean
public HessianReferenceProxyFactoryBean helloClient() {
    HessianReferenceProxyFactoryBean factory = new HessianReferenceProxyFactoryBean();
    factory.setServiceUrl("http://127.0.0.1:8080/userService");
    factory.setServiceInterface(IUserService.class);
    return factory;
}
Copy the code

The Hessian Service is initialized to the Spring IOC container and used as a normal Service.

@Autowired
private IUserService userService;
Copy the code

From the above Hessian service development process, we can see that each Hessian service development has a one-to-one service exposure, service reference. When the system is relatively large, it will increase the difficulty of service management and a lot of duplicated code

Hessian annotation implementation

As Spring Boot became popular, more and more people became accustomed to the annotated development model. Hessian can also implement the development and use of annotation patterns.

@HessianService

Let’s first analyze the Hessian service exposure process:

  • Instantiate a Service Bean
  • Instantiate a HessianServiceExporter Bean
  • Register the HessianServiceprovider into the IOC container for unified management so that we can define a composite annotation@HessianService
Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface HessianService { /** * Hessian service name * @return
     */
    String name() default "";
}
Copy the code

The instance of the Service Bean is handled by Spring IOC

By implementing InitializingBean, ApplicationContextAware’s corresponding interface handles the @HessianService service exposure

@HessianReference

Consumer consumption services, draw lessons from the @autowired implementation approach: inheritance InstantiationAwareBeanPostProcessorAdapter overloading postProcessPropertyValues method

  • InstantiationAwareBeanPostProcessorAdapterCallback after the Bean is instantiated, but before the property is displayed by setting the value.
  • postProcessPropertyValuesImplementing dependency Injection

This implements Hessian’s annotation pattern

@HessianService
public class StudentServiceImpl  implements IStudentService{
}
Copy the code
@HessianReference
private IUserService userService;
Copy the code

summary

In the above implementation, it is clear that all Bean operations in Spring Framework based development are conducted around the IOC container. Spring Framework provides a variety of interfaces for extension, through the implementation of the methods in the interface can realize personalized business (such as @HessianReference dependency injection in the above)

The focus of this article is not to implement the Hessian annotation pattern, but to learn about Spring’s good design layering (open to extension) through the implementation of the Hessian annotation pattern: to make our code highly extensible by layering the design

The source code is here