Dubbo Entry Demo

1. Dubbo brief introduction

Dubbo is a distributed service framework dedicated to providing high-performance and transparent RPC remote service invocation solutions, as well as SOA service governance solutions. Simply put, dubbo is a service framework. If there is no distributed requirement, it is actually unnecessary. Only when it is distributed, there is a requirement for a distributed service framework like dubbo, and it is essentially a service call. Dongdong, to put it bluntly, is a distributed framework for remote service invocation (say goodbye to WSdl in the Web Service mode, and register on dubbo in the form of a server and a consumer).
Its core parts include:
1. Remote communication: Provide a variety of The abstract encapsulation of the NIO framework based on long connections, including multiple thread models, serialization, and information exchange in the "request-response" mode.
2. Cluster fault tolerance: Provides transparent remote procedure calls based on interface methods, including multi-protocol support, and cluster support such as soft load balancing, failure tolerance, address routing, and dynamic configuration.
3. Automatic discovery: Based on the registry directory service, the service consumer can dynamically find the service provider, making the address transparent, so that the service provider can smoothly increase or decrease the machine.

2. What can Dubbo do?

1. Transparent remote method call, just like calling a local method to call a remote method, just simple configuration, without any API intrusion.      
2. Soft load balancing and fault tolerance mechanism can replace hardware load balancers such as F5 in the intranet, reduce costs and reduce single points.
3. The service is automatically registered and discovered, and it is no longer necessary to write the address of the service provider. The registration center queries the IP address of the service provider based on the interface name, and can smoothly add or delete service providers.

Dubbo adopts the full Spring configuration method to transparently access the application without any API intrusion into the application. You only need to use Spring to load the Dubbo configuration, and Dubbo is loaded based on Spring's Schema extension.

2.Dubbo entry Demo construction

1. Create a maven project, such as dubbo-demo-provider, groupId: com.liddhome, artifactId: dubbo-demo-provider, which is a service provider, and its pom.xml dependencies are as follows:

<dependencies>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.6.0</version>
    </dependency>
    <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.10</version>
    </dependency>
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>4.0.1</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.46</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.4</version>
    </dependency>
    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>4.0.35.Final</version>
    </dependency>
</dependencies>

Create a new package under src, such as com.liddhome.service, and create an interface class DemoProviderService under this package. The code is as follows

package com.liddhome.service;

public interface DemoProviderService {

	public String sayhello(String str);
}

Create a new package, such as com.liddhome.serviceImpl, and build the implementation class DemoProviderServiceImp under this package. The code is as follows:

package com.liddhome.serviceImpl;

import com.liddhome.service.DemoProviderService;

public class DemoProviderServiceImp implements DemoProviderService{

	@Override
	public String sayhello(String str) {
		return str+"dubbo-zookeeper";
	}

}

Create a new dubbo-demo-provider.xml file under the resource of the project with the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

	<dubbo:application name="demo-provider"/>
	
	<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
	
	<dubbo:protocol name="dubbo" port="20881"/>
	
	<bean name="demoProvider" class="com.liddhome.serviceImpl.DemoProviderServiceImp"/>
	
	<dubbo:service interface="com.liddhome.service.DemoProviderService" ref="demoProvider"/>

</beans>

Write the test class:

package dubbotest;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ProviderTest {
	
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-demo-provider.xml"});
	    context.start();

	    System.out.println("Provider:20881注册服务成功!");
	    try {
			System.in.read();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // press any key to exit
	    context.close();
	}
	
}

Download zookeeper, start zookeeper, then run the test class, the console will display:

3. Consumers

The same service provider, create a new maven project, dubbo-demo-consumer

pom.xml file dependency content:

<dependencies>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.6.0</version>
    </dependency>
    <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.10</version>
    </dependency>
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>4.0.1</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.46</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.4</version>
    </dependency>
    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>4.0.35.Final</version>
    </dependency>
</dependencies>

Create a new package under src, com.liddhome.service

Copy the interface class of the service provider

package com.liddhome.service;

public interface DemoProviderService {

	public String sayhello(String str);
}

Newly built dubbo-demo-consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="demo-consumer"/>

    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <dubbo:reference id="demoProviderService" check="false" interface="com.liddhome.service.DemoProviderService"/>

</beans>

New test class:

package consumertest;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.liddhome.service.DemoProviderService;

public class ConsumerTest {

	public static void main(String[] args) {
		
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-demo-consumer.xml"});
        context.start();
        DemoProviderService demoService = (DemoProviderService) context.getBean("demoProviderService"); // get remote service proxy
        String result = demoService.sayhello("Hello ");
        System.out.println("远程调用结果是:"+result);
        try {
			System.in.read();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        context.close();
       


        }
}

Start the test class to consume the service provided by the consumer. We only define the interface of the consumer, but do not implement the interface. This is the implementation of the interface provided by the service provider:

OK, a demo has been implemented, but this interface has been written twice, there are repetitions, and the dependencies in the project's pom are also repeated. The next article will extract some public content to facilitate project management.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325023981&siteId=291194637
Recommended