Summary:

Dubbo is an open source distributed service framework of Alibaba. Its most prominent feature is that it is structured in a hierarchical manner, which can be decoupled (or loosened as much as possible) between layers. From the perspective of the service model, Dubbo adopts a very simple model, in which either the Provider provides the service or the Consumer consumes the service, so the roles of Provider and Consumer can be abstracted based on this.

                

Let’s take a look at Dubbo’s RPC call process, which mainly involves four modules:

  • We usually take Zookeeper as our Registry
  • Provider: a service Provider (producer) that provides a concrete service implementation
  • Consumer: a Consumer who subscribes to services from a registry
  • Monitor: Monitors the number and time of RPC calls

From the figure above, we can know that the whole process of RPC service invocation is as follows:

  • Producers publish services to service registries
  • A consumer subscribes to a service in a service registry
  • The consumer invokes the registered service

I. Project construction

The development environment mainly involves the following aspects:

  • Spring-boot
  • JDK 8
  • Dubbo
  • Zookeeper

Project construction: http://start.spring.io/ To quickly build a Web project, you can refer to specific operations

www.cnblogs.com/jaycekon/p/…

Because Dubbo needs to use a service registry, we will use Zookeeper as the service registry. You can refer to the installation and configuration details

www.cnblogs.com/jaycekon/p/…

After the base environment is determined, the directory structure for our project is as follows:

                       

As shown in the figure above, our project is mainly divided into two modules, one is producer: Spring-boot-Dubbo, the other is Spring-boot-Consumer.

The structure of the whole project is very simple, which is in line with the characteristics of Spring-Boot. It is simple and convenient. Let’s start to analyze the structure of the whole project step by step

Second, the Productor

2.1, Pom. XML

The project dependencies section mainly uses the basic Spring-boot-Web dependencies, then we need to introduce additional Dubbo and Zookeeper dependencies (see source code for detailed dependencies, Github address at the bottom of the blog post) :

<dependency> <groupId>io.dubbo.springboot</groupId> <artifactId>spring-boot-starter-dubbo</artifactId> < version > 1.0.0 < / version > < / dependency > < the dependency > < groupId > org. Apache. Zookeeper < / groupId > <artifactId> Zookeeper </artifactId> <version>3.4.6</version> <exclusions> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId>
                      <artifactId>log4j</artifactId>
                  </exclusion>
              </exclusions>
          </dependency> Copy the code

2.2. Configuration files

Since we are using Spring-Boot for our underlying development, we should be able to take advantage of the advantages of spring-boot, so we can configure the Dubbo service directly in the application.properties file:

# Dubbo service provider configurationSpring. Dubbo. Application. The name = the provider - service name spring. The dubbo. Registry. Address = zookeeper: / / 127.0.0.1:2181 - registry address Spring. Dubbo. Protocol. The name = dubbo -- spring dubbo agreement. Dubbo. Protocol. The port = 20880 Spring. Dubbo. Scan = com. Jaycekon. Dubbo. Service - a statement need to expose the service interfaceCopy the code

If you do not use spring-Boot for automatic configuration, you can refer to the following configuration XML configuration:

  1. <? xml version="1.0" encoding="UTF-8"? > <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
             xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <! <dubbo:application name="provider"/ > <! --> <dubbo: Registry protocol="zookeeper" address="127.0.0.1"  check="false"/ > <! --> <dubbo:protocol name="dubbo" port="1" dispather="all" check="false" />
          <dubbo:provider timeout="10000"  threads="10" threadpool="fixed"   loadbalance="roundrobin"/ > <! <dubbo:service interface="com.jaycekon.dubbo.service" ref="userService"/> </beans> Copy the code

2.3. Service provision

There are two main parts in service provision, one is exposure service, the other is service realization

Exposed service: the interface we use in normal development, here we create a UserService interface, mainly including a method to save the user.

import com.jaycekon.dubbo.domain.User;
  /**
   * Created by Jaycekon on 2017/9/19.
   */  public interface UserService {
      User saveUser(User user);
  } Copy the code

Service implementation: The Service implementation, like our normal services, implements the interface, but in particular, we need to use the @service annotation of Dubbo. More springboot actual combat content, Java bosom friend public account reply “Springboot aggregation”

import com.alibaba.dubbo.config.annotation.Service;  
import com.jaycekon.dubbo.domain.User;  
import com.jaycekon.dubbo.service.UserService;  
/**  
 * Created by Jaycekon on 2017/9/19.  
 */  
@Service  
public class UserServiceImpl implements UserService {  
    @Override  
    public User saveUser(User user) {  
        user.setId(1);  
        System.out.println(user.toString());  
        returnuser; }}Copy the code


2.4. Overall structure

Dubbo’s service provider has been developed in general. It is very simple. The overall directory structure is as follows:

                             

Third, Consumer

3.1, pom. XML

The relative dependencies of consumers are consistent with those of producers.

<? xml version="1.0" encoding="UTF-8"? > <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Jaycekon < / groupId > < artifactId > spring - the boot - consumer < / artifactId > <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-consumer</name> <description>Demo projectforSpring Boot</description> <parent> <groupId>org.springframework.boot</groupId> The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 1.5.7. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> < project. Reporting. OutputEncoding > utf-8 < / project. Reporting. OutputEncoding > < Java version > 1.8 < / Java version > </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <! --> <dependency> <groupId> io.dubo. springboot</groupId> < artifactId > spring - the boot - starter - dubbo < / artifactId > < version > 1.0.0 < / version > < / dependency > < the dependency > < the groupId > org. Apache. Zookeeper < / groupId > < artifactId > zookeeper < / artifactId > < version > 3.4.6 < / version > < exclusions > <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId>  
                    <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <! -- MVN spring-boot:run --> <dependency> <groupId>org.springframework</groupId> < artifactId > springloaded < / artifactId > < version > 1.2.3. RELEASE < / version > < / dependency > < the dependency > < the groupId > org. Projectlombok < / groupId > < artifactId > lombok < / artifactId > < version > 1.16.18 < / version > < scope > provided < / scope > </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

3.2. Configuration files

Config files differ slightly from producers:

Avoid conflicts with server project ports
server.port=8081 
## Dubbo service consumer configurationSpring. Dubbo. Application. The name = consumer spring. The dubbo. Registry. Address = zookeeper: / / 127.0.0.1:2181 spring.dubbo.scan=com.jaycekon.dubbo.serviceCopy the code

Xml-based configuration:

<? xml version="1.0" encoding="UTF-8"? > <beans xmlns="http://www.springframework.org/schema/beans"  
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <! <dubbo:application name="provider"/ > <! --> <dubbo: Registry protocol="zookeeper" address="${dubbo.registry.address}"  check="false"/ > <! --> <dubbo:protocol name="dubbo" port="1" dispather="all" check="false" />  
    <dubbo:provider timeout="10000"  threads="10" threadpool="fixed"   loadbalance="roundrobin"/ > <! <dubbo:service interface="com.jaycekon.dubbo.service" ref="userService"/>  
</beans> Copy the code

3.3. Service implementation

Here, if we need to invoke the relevant service in the registration service, we need to implement the relevant interface.

import com.jaycekon.dubbo.domain.User;  
/**  
 * Created by Jaycekon on 2017/9/19.  
 */  
public interface UserService {  
    User saveUser(User user);  
} Copy the code

For example, here we need to use the saveUser (User User) method in the producer, so we need to create an interface and then invoke it using the @reference annotation:

import com.alibaba.dubbo.config.annotation.Reference; import com.jaycekon.dubbo.domain.City; import com.jaycekon.dubbo.domain.User; import org.springframework.stereotype.Component; /** * CityDubboConsumerService * <p> * Created by Jaycekon on 20/09/2017. @Reference CityDubboService cityDubboService; @Reference UserService userService; public voidprintCity() {  
        String cityName = "Guangzhou";  
        City city = cityDubboService.findCityByName(cityName);  
        System.out.println(city.toString());  
    }  
    public User saveUser() {  
        User user = new User();  
        user.setUsername("jaycekon")  
                .setPassword("jaycekong824");  
        returnuserService.saveUser(user); }}Copy the code

3.4. Service Invocation

Finally, we need to implement a RESTful interface that provides users with calls to:

import com.jaycekon.dubbo.service.CityDubboConsumerService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RestController;  
/**  
 * Created by Jaycekon on 2017/9/19.  
 */  
@RestController  
public class UserController {  
    @Autowired  
    private CityDubboConsumerService service;  
    @RequestMapping("/save")  
    public Object saveUser() { 
        returnservice.saveUser(); }}Copy the code

3.5. Directory Structure

Four,

This blog is mainly summed up by the blogger while learning about Spring-boot. After a series of comparisons, Dubbo is still a bit behind Spring-Cloud, both in terms of service delivery and community activity. However, since Dubbo is used internally, we still need to learn about it. Later, we will learn and itemize spring-Cloud.


Author: Anonymous

Source: Java Friend

Reprint links: developer.51cto.com/art/202005/…