Welcome to my GitHub
Github.com/zq2599/blog…
Content: all original article classification summary and supporting source code, involving Java, Docker, Kubernetes, DevOPS, etc.;
Dubbo Combat series navigation
- Preparation and initial experience
- With SpringBoot integration
- Use the Zookeeper registry
- Admin console dubo-admin
About the dubbo
- Apache Dubbo (pronounced ʌbə) is a high-performance, lightweight, open source Java RPC framework that provides three core capabilities: interface-oriented remote method invocation, intelligent fault tolerance and load balancing, and automatic service registration and discovery;
- Here is the architecture from the official:
Version introduction and selection
- As of this writing, the Dubbo community has two major releases, 2.6.x and 2.7.x;
- 2.6. X is mainly bugfix and a small amount of enhancements, thus fully guaranteeing stability;
- 2.7. X, as the main development version of the community, has been continuously updated and added a large number of new features and optimizations, while also bringing some stability challenges;
- In summary, the Dubbo Series chose the community recommended version 2.7.6, which is a stable version of the 2.7 series;
Environmental information
My coding environment is as follows:
- Operating system: macOS Catalina 10.15.5
- The JDK: 1.8.0 comes with _121
- Maven: 33.3.9
- Development tools: IntelliJ IDEA 2019.3.2 (Ultimate Edition)
Matters needing attention
If you are running the code in Windows and have VMWare installed, please turn off the corresponding virtual network card. Otherwise, consumers may not be able to find the services they need in Multicast mode.
This paper gives an overview of
- As the beginning of the Dubbo series, this article focuses on the following:
- The parent project for the entire Dubbo series;
- Create a public two-party library for the entire Dubbo series;
- To preliminarily experience the Dubbo framework, for the sake of simplicity, the actual practice of this paper does not use the registry temporarily. Instead, the service provider broadcasts its own address when it starts, and then the consumer subscribes when it starts, and calls it remotely at any time. The call logic is as follows:
4. Create a subproject to provide the remote service, called HelloXMLDirectProvider, and run it. 5. Create a sub-project named HelloXMLDirectConsumer. After running, it will call the remote service provided by HelloXMLDirectProvider and print the content returned by the remote end.
- So let’s start coding
Download the source code
- If you don’t want to code, you can download all the source code at GitHub, with the following address and link information:
The name of the | link | note |
---|---|---|
Project home page | Github.com/zq2599/blog… | The project’s home page on GitHub |
Git repository address (HTTPS) | Github.com/zq2599/blog… | The project source warehouse address, HTTPS protocol |
Git repository address (SSH) | [email protected]:zq2599/blog_demos.git | The project source warehouse address, SSH protocol |
- The Git project has multiple folders, and the application of this chapter is in the Dubbopractice folder, as shown in the red box below:
Dubbopractice is a parent-child project. The code for this project is in the two sub-projects helloXMLDirectProvider and HelloXMLDirectConsumer, as shown below:
The father project of the Dubbo Series
- In order to facilitate the management of dubbo series code and dependency library version management, we create the parent Maven project named Dubbopractice, the entire series of subsequent source code will be as its child project;
- The pom.xml for Dubbopractice reads as follows:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modules>
<module>practiceinterface</module>
<module>helloxmldirectprovider</module>
<module>helloxmldirectconsumer</module>
<module>springbootzkprovider</module>
<module>springbootzkconsumer</module>
<module>springbootmulticastprovider</module>
<module>springbootmulticastconsumer</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
<dubbo.version>2.7.6</dubbo.version>
<springboot.version>2.3.3. RELEASE</springboot.version>
</properties>
<modelVersion>4.0.0</modelVersion>
<groupId>com.bolingcavalry</groupId>
<artifactId>dubbopractice</artifactId>
<version>1.0 the SNAPSHOT</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.25. The Final</version>
</dependency>
<! -- Dubbo related -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-multicast</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.7</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>2.11.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
<scope>compile</scope>
</dependency>
<! -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<! -- swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Copy the code
Dubbo series of two library
- It involves service invocation between multiple projects, so one project needs to store common data structures, interface definitions, etc., so a new subproject named PracticeInterface is created.
- The pom.xml for the PracticeInterface project reads as follows, which is pretty simple:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbopractice</artifactId>
<groupId>com.bolingcavalry</groupId>
<version>1.0 the SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.bolingcavalry</groupId>
<artifactId>practiceinterface</artifactId>
<version>1.0 the SNAPSHOT</version>
<name>practiceinterface</name>
<description>Beans of all app</description>
</project>
Copy the code
- This project currently has only one interface definition, which will be used by several sub-projects:
package com.bolingcavalry.dubbopractice.service;
public interface DemoService {
String sayHello(String name);
}
Copy the code
Code (Service Provider)
- Create the helloXMLDirectProvider project to provide the service. Create a total of 6 files. Create the following sequence and functions:
Create order | The file name | role |
---|---|---|
1 | pom.xml | Project POM document |
2 | src/main/java/com/bolingcavalry/helloxmldirectprovider/ProviderApplication.java | Start the class |
3 | src/main/java/com/bolingcavalry/helloxmldirectprovider/service/impl/DemoServiceImpl.java | Provide specific services |
4 | src/main/resources/log4j.properties | Log configuration file |
5 | src/main/resources/dubbo.properties | Dubbo configuration file |
6 | src/main/resources/spring/dubbo-provider.xml | Spring bean configuration |
- The complete file location is shown below:
- Next create each of these one by one;
- Create project, called helloxmldirectprovider pom. The XML content as follows, is just the new second party libraries practiceinterface was dependent on:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbopractice</artifactId>
<groupId>com.bolingcavalry</groupId>
<version>1.0 the SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.bolingcavalry</groupId>
<artifactId>helloxmldirectprovider</artifactId>
<version>1.0 the SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.bolingcavalry</groupId>
<artifactId>practiceinterface</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
</dependencies>
</project>
Copy the code
- Providerapplication. Java, a common daemon, loads the Spring configuration to initialize it:
package com.bolingcavalry.helloxmldirectprovider;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ProviderApplication {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-provider.xml"); context.start(); System.in.read(); }}Copy the code
- Write the business implementation class DemoServiceImp.java that provides the specific service, which is just a simple interface implementation class:
package com.bolingcavalry.helloxmldirectprovider.service.impl;
import com.bolingcavalry.dubbopractice.service.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.rpc.RpcContext;
@Slf4j
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
log.info("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Hello " + name + ", response from provider: "+ RpcContext.getContext().getLocalAddress(); }}Copy the code
- The log configuration file log4j.properties contains the following contents:
###set log levels###
log4j.rootLogger=info, stdout
###output to the console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy HH:mm:ss:SSS z}] %t %5p %c{2}: %m%n
Copy the code
- Create the dubbo configuration file dubbo.properties in the same location with a simple qos-only port setting to support Telnet commands:
dubbo.application.qos.port=22222
Copy the code
- New folder spring under the resources directory, create a file under the folder dubbo – provider. XML, to focus on the dubbo: registry configuration, its address attribute values for the multicast: / / 224.5.6.7:1234, On behalf of the current service to enable consumers to obtain information about themselves through broadcasting:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/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://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="demo-provider"/>
<! Broadcast mode -->
<dubbo:registry address=Multicast: / / 224.5.6.7: "1234" />
<bean id="demoService" class="com.bolingcavalry.helloxmldirectprovider.service.impl.DemoServiceImpl"/>
<dubbo:service interface="com.bolingcavalry.dubbopractice.service.DemoService" ref="demoService"/>
</beans>
Copy the code
- At this point, the coding of the service provider is completed, and the service can be started by directly running the ProviderApplication class on IDEA. The log output after successful startup is as follows:
Code (service consumer)
- Now that the service is available on the network, create a project helloXMLDirectConsumer that consumes the service. Create 5 files in the following order:
Create order | The file name | role |
---|---|---|
1 | pom.xml | Project POM document |
2 | src/main/java/com/bolingcavalry/helloxmldirectconsumer/ConsumerApplication.java | Start, invoke the remote service, and end your own process |
3 | src/main/resources/log4j.properties | Log configuration file |
4 | src/main/resources/dubbo.properties | Dubbo configuration file |
5 | src/main/resources/spring/dubbo-consumer.xml | Spring bean configuration |
- The complete file location is shown below:
- Next, create each of these files one by one;
- Helloxmldirectconsumer = helloXmlDirectConsumer = helloXmlDirectConsumer = helloXmlDirectConsumer = helloDirectConsumer = helloDirectConsumer = helloDirectConsumer = helloDirectConsumer = helloDirectConsumer = helloDirectConsumer = helloDirectConsumer = helloDirectConsumer = helloDirectConsumer = helloDirectConsumer = helloDirectConsumer
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbopractice</artifactId>
<groupId>com.bolingcavalry</groupId>
<version>1.0 the SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.bolingcavalry</groupId>
<artifactId>helloxmldirectconsumer</artifactId>
<version>1.0 the SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.bolingcavalry</groupId>
<artifactId>practiceinterface</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
</dependencies>
</project>
Copy the code
- Write the unique Java file ConsumerApplication. Java, which uses the simplest method to initialize the Spring environment, then obtains the service instance, and terminates the process after executing the method:
package com.bolingcavalry.helloxmldirectconsumer;
import com.bolingcavalry.dubbopractice.service.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ConsumerApplication {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml");
context.start();
DemoService demoService = context.getBean("demoService", DemoService.class);
String hello = demoService.sayHello("world1");
System.out.println("result: "+ hello); }}Copy the code
- The log configuration file log4j.properties contains the following contents:
###set log levels###
log4j.rootLogger=info, stdout
###output to console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy HH:mm:ss:SSS z}] %t %5p %c{2}: %m%n
Copy the code
- Create the dubbo configuration file dubbo.properties in the same location, with a simple port setting for qos only, used to support Telnet commands, which are not used in this example because the process will end after a remote call:
dubbo.application.qos.port=33333
Copy the code
- New folder spring under the resources directory, create a file under the folder dubbo – consumer. XML, to focus on the dubbo: registry configuration, its address attribute values for the multicast: / / 224.5.6.7:1234? Unicast =false indicates that the current service broadcasts its own information to consumers. Unicast =false indicates that multiple consumers can receive the broadcast:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/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://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="demo-consumer"/>
<dubbo:registry address="Multicast: / / 224.5.6.7:1234? unicast=false"/>
<dubbo:reference id="demoService" check="false" interface="com.bolingcavalry.dubbopractice.service.DemoService" timeout="2000"/>
</beans>
Copy the code
- The dubbo:reference timeout property is the timeout time for the remote call, set to 2 seconds. Note that the service provided by helloXMLDirectProvider takes 1 second to return, so this setting cannot be less than 1 second.
- At this point, the service consumer code is complete, and the ConsumerApplication class can be launched directly on IDEA. The red box is what is returned from a remote call to the HelloXMLDirectProvider service:
- This completes the preparation and initial experience of the Dubbo Series, and we will learn more about this excellent framework in the following chapters.
You are not alone, Xinchen original accompany all the way
- Java series
- Spring series
- The Docker series
- Kubernetes series
- Database + middleware series
- The conversation series
Welcome to pay attention to the public number: programmer Xin Chen
Wechat search “programmer Xin Chen”, I am Xin Chen, looking forward to enjoying the Java world with you…
Github.com/zq2599/blog…