I. Introduction to basic components

1. Dubbo framework

Dubbo, the core framework of service-oriented governance, was widely used in China a few years ago. Subsequently, due to the rise of micro-service architecture, more companies turned to the mature technology stack under micro-service, but Dubbo itself is indeed an excellent framework.

The common application iteration and upgrade processes are as follows:

  • As the number of applications increases, the acceleration caused by a single application increases less and less. One way to improve efficiency is to break the application into several unrelated applications to improve efficiency. At this point, a Web framework (MVC) for accelerating front-end page development is key.
  • With the increasing number of vertical applications, the interaction between applications is inevitable. Core businesses are extracted as independent services, gradually forming a stable service center, so that front-end applications can respond to changing market demands more quickly. At this point, the distributed Services framework (RPC) for business reuse and integration is key.
  • With service development, more and more services are provided. In this case, a scheduling center needs to be added to manage cluster capacity in real time based on access pressure to improve cluster utilization. At this point, a resource scheduling and Governance center (SOA) for improving machine utilization is key.

The core capabilities of the Dubbo framework are interface oriented remote method invocation, intelligent fault tolerance and load balancing, and automatic service registration and discovery. Just can solve the above business development pain points.

2. Micro-service framework

SpringCloud is an ordered collection of frameworks. It makes use of SpringBoot’s development convenience to subtly simplify the development of distributed system infrastructure, such as service discovery registration, configuration center, message bus, load balancing, circuit breakers, data monitoring, etc., can be started and deployed with one click with SpringBoot’s development style.

The following AliCloud microservices components have been used, among which the Nacos registry, the most basic component, directly supports the Dubbo framework, so that Cloud and Dubbo two frameworks are successfully integrated together.

3. Nacos Registry

The Nacos registry is primarily used to discover, configure, and manage microservices. It also provides an easy-to-use feature set to quickly implement dynamic service discovery, service configuration, service metadata, and traffic management.

As shown above, Nacos seamlessly supports some of the major open source eco-frameworks, such as SprinCloud and Dubbo. AliCloud includes Seata, RocketMQ, Sentinel and other components.

Ii. Service structure Diagram

The structure of the SpringCloud and Dubbo integration is illustrated below, using the Nacos center:

Provider: Provides core Dubbo service interfaces.

Consumer: Dubbo service interface for Consumer registration;

Nacos registry: Configure, discover, and manage Dubbo services;

As you can see from the above process, the core Dubbo framework and the microservices native framework are very similar both in terms of architecture and usage process, and the above process also follows the following rule: Dubbo-server connects to its service library DB and provides services through interfaces on the Dubbo-facade. If different Dubbo-servers need to access other service interfaces, they must also use interfaces on the facade of other services. Dubbo-client serves as the interface service consumer. The services of many business modules can be accessed through facade interfaces, and the overall architectural hierarchy is clear.

Three, coding case implementation

1. Case structure and dependency

Case structure

It contains three modules: Server, facade, and Client.

The core depends on

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-dubbo</artifactId>
    <version>2.1.1. RELEASE</version>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <version>2.1.1. RELEASE</version>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    <version>2.1.1. RELEASE</version>
</dependency>
Copy the code

2. Configure the server

The configuration file

Nacos registry and Dubbo are the two core configurations.

server: port: 9010 spring: application: name: node10-dubbo-server cloud: nacos: discovery: server-addr: http://localhost:8848 config: server-addr: http://localhost:8848 file-extension: yaml # Dubbo service configuration Dubbo: scan: base-packages: com.cloud.dubbo.service protocol: name: dubbo port: -1 registry: address: spring-cloud://localhostCopy the code

Service interface implementation

The DubboService is the interface provided in the Dubbo-facade package.

import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
public class DubboServiceImpl implements DubboService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DubboServiceImpl.class) ;

    @Override
    public String getInfo(a) {
        LOGGER.info("node10-dubbo-server start ...");
        return "node10-dubbo-server"; }}Copy the code

Note: @service is a Dubbo framework annotation, not a Spring framework annotation.

3. Configuration on the consumer side

The configuration file

The main configuration is to link to the Nacos registry and subscribe to the registry’s Node10-Dubbo-Server service.

server: port: 9011 spring: application: name: node10-dubbo-client cloud: nacos: discovery: server-addr: http://localhost:8848 config: server-addr: http://localhost:8848 # Dubbo service config: protocol: name: Dubbo port: -1 registry: address: spring-cloud://localhost cloud: subscribed-services: node10-dubbo-serverCopy the code

Dubbo interface call

Similarly, the DubboService is the external interface provided in the Dubbo-facade package.

import com.cloud.dubbo.service.DubboService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DubboWeb {

    @Reference
    private DubboService dubboService ;

    @GetMapping("/getInfo")
    public String getInfo (a) {
        returndubboService.getInfo() ; }}Copy the code

Note: @Reference is also an annotation in the Dubbo framework.

After the above process is developed, start dubo-server and Dubo-client respectively to view the registry service list:

You can see the full effect of the case by testing the getInfo interface request above.

Four, technology selection

Few people choose the architecture mode of SpringCloud+Dubbo framework. Here is a brief explanation of why, because these two frameworks are quite complex, learning cost is one aspect, and risk is the main reason. Using these two frameworks at the same time means facing and solving the problems arising under the two frameworks. There is no need for bells and whistles when either framework can reliably solve a business problem.

Five, source code address

Making address: given a smile GitEE address: https://github.com/cicadasmile/spring-cloud-base Given a smile at https://gitee.com/cicadasmile/spring-cloud-baseCopy the code