Record a personal experience of the dubbo project

Keywords: MySQL Dubbo Spring Zookeeper xml

I. Case Description

There are two systems, A system and B system. A system calls the interface of B system to get data for querying user list.

II. Environmental Construction

Install zookeeper and decompress (zookeeper-3.4.8.tar.gz) as follows:

Then enter conf and rename zoo_sample.cfg to zoo.cfg. And related to the following:

This directory is a directory for storing data. Then start, in the bin directory:

III. Engineering Creation

1. Building Project B

1. Import dependency

<dependencies>
<!-- dubbo Use spring Configuration, so you need to import spring Container dependency -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<!-- Exclusion and transmission spring rely on -->
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- zookeeper rely on -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>

2. Creating Objects

public class User implements Serializable{

//Serialization automatic generation
private static final long serialVersionUID = 1749666453251148943L;

private Long id;
private String username;
private String password;
private Integer age; 
//getter and setter
}

3. Creating Services

public class UserServiceImpl implements UserService {

//Implement query, do simulation here, do not do specific database query
public List<User> queryAll() {
List<User> list = new ArrayList<User>();
for (int i = 0; i < 10; i++) {
User user = new User();
user.setAge(10 + i);
user.setId(Long.valueOf(i + 1));
user.setPassword("123456");
user.setUsername("username_" + i);
list.add(user);
}
return list;
}
}

4. Write the Dubbo configuration file

I put dubbo/dubbo-server.xml in the root directory as follows:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org
/schema/context" xmlns:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx"
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://www.springframework.org/schema/context
http://www.springframework.org/schema/context
/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop
/spring-aop-4.0.xsd http://www.springframework.org
/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<!-- Provider application information for computing dependencies -->
<dubbo:application name="dubbo-b-server" />
<!-- The registry used here is zookeeper -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient"/>
<!-- use dubbo Protocol Exposure Service at Port 20880 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- Expose the interface to dubbo in -->
<dubbo:service interface="com.shen.dubbo.service.
UserService" ref="userServiceImpl" />
<!-- Add specific implementation classes to Spring Container -->
<bean id="userServiceImpl" class="com.shen.dubbo.service.impl.UserServiceImpl" />
</beans>

5. Writing Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>dubbo-b</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo/dubbo-*.xml</param-value>
</context-param>
<!--Spring Of ApplicationContext load -->
<listener>
<listener-class>org.springframework.web.context.
ContextLoaderListener</listener-class>
</listener>
</web-app>

6. Start tomcat

The following will be seen in the console:

As you can see, the UserService service has been registered in the zookeeper registry, and the protocol is dubbo.

2. Building Project A

1. Copying basic documents

Copy User Object, UserService Interface from b System to a System

2. Write the Dubbo configuration file

<! - Provider application information for computing dependencies - >
<dubbo:application name="dubbo-a-consumer" />
<! - The registry used here is zookeeper - >.
<dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient"/>
<! - Search for services from the registry - >
<dubbo:reference id="userService" interface="com.shen.dubbo.service.UserService"/>

3. Write UserService test cases

public class UserServiceTest {
private UserService userService;
@Before
public void setUp() throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"classpath:dubbo/*.xml");
this.userService = applicationContext.getBean(UserService.class);
}
@Test
public void testQueryAll() {
List<User> users = this.userService.queryAll();
for (User user : users) {
System.out.println(user);
}
}
}

The results are as follows:

As you can see, 10 pieces of data have been queried, that is to say, system A gets the data through the services provided by system B.

Here I recommend an architecture learning exchange group. Communication Learning Group Number: 478030634 will share some videos recorded by senior architects: Spring, MyBatis, Netty Source Code Analysis, Principles of High Concurrency, High Performance, Distributed, Microsoft Architecture, JVM Performance Optimization, Distributed Architecture and so on, which become necessary knowledge systems for architects. Free learning resources are also available, which have benefited a lot at present.

3. Solving the problem of code duplication

We can see that in the above case, both User entity and service interface projects need to be used, and code reuse is not high. Then we can extract this part of the code and package it for all systems to use. So you can create a project named dubbo-b-api. Then put the relevant code into the project, and then import the project dependencies into other projects. This is what we should do in a real project, because the caller may not know the details.

Posted by damien@damosworld.com on Sat, 26 Jan 2019 09:21:14 -0800