Dubbo API configuration mode


My project AngBoot Spring security is used for permission management and authentication, but the original intention of the project is to serve as a development template structure. Therefore, in order to cope with micro services and more flexible use scenarios, I introduced Dubbo to provide remote authentication services. In this way, it is easy to migrate from my embedded authentication system to any developer's own authentication system by modifying the configuration

Today, when introducing Dubbo into my project, I encountered a problem. The project itself is a spring boot project. If you need Dubbo, you can use @ EnableDubbo and annotation configuration to easily introduce Dubbo into the spring boot environment. However, my requirement is to introduce Dubbo rpc remote service in a configurable, flexible and dynamic way, that is, to use embedded modules by default, But you can still use remote services through configuration, so it's not appropriate to add @ EnableDubbo directly for SpringBoot, because there is no remote call by default

See Dubbo document We can find the API configuration mode of Dubbo, but the official website is relatively rough, but the intuitive guess is that we can put ApplicationConfig, RegistryConfig and other objects into IOC, and then take the remote objects into IOC through ReferenceConfig, so it will not affect other modules and codes. So write an exmaple for verification

Create service provider provider ticket

The service provider doesn't care about that kind of configuration. We use the fastest and simplest spring boot @ enabledubbo annotation to configure

  • SpringBoot entry class
public class ProviderTicketApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderTicketApplication.class, args);


  • Service interface
package com.jack.ticket.service;

public interface TicketService {
    public String getTicket();

  • Service realization
package com.jack.ticket.service;

import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

@Component // Add services to IOC
@Service // Publish the service
public class TicketServiceImpl implements TicketService {
    public String getTicket() {
        return "Amazing., My country....";

  • Dubbo configuration

# Address of Registration Center

# Services required for package scanning

  • Open Zookeeper and start the service provider ProviderTicketApplication

API configuration create service consumer user API

  • SpringBoot entry class. Note that there is no EnableDubbo annotation
public class ConsumerUserApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerUserApplication.class, args);


  • Remote service interface
package com.jack.ticket.service;

public interface TicketService {

    String getTicket();

  • Create a Configuration class to get remote services
package com.jack.user.config;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.jack.ticket.service.TicketService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class DubboConfiguration {

    * Create the ApplicationConfig object
    * Adding it to IOC is not necessary, because this instance is only needed to get the remote object,
    * Therefore, it is necessary to add this instance to IOC when remote services need to be acquired many times,
    * Of course, you can reuse it as a member property
   public ApplicationConfig applicationConfig() {
      ApplicationConfig application = new ApplicationConfig();

      return application;

    * Create RegistryConfig to configure registry information
    * @see DubboConfiguration#applicationConfig() It is not necessary to join IOC
   public RegistryConfig registryConfig() {
      RegistryConfig registry = new RegistryConfig();

      return registry;

    * Get remote service object
    * In fact, if the remote object is called only once, there is no need to add IOC. It is better to get it once when using,
    * However, most scenarios need to be repeatedly called, and it is expensive to obtain remote service objects,
    * It is obviously a good practice to add it to IOC container for reuse
   public TicketService ticketService() {
      // This instance is very heavy. It encapsulates the connection with the registry and the connection with the provider. Please cache it by yourself, otherwise it may cause memory and connection leakage
      ReferenceConfig<TicketService> reference = new ReferenceConfig<>();
      // Multiple registries can use setRegistries()

      // Use TicketService as a local bean
      // Note: this proxy object encapsulates all communication details internally. The object is heavy. Please cache and reuse
      TicketService ticketService = reference.get();

      return ticketService;

  • Create UserService to use remote service object

Since the remote service object has been acquired and added to the current IOC, dependency injection can be performed through @ Autowired

package com.jack.user.service;

import com.jack.ticket.service.TicketService;
import org.springframework.stereotype.Service;

public class UserService {

    public final TicketService ticketService;

    public UserService(TicketService ticketService) {
        this.ticketService = ticketService;

    public void buyTicket() {
        String ticket = ticketService.getTicket();

        System.out.println("Tickets are available., ticket: "   ticket);

  • Operation test
package com.jack.user.service;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

public class UserServiceTests {

    private UserService userService;

    public void testBuyTicket() {
        Assert.assertNotNull("init user service failed...", userService);



At this point, the API configuration mode of Dubbo is introduced. We can use it conveniently in the Spring/SpringBoot environment and flexibly carry out extended programming with Spring Conditional so that our project / product can cope with more usage scenarios

Please pay more attention to my project AngBoot

Daily praise: welcome to comment, comment, follow and forward. It would be better if you could give some appreciation, hahaha https://blog.csdn.net/DreamLi1314

Published 21 original articles, won praise 35, visited 60000+
Private letter follow

Tags: Dubbo Spring SpringBoot Zookeeper

Posted on Sun, 02 Feb 2020 00:45:41 -0800 by zeezack