1. Provider part
Introduce dubbo configuration in applicationContent
<!-- Import dubbo configuration-->
<import resource="dubbo-service.xml" />
dubbo-service.xml is configured 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">
<!-- provider application information for calculating dependencies -->
<dubbo:application name="ms-providerimpl" />
<!-- Use the zookeeper registry to expose the service address -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- Expose services on port 20880 using the dubbo protocol -->
<dubbo:protocol name="dubbo" port="28080" />
<!-- Use annotations to expose the interface-->
<dubbo:annotation package="cn.my.providerimpl" />
<!-- Delay until Spring initialization is completed, and then expose the service, the service call timeout is set to 6 seconds, No retry on timeout -->
<dubbo:provider delay="-1" timeout="6000" retries="0"/>
</beans>
The service layer adds the interface annotations to be exposed
example:
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
@Component
@Service
public class TestServiceImpl implements TestService{
}
TestService is an exposed interface class, adding these two annotations can realize interface exposure
The consumer part
The dubbo configuration is introduced at the front in spring-mvc.xml
<!-- Import dubbo configuration-->
<import resource="dubbo-web.xml" />
dubbo-web.xml is configured 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-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- provider application information for calculating dependencies -->
<dubbo:application name="zbt" />
<!-- Use the zookeeper registry to expose the service address -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- dubbo injects with annotations -->
<dubbo:annotation package="com.test.controller" />
<!-- dubbo direct import -->
<!-- <dubbo:reference interface="com.msprovider.api.test.service.TestService" id="testService" check="false" />-->
</beans>
The exposed interface can be introduced in the controller class through annotations
example:
import com.alibaba.dubbo.config.annotation.Reference;
@Reference
private TestService testService;
Adding the @Reference annotation enables the introduction of the interface
Three; dubbo basic jar package introduction
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.9</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>