Recently, I studied alibaba’s open source distributed RPC framework dubbo, and wrote a demo to experience the functions of Dubbo.

Quick start

In fact, the official Dubbo documentation already provides instructions on how to use the RPC framework example code, based on Netty long connections. The main purpose of this framework is to provide some technical storage and understand the design of distributed RPC framework in today’s microservices, Service Mesh fire.

Dubbo demo: Dubbo demo: Dubbo demo: Dubbo demo: Dubbo demo: Dubbo demo

DubboCode figure

Let me explain what each model does.

  1. Micro-service-dubbo-common: a utility module that other models rely on.
  2. Micro-service-dubbo-dal: Is the DAO module of the entire project, where the relevant code for database operations is stored.
  3. Micro-service-dubo-interface: a generic interface module that declares interfaces and is relied on by both the consumer and provider for split-down and distributed deployment of projects.
  4. Micro-service-dubo-model: a public entity class module, not limited to the model corresponding to the database, but can also store DTO, VO, and so on.
  5. Micro-service-dubbo-provider: indicates the service provider of the project.
  6. Micro-service-dubo-web: The consumer of the project and the Controller layer that interacts directly with the front end.

You also need to add related dependencies to the POM file

<! --dubbo--> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>${zkclient_version}</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>${zookeeper_version}</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>${curator_version}</version> </dependency>Copy the code

Interface to create

Since it is an RPC service, you need an interface and an implementation class. The interface definition is in our micro-service-Dubbo-interface, and the implementation is created in the provider. In our project, we create the DemoService implementation in the micro-service-dubbo-Provider.


                                                
public interface DemoService {
    String sayHello(String name);

    public List getUsers();
}


                                            Copy the code
@Service("demoService") public class DemoServiceImpl implements DemoService { @Override public String sayHello(String name) { System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext .getContext().getRemoteAddress()); return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress(); } @Override public List getUsers() { List list = new ArrayList(); User u1 = new User(); u1.setName("hejingyuan"); u1.setAge(20); u1.setSex("f"); User u2 = new User(); u2.setName("xvshu"); u2.setAge(21); u2.setSex("m"); list.add(u1); list.add(u2); return list; }}Copy the code

Consumer’s POM.xml then adds a dependency on this interface, where the interface is defined in our consumer is micro-service-dubbo-Web.

<dependency> <groupId>com.whforever</groupId> <artifactId>micro-service-dubbo-provider</artifactId> < version > 1.0 - the SNAPSHOT < / version > < / dependency > < the dependency > < groupId > com. Whforever < / groupId > < artifactId > micro - service - dubbo - interface < / artifactId > < version > 1.0 - the SNAPSHOT < / version > < / dependency >Copy the code

Once you have an interface, you need to configure it.

Interface configuration

First publish the interface to the provider. Create an XML file named: dubo-provider.xml.

Contents of the document:

<? The 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-4.3.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <! -- provider's application name, used for tracing dependency relationship --> <dubbo:application name="demo-provider"/> <! -- Use multicast Registry center to export service --> <dubbo: Registry protocol="zookeeper" address="127.0.0.1:2181" /> <! -- use dubbo protocol to export service on port 20880 --> <dubbo:protocol name="dubbo" port="20880"/> <! -- service implementation, as same as regular local bean --> <bean id="demoProviderService" class="com.whforever.service.impl.DemoServiceImpl"/> <! -- declare the service interface to be exported --> <dubbo:service interface="com.whforever.service.DemoService" ref="demoProviderService"/> </beans>Copy the code

Quite simply, you publish an interface, similar to a Bean in Spring.

Similarly, create a dubo-consumer.xml file under the consumer, micro-service-Dubo-Web resource file. The content is slightly different.

<? The 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-4.3.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <! -- consumer's application name, used for tracing dependency relationship (not a matching criterion), don't set it same as provider --> <dubbo:application name="demo-consumer"/> <! -- use multicast registry center to discover service --> <! - "dubbo: registry address =" multicast: / / 224.5.6.7:1234 "/ > -- > < dubbo: registry protocol =" zookeeper" Address = "127.0.0.1:2181" / > <! -- generate proxy for the remote service, then demoService can be used in the same way as the local regular interface --> <dubbo:reference id="demoConsumerService" check="false" interface="com.whforever.service.DemoService"/> </beans>Copy the code

It can be seen from the above that the registration and discovery protocol of the two files is ZooKeeper. Therefore, ZooKeeper needs to be started before the service starts. For details, go to the ZooKeeper registry to install and start the service

Ready to test

There’s a little work to be done before the test.

A bootstrap is required to start the provider, as shown in the following code:


                                                    
public class ProviderMain {
    public static void main(String[] args) throws IOException {
        System.setProperty("java.net.preferIPv4Stack", "true");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-provider.xml");
        context.start();

        System.in.read(); // press any key to exit
    }
}


                                                Copy the code

Consumer code

@Controller @RequestMapping("/") public class IndexController { @Autowired DemoService demoService; @RequestMapping("/echo") @ResponseBody public String echo() { System.out.println(">>>>>>echo"); return JSON.toJSONString(demoService.getUsers()); }}Copy the code

run

Run provider first:

[06/06/18 11:56:29:029 CST] main INFO config.AbstractConfig: [DUBBO] The service ready on spring started. service: Com. Whforever. Service. DemoService dubbo version: 2.6.1, current host: 192.168.1.120 [06/06/18 11:56:30:030 CST] main INFO config.AbstractConfig: [DUBBO] Export DUBBO service com. Whforever. Service. DemoService to local registry, DUBBO version: 2.6.1, current host: 192.168.1.120 [06/06/18 11:56:30:030 CST] main INFO config.AbstractConfig: [DUBBO] Export dubbo service com.whforever.service.DemoService to url Dubbo: / / 192.168.1.120:20880 / com. Whforever. Service. DemoService? Anyhost = true&application = demo - provider&bind. IP = 192.168.1.120 & bind. The port = 20880 & dubbo = 2.6.1 & generic = false&interface = com. The WHF Orever. Service. DemoService&methods = sayHello, getUsers&pid = 13992 & side = provider x tamp = 1528300589682, dubbo version: 2.6.1, current host: 192.168.1.120 [06/06/18 11:56:30:030 CST] main INFO config.AbstractConfig: [DUBBO] Register dubbo service com.whforever.service.DemoService url Dubbo: / / 192.168.1.120:20880 / com. Whforever. Service. DemoService? Anyhost = true&application = demo - provider&bind. IP = 192.168.1.120 & bind. The port = 20880 & dubbo = 2.6.1 & generic = false&interface = com. The WHF Orever. Service. DemoService&methods = sayHello, getUsers&pid = 13992 & side = provider x tamp = 1528300589682 to registry Registry: / / 127.0.0.1:2181 / com. Alibaba. Dubbo. Registry. RegistryService? Application =demo-provider&dubbo= 2.6.1&PID =13992®istry= Zookeeper ×tamp=1528300589673, dubbo version: 2.6.1, current host: 192.168.1.120 [06/06/18 11:56:30:030 CST] the main INFO. Transport AbstractServer: [DUBBO] Start NettyServer bind /0.0.0.0:20880, export /192.168.1.120:20880, DUBBO version: 2.6.1, current host: 192.168.1.120Copy the code

Run sonsumer again:

Consumer figure

After viewing the Dubbo monitoring center, the following information is displayed. For details about how to install and deploy the DuBBo monitoring center, go to the Simple Monitoring center

DubboAdmin figure

summary

I have heard about Dubbo for a long time, and only recently did I write some demos. Generally speaking, it is relatively easy to get started with detailed documentation provided by the authorities, and the community is active. About the code in this blog, the owner of the building has been put on Github, the interested partners, please go to Dubbo to experience the Demo template code

extra

LIGHTCONF is a configuration management platform based on Netty implementation. Its core design goal is to “provide unified configuration management services for business”, which can be done out of the box.

  • Netty – based lightweight distributed application configuration center