After the dubbo-Admin console and ZooKeeper are built, today we will test the integration of spring service layer and interface.

The SSM infrastructure needs to be built by yourself. This example only provides integration on an SSM basis.

First add the following dependencies to the POM of the Service layer and the Web layer: and change both the Service layer and the Web layer to WAR packaging

<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.0</version> </dependency> < the dependency > < groupId > org. Apache. Zookeeper < / groupId > < artifactId > zookeeper < / artifactId > < version > 3.4.6 < / version > </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> The < version > 0.1 < / version > < / dependency >Copy the code

For a simple example, for the Service layer, we create two packages, one for the Service interface class and the other for the Service implementation class.



Create the DemoService interface class and DemoServiceImpl implementation class

public interface DemoService {

    public String hello(String words);
}
Copy the code

@Service("demoService")
@Transactional
@Slf4j
public class DemoServiceImpl implements DemoService {


    @Override
    public String hello(String words) {
        return words+" received"; }}Copy the code

Next configure the Spring consumer and provider configurations

Spring – the provider respectively. The XML

<! <dubbo:application name="MKShopService"/ > <! <dubbo: Registry protocol="zookeeper" address="192.168.50.10:2181"/>
<!--使用dubbo协议 端口号为20800-->
<dubbo:protocol host="192.168.50.36" name="dubbo"  port="20800"/ > <! <dubbo:service interface="com.nexus.manager.service.DemoService" ref="demoService"/>Copy the code

spring-consumer.xml

<dubbo:application name="MKShopManagerWeb"/ > <! --> <dubbo: Registry protocol="zookeeper" address="192.168.50.10:2181"/ > <! --> <dubbo:protocol host="192.168.50.36"/ > <! <dubbo: Reference interface="com.nexus.manager.service.UserService" id="userService"/>
<dubbo:reference interface="com.nexus.manager.service.DemoService" id="demoService"/>Copy the code

The best IDEA is to create two Tomcat and change different port numbers respectively. I changed them to 9080 and 8080 ports and JMX ports to 1099 and 2099.

Run the Service service first, then the Web layer. (Turn ZooKeeper on before running!)


I stepped in a lot of holes along the way,

1. SpringMVC’s loading order may cause Dubbo to re-load the bean registry service.

2. The default timeout value of dubbo is 300, which may cause an abnormal timeout. You need to adjust the timeout value.

3, LAN network machine, it is important to note that in the same local area network (LAN) is the first requirement can ping each other, and get the IP is the native IP (because for multiple network card computer, dubbo default access IP is not necessarily correct, it is possible that virtual LAN IP, lead to register, but not consumption, display timeout), the question has bothered me for a long time.


This concludes the Dubbo + ZooKeeper integration with Spring.