bjsxt learning notes: the most basic use of Dubbo

Posted by jobe1 on Sat, 09 Nov 2019 14:53:17 +0100

I. background of Dubbo's birth (excerpt Dubbo official website - Introduction - background)

II. Dubbo structure chart (excerpt) Dubbo official website - Introduction - Architecture)

3. Dubbo core dependency (jar package): Dubbo, zkclient

IV. construction methods of Dubbo project: profile and annotation

V. core configuration of Dubbo project configuration file:

(I) profile

1. Service provider (following example): ① application name ② protocol (dubbo protocol recommended) ③ registry ④ exposed service interface ⑤ implementation class bean object of exposed service interface

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">
    <!--Configure provider application information for dependency calculation-->
    <dubbo:application name="provide-xml"/>
    <!--Configuration Protocol (using dubbo Protocol at 20880 port(dubbo Protocol default port)Exposure services)-->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!--Configure the registry to expose service addresses-->
    <dubbo:registry address="zookeeper://192.168.110.11:2181"/>
    <!--Configure interfaces for exposed services-->
    <dubbo:service interface="com.bjsxt.dubbo.service.ITPService_xml" ref="dataServiceImpl"/>
    <!--Register the implementation class of the exposed service interface bean object-->
    <bean id="dataServiceImpl" class="com.bjsxt.dubbo.service.impl.ITPServiceImpl_xml"/>
</beans>

2. Service consumer (example below): ① application name ② registration center ③ service interface exposed by provider

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">
    <!--Configure the consumer application name to calculate the dependency. It is not a matching condition. It should not be the same as the provider-->
    <dubbo:application name="consumer-xml"></dubbo:application>
    <!--Configure registry to expose discovery service address-->
    <dubbo:registry address="zookeeper://192.168.110.11:2181"/>
    <!--Declare the exposed service interface of the service provider to be called (provided that the interface has been copied to the business layer of the consumer)-->
    <dubbo:reference interface="com.bjsxt.dubbo.service.ITPService_xml" id="iTPService"/>
</beans>

(II) annotation

1. Service provider (example below): ① application name ② protocol ③ registration center ④ dubbo annotation scanning

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">
    <!--Configure provider application information for dependency calculation-->
    <dubbo:application name="provide-annotation"/>
    <!--Configuration Protocol (using dubbo Protocol at 20880 port(dubbo Protocol default port)Exposure services)-->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!--Configure the registry to expose service addresses-->
    <dubbo:registry address="zookeeper://192.168.110.11:2181"/>
    <!--To configure dubbo Annotation scan(Annotating: Using dubbo Annotations@Service To replace the other two configurations)-->
    <dubbo:annotation package="com.bjsxt.dubbo.service.impl"/>
</beans>

2. Service consumer (the following example): ① application name ② registration center ③ dubbo annotation scanning ④ declare the business layer implementation class bean object of the exposed service interface

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">
    <!--Configure the consumer application name to calculate the dependency. It is not a matching condition. It should not be the same as the provider-->
    <dubbo:application name="consumer-annotation"></dubbo:application>
    <!--Configure registry to expose discovery service address-->
    <dubbo:registry address="zookeeper://192.168.110.11:2181"/>
    <!--To configure dubbo Annotation scan-->
    <dubbo:annotation package="com.bjsxt.dubbo.service.impl"/>
    <!--Configure the business implementation class of the service interface exposed by the service consumer calling the provider bean Right image-->
    <bean id="iTServiceConsumerImpl_SSM" class="com.bjsxt.dubbo.service.impl.ITServiceConsumerImpl_SSM"/>
</beans>

Vi. example - simple implementation of Dubbo project:

(0) environment construction of my test project:

① the quickstart template of Java under maven ② does not use SSM framework ③ the registration center selects zookeeper and installs it in the Linux virtual machine

④ there is no persistence layer in the object-oriented three-tier architecture, only the data class pojo/entity (the test data itself is set in the test method of the implementation class of the service interface exposed by the service provider)

(I) profile

1. Project code:

(1) service provider:

① data pojo/entity

package com.bjsxt.dubbo.pojo;

import lombok.Data;
import java.io.Serializable;

@Data
public class TestData implements Serializable {
    private Integer uid;
    private String uname;
}

② business layer service

i. exposed service interfaces

package com.bjsxt.dubbo.service;

import com.bjsxt.dubbo.pojo.TestData;
import java.util.List;

public interface ITPService_xml {
    //Query test data
    List<TestData> selectTestData();
}

ii. Implementation class of exposed service interface

package com.bjsxt.dubbo.service.impl;

import com.bjsxt.dubbo.pojo.TestData;
import com.bjsxt.dubbo.service.ITPService_xml;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
 * spring The @ Autowired annotation, @ Resource annotation and @ Service annotation in the framework
 * @Autowired Annotation omits the < property > configured in < bean > in xml and the set and get methods of properties in corresponding < bean > classes.
 * @Resource Annotations are very similar to @ Autowired annotations
 * @Service Annotation omits the configuration of < bean > in xml Implement xml zero configuration
 * Details: again~
 */
@Service
public class ITPServiceImpl_xml implements ITPService_xml {
    //Query test data(Omit persistence layer and customize test data)
    @Override
    public List<TestData> selectTestData() {
        ArrayList<TestData> list=new ArrayList<>();
        TestData testData=new TestData();
        testData.setUid(0);
        testData.setUname("fuck");
        list.add(testData);
        return list;
    }
}

(2) service consumer:

① data pojo/entity: the same as above

(2) business layer service: copy the code of the service interface exposed by the service provider

2. Starting mode:

(1) service provider:

① API loading configuration file (I didn't write this method) (please refer to the following startup method of service consumer, or see the example code given on Dubbo official website):

import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"http://10.20.160.198/wiki/display/dubbo/provider.xml"});
        context.start();
        System.in.read(); // press any key to exit
    }
}

(2) main method of using dubbo service to run container:

Premise: place the spring configuration file in the spring directory under the META-INF directory, otherwise the dubbo service run container cannot find the spring configuration file

package com.bjsxt.dubbo;

import com.alibaba.dubbo.container.Main;
public class AppTest {
    /**
     * Start dubbo's service running Container by loading the spring configuration file
     * (This container is of spring type,
     *      dubbo In addition to directly loading the spring configuration file,
     *      Next, load the spring configuration file through the Main class in dubbo
     *          -This method needs to put the spring configuration file in the "spring" directory under the "META-INF" directory.)
     */
    public static void main(String[] args) {Main.main(args);}
}

[operation result · figure] (premise: the zookeeper registration center has been started, and the registration center has been started successfully) (Note: after operation, it must be closed manually)

(2) service consumer: API load configuration file

package com.bjsxt.dubbo;

import com.bjsxt.dubbo.pojo.TestData;
import com.bjsxt.dubbo.service.ITPService_xml;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
 * It's just a simple test. It doesn't involve the web, nor does the service provider involve the persistence layer
 */
public class AppTest {
    @Test
    public void test(){
        /*Load spring profile*/
        ApplicationContext ac=new ClassPathXmlApplicationContext("spring-consumer.xml");
        /*Obtain business layer objects of exposed service interface through profile reflection*/
        ITPService_xml itpService_xml = ac.getBean("iTPService", ITPService_xml.class);
        /*Get service return value*/
        List<TestData> list=itpService_xml.selectTestData();
        /*output*/
        System.out.println(list);
    }
}

[operation result · figure]

(II) annotation

1. Project code:

(1) service provider:

① data pojo/entity: the same as above

② service of business layer: the same as above

(2) service consumer:

① data pojo/entity: the same as above

② business layer service:

i. copy the code of the service interface exposed by the service provider

ii. Create consumer interface calling service interface and its implementation class

package com.bjsxt.dubbo.service;

import com.bjsxt.dubbo.pojo.TestData;
import java.util.List;

public interface ITServiceConsumer_SSM {
    //Display test data
    List<TestData> show();
}
package com.bjsxt.dubbo.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.bjsxt.dubbo.pojo.TestData;
import com.bjsxt.dubbo.service.ITService_SSM;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ITServiceConsumerImpl_SSM implements com.bjsxt.dubbo.service.ITServiceConsumer_SSM {
    @Reference
    private ITService_SSM itService_ssm;
    @Override
    public List<TestData> show() {
        return itService_ssm.selecte();
    }
}

2. Starting mode:

(1) service provider: the same as above

(2) service consumer: the same as above

(3) compare the differences between the two methods:

1. Service provider

(1) profile: ① profile type:

i. special configuration is required: exposed service interface, bean object of business layer implementation class declaring exposed service interface

ii. No configuration required: dubbo annotation scanning

② annotation:

i. special configuration required: dubbo annotation scanning

ii. No configuration required: exposed service interface, bean object of business layer implementation class declaring exposed service interface

(2) annotation: ① profile style: in the business layer, the annotation @ Service of the implementation class of the exposed Service interface is spring framework

(2) annotation: in the business layer, the annotation @ Service of the implementation class of the exposed Service interface is dependent on dubbo

2. Service consumer:

(1) profile: ① profile type:

i. no configuration required: dubbo annotation scan

② annotation:

i. special configuration required: dubbo annotation scanning

ii. No configuration required: exposed service interface, bean object of business layer implementation class declaring exposed service interface

(2) annotation: annotation: in the business layer, in the exposed Service interface implementation class: ① the @ Service of the annotation implementation class is dubbo dependent; ② the attribute annotation of the implementation class object of the Service interface exposed by the Service provider is @ Reference in dubbo dependency

Topics: Java Dubbo Spring xml