By: no dishes studio – Marklux

Source: Dubbo Primer (2) – Simple Practice

The copyright belongs to the author, please indicate the source

Once we understand what a distributed framework is, we need to get hands-on with it to understand how the whole system works.

This paper tries to use Dubbo to build a simple distributed service and use DubboKeeper to monitor the service.

From: Talk about Dubbo and the simplest Dubbo tutorial

The preparatory work

Before you can officially use Dubbo, you need two things:

1. Service Registry (ZooKeeper)

Dubbo consumers and service providers need to register in a unified registry to use dubbo, which can be a ZooKeeper, but you can also use other registries and integrate them.

The installation process of ZooKeeper is simple, just refer to this document (it is only the simplest stand-alone version, not complicated).

2. Service Monitoring Center (DubboKeeper)

To monitor the performance of the entire distributed service, it is best to have a visual management and monitoring application, such as DubboKeeper, developed by the DubboClub team, or dubbo’s official DubboAdmin project.

Since the deployment of DubboKeeper is somewhat complex, I will briefly describe it here. Note that monitoring software is not necessary, but it is recommended to deploy one in order to see how the service works.

For details about the deployment process, see this document

Download the source code

Dubbokeeper: DubboKeeper: DubboKeeper: DubboKeeper

https://github.com/dubboclub/dubbokeeper
Copy the code

Let’s assume that dubbokeeper has been cloned into the ~/dubbokeeper directory.

Modify the configuration

Dubbokeeper currently supports MySQL,MongoDB, and Lucene three data storage solutions. Let’s take MySQL as an example to deploy it.

The ~/dubbokeeper/conf/dubbo-mysql.properties configuration file must be filled in. Important configuration items are described as follows:

dubbo.registry.address=zookeeper://localhost:2181 Registry address
dubbo.protocol.name=dubbo
dubbo.protocol.port=20884 #dubbo service port
dubbo.monitor.mysql.url=jdbc:mysql://localhost:3306/dubbo-monitor Mysql server address
dubbo.monitor.mysql.username=root
dubbo.monitor.mysql.password=secret Username and password
Copy the code

The next thing to fill in the ~ / dubbokeeper/dubbokeeper – UI/SRC/main/resources/dubbo. The properties in the configuration file registry and network parameters such as the server type.

Initializing the database

Create a mysql database, and run ~ / dubbokeeper/doc/storage/mysql/SQL/application. The SQL create table structure.

Compile the package

Run ~/dubbokeeper/install-mysql.sh to package the package and the target output directory will be generated.

Start the deployment

In this step, several services must be started before the application can be deployed, in order

Start the ZooKeeper service. Ensure that the port configuration is consistent with the configuration file.

Run the mysql-dubbokeeper-server/bin/start-mysql.sh command in the target directory to start the storage service.

Finally, copy the war package from mysql-dubbokeeper-UI in target directory to tomcat (self-deployed) webapps directory. Tomcat will automatically decompress and install the war package. After visiting http://localhost:8080/dubbokeeper-ui-1.0.1, see the following interface is the successful installation.

Create API

Now the programming begins in earnest. We will use Dubbo to create a very simple example with one consumer and one service provider.

The first step is to create a data exchange definition that both sides need to rely on, through an interface.

Create maven project msa-demo-api, pom. XML file:

<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. Marklux < / groupId > < artifactId > msa - demo - API < / artifactId > <version> 1.0-snapshot </version> <packaging> POM </packaging> <properties> <motan.version>0.3.0</motan.version> <! -- The version of GA widely used inside Alibaba is: 2.4.9, This version is highly recommended --> <dubbo.version>2.5.3</dubbo.version> <dubbo.version> 2.8.4</ dubbo.version> < spring version > 4.3.6. RELEASE < / spring. Version > < Java version > 1.7 < / Java version > <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> < the dependency > < groupId > com. Making. Sgroschupf < / groupId > < artifactId > zkclient < / artifactId > < version > 0.1 < / version > </dependency> <! Spring framework</groupId> <artifactId> Spring-core </artifactId> <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> < version > 1.6.11 < / version > < / dependency > < the dependency > < groupId > org. Aspectj < / groupId > <artifactId> AspectJweaver </artifactId> <version>1.6.11</version> </dependency> </dependencies> </dependencies> </project>Copy the code

Add the DemoService interface to the com.marklux.dubo. demo package as follows:

package com.marklux.dubbo.demo;

/**
 * Created by lumin on 18/5/15.
 */
public interface DemoService {
    String sayHello(String name);
}
Copy the code

Subsequent service providers implement services based on this interface, and consumers invoke services based on this interface. (Kind of like IDL files in Thrift)

Creating a service provider

Create a new Maven module, msa-demo-provider, to implement DemoService. Module structure is as follows:

A dependency on the MSA-Demo-API module needs to be added to the project’s POM.xml:

<? 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. Marklux < / groupId > < artifactId > msa - demo - provider < / artifactId > < version > 1.0 - the SNAPSHOT < / version > < dependencies > < the dependency > < groupId > com. Marklux < / groupId > <artifactId>msa-demo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </dependencies> </project>Copy the code

Write DemoServiceImpl to implement the service content as follows:

package com.marklux.dubbo.demo.impl;

import com.marklux.dubbo.demo.DemoService;
import org.springframework.stereotype.Service;

/**
 * Created by lumin on 18/5/15.
 */
@Service("demoService")
public class DemoServiceImpl implements DemoService {

    public String sayHello(String name) {
        return "Hello, "+ name; }}Copy the code

Next fill out the msa-demo-provider. XML configuration file to configure the service access registry and expose the port number:

<? 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= <dubbo:application name="dubbo_provider"/ > <! -- Use the ZooKeeper registry to expose the service address --> <dubbo:registry address=Zookeeper: / / "127.0.0.1:2181"/ > <! -- Expose service on port 20880 with dubbo --> <dubbo:protocol name="dubbo" port="20880"/ > <! <dubbo:service interface="com.marklux.dubbo.demo.DemoService" ref="demoService" />
</beans>
Copy the code

Then fill in springMVC.xml to provide the configuration of the Spring service container for the entry class:

<? xml version="1.0" encoding="UTF-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"
       default-autowire="byName">

    <aop:aspectj-autoproxy />
    <context:component-scan base-package="com.marklux.dubbo.demo" />
    <import resource="classpath:msa-demo-provider.xml" />
</beans>
Copy the code

Finally in com. Marklux. Dubbo. Demo. The test package add test start class, used to start the service:

package com.marklux.dubbo.demo.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * Created by lumin on 18/5/15.
 */
public class DemoServiceImplTest  {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-mvc.xml");
        context.start();
        System.out.println("Dubbo Service started...."); try { System.in.read(); } catch (IOException e) {e.printStackTrace(); }}}Copy the code

After starting the DemoServiceImplTest class, the service is registered and started, and you can see the status of the service in DubboKeeper:

Create consumers

Finally to create a consumer to invoke the service, add a maven module msa – demo – client, pom. XML is alexandrine, add the dependence on msa – demo – API module, pay attention to don’t need msa – demo – the dependence of the provider.

The structure of the module is as follows:

package com.marklux.dubbo.demo.test;

import com.marklux.dubbo.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * Created by lumin on 18/5/15.
 */
public class DemoServiceConsumerTest {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:springmvc.xml"});

        context.start();
        DemoService demoService = (DemoService) context.getBean("demoService");

        System.out.println(demoService.sayHello("Ha ha ha.")); try { System.in.read(); } catch (IOException e) { e.printStackTrace(); }}}Copy the code

Remember to add dubbo configuration file msa-demo-client. XML and Spring configuration file springmVC.xml as follows:

<? 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="dubbo_consumer"/ > <! -- Use multicast broadcast registries to expose discovery service addresses --> <dubbo: Registry protocol="zookeeper" address=Zookeeper: / / "127.0.0.1:2181"/ > <! <dubbo: Reference ID ="demoService" interface="com.marklux.dubbo.demo.DemoService" />
</beans>
Copy the code
<? xml version="1.0" encoding="UTF-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"
       default-autowire="byName">

    <aop:aspectj-autoproxy />
    <context:component-scan base-package="com.marklux.dubbo.demo" />
    <import resource="classpath:msa-demo-consumer.xml" />
</beans>
Copy the code

Everything is ready. Now start DemoServiceConsumerTest and you can see that DemoService was successfully called:

In dubboKeeper, you can also view its related status:

summary

At this point, a simple service-invocation demo using the Dubbo protocol is complete. The most notable feature is that no additional code is required to invoke the service provided by the service provider in the consumer, and the entire invocation process is completely transparent.

In addition, dubboKeeper can help you intuitively understand the distribution and operation of the entire application service.