preface

My project AngBoot uses SpringSecurity for permission management and authentication. However, the project was originally designed as a development template structure. Therefore, in order to cope with microservices and more flexible usage scenarios, I introduced Dubbo to provide remote authentication services. This makes it easy to migrate from my embedded authentication system to any developer’s own authentication system by modifying the configuration.

The project itself is a SpringBoot project. If you need Dubbo, you can easily import Dubbo into the SpringBoot environment by using @enableDubbo and annotation configuration. However, My requirement is to configure, flexibly, and dynamically introduce the Dubbo RPC remote service. Simply speaking, the built-in module is used by default, but the remote service can still be configured. So adding @enableDubbo to SpringBoot is not a good way to use Dubbo directly because there is no remote call by default

The Dubbo API is a bit rough, but you can put ApplicationConfig, RegistryConfig, and other objects into IOC. The remote object is then taken and added to the IOC via ReferenceConfig, so that other modules and code are not affected. So let’s write an exmaple to verify that.

Create a service provider provider-ticket

The service provider doesn’t care about that configuration, so we’ll use the fastest and simplest SpringBoot + @enabledubbo annotation.

  • SpringBoot entrance class
@SpringBootApplication @EnableDubbo public class ProviderTicketApplication { public static void main(String[] args) { SpringApplication.run(ProviderTicketApplication.class, args); }}Copy the code

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

public interface TicketService {
    public String getTicket();
}
Copy the code

  • The service implementation
package com.jack.ticket.service; import com.alibaba.dubbo.config.annotation.Service; import org.springframework.stereotype.Component; @component // Add ioc@service // Publish Service public class TicketServiceImpl implements TicketService {@Override public String getTicket() {return "good, my country...." ; }}Copy the code

  • Dubbo configuration
Dubbo. Application. Name = the provider - ticket # registry address dubbo. Registry. Address = zookeeper: / / 127.0.0.1:2181 # package scan need to provide services dubbo.scan.base-packages=com.jack.ticket.serviceCopy the code

  • Start Zookeeper and start the service providerProviderTicketApplication

The API configuration creates the service consumer consumer-user-API

  • SpringBoot entry class. Note that there is no EnableDubbo annotation
@SpringBootApplication public class ConsumerUserApplication { public static void main(String[] args) { SpringApplication.run(ConsumerUserApplication.class, args); }}Copy the code

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

public interface TicketService {

    String getTicket();
}Copy the code

  • Create a Configuration class to get the remote service
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; @configuration public class DubboConfiguration {/** * Create the ApplicationConfig object. * Therefore, adding an instance to IOC becomes necessary when remote services require multiple fetches. * Of course, */ @bean public ApplicationConfig ApplicationConfig () {ApplicationConfig application = new ApplicationConfig(); application.setName("consumer-user"); return application; } /** * Create RegistryConfig to configure the registry information. * @see DubboConfiguration#applicationConfig() Likewise joining IOC is not required */ @bean public RegistryConfig registryConfig() { RegistryConfig registry = new RegistryConfig(); Registry. SetAddress (" zookeeper: / / 127.0.0.1:2181 "); return registry; In fact, if the remote object is called only once, there is no need to join IOC, just get it once when you use it, * however, most scenarios require multiple calls and fetching the remote object is expensive, so, */ @bean public TicketService TicketService () {// This instance is very large, encapsulates the connection to the registry and the connection to the provider, please cache it yourself. ReferenceConfig<TicketService> reference = new ReferenceConfig<>(); reference.setApplication(applicationConfig()); // Multiple registries can use setRegistries() reference.setregistry (registryConfig()); reference.setInterface(TicketService.class); TicketService TicketService = reference.get(); TicketService = reference.get(); return ticketService; }}Copy the code

  • Create UserService using the remote service object

Since the remote service object has been acquired and added to the current IOC, dependency injection can be done via @AutoWired.

package com.jack.user.service; import com.jack.ticket.service.TicketService; import org.springframework.stereotype.Service; @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(" ticket: "+ ticket); }}Copy the code

  • Run the 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; @RunWith(SpringRunner.class) @SpringBootTest public class UserServiceTests { @Autowired private UserService userService;  @Test public void testBuyTicket() { Assert.assertNotNull("init user service failed..." , userService); userService.buyTicket(); }}Copy the code

conclusion

At this point, Dubbo API configuration is introduced. We can easily use it in the Spring/SpringBoot environment and flexibly extend programming with Spring Conditional so that our own projects/products can cope with more application scenarios.

Check out my project AngBoot for more

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