Getting started with dubbo configuration

The mechanism of dubbo is not mentioned here. The official documentation of dubbo explains the principle very thoroughly. Talk about using.

Use dubbo to include server and client, and also need a registration center (usually zookeeper). Dubbo is integrated with spring features, so both the server and the client need to configure their own xml files.

The client and server also need to rely on a common interface package. This interface package (usually jar) is developed by the server, which only defines the interfaces to be exposed, as well as the entities and exceptions used by these interfaces. For synchronization and updating convenience, this package is usually uploaded to the maven repository. Here it is assumed that the starting coordinates are

    <groupId>com.sth.system</groupId>
    <artifactId>sth-api</artifactId>
    <version>xxx</version>

 and contains two interfaces:

interface A{
String a();
}

interface B{
int b (int arg);
}

 

 

The server itself naturally depends on sth-api and implements the AB interface:

class AI implements A{
String a(){
return "ok";
}
}

class BI implements B{
int b(int i){
return i;
}
}

 Configure the implementation bean of the interface in the spring configuration file:

<bean id="ai" class="AI">
<bean id="bi" class="BI">

Introduce the dubbo space, configure the dubbo information and reference the implementation bean:

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://code.alibabatech.com/schema/dubbo
		http://code.alibabatech.com/schema/dubbo/dubbo.xsd”
<dubbo:application name="dubbo" />
<dubbo:registry address="${dubbo.registry.address}" />
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:service interface="A" ref="ai" protocol="dubbo" group=""/>
<dubbo:service interface="B" ref="bi" protocol="dubbo" group=""/> 

In this way, the interface is exposed after the application starts. The address of the registry is the address of zk, starting with zookeeper://, such as zookeeper://127.0.0.1:2181. Multiple zk can be separated by commas. The port of the protocol is 20880 by default. It is recommended that this attribute be set to a negative number, because it is possible that multiple applications deployed together will cause port conflicts and cause them to fail to start. For negative numbers such as -1, dubbo will automatically find a free port (starting from 20880).

 

 

The client also depends on sth-api, and then just configure dubbo:

	<dubbo:application name="hahaha" />
	<dubbo:consumer check="false" />
	<dubbo:registry address="${dubbo.registry.address}" />
	<dubbo:protocol name="dubbo" />
	<dubbo:reference group="" id="serviceA"	interface="A" />
	<dubbo:reference group="" id="serviceB"	interface="B" />

 The consumer's check is to check whether the dependent service exists. If it is not written, dubbo will go to zk to find the dependent service when it starts. If it is not found, it will fail to start. It is recommended to write it.

Unlike the server-side writing service, the client-side writing is a reference. Note that they all have the group attribute, which is used for service isolation, and the client can only call the server with the same value.

The client can call the service like this:

String s = serviceA.a();
int i = serviceB.b(1);

 

 

What everyone uses a lot is actually Dangdang’s dubbox:

<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.9</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.8.4</version>
</dependency>

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326774884&siteId=291194637