SpringBoot| SpringBoot integrates Dubbo

Table of Contents

1: SpringBoot integrates Dubbo

1. Create a public project

2. Create a provider project provider

3. Create consumer project

4. Installation of Zookeeper in the registration center

Book recommendation: “Python Automated Office Application Encyclopedia”


One: SpringBoot integrates Dubbo

Alibaba provides dubbo integration springBoot open source project, you can go to GitHub GitHub – apache/dubbo-spring-boot-project: Spring Boot Project for Apache Dubbo to view the introductory tutorial

The Apache Dubbo Spring Boot project makes it easy to create Spring Boot applications using Dubbo as an RPC framework. What’s more, it also provides:

  • Auto-configuration capabilities (e.g., annotation-driven, auto-configuration, externalized configuration).
  • Production-ready features (e.g., security, health checks, externalized configuration).

Apache Dubbo is a high-performance, lightweight, java-based RPC framework. Dubbo provides three key functions, including interface-based remote invocation, fault tolerance and load balancing, and automatic service registration and discovery.

Release version

dubbo-spring-boot-starterYou can bring the latest content into your project by adding the following dependencies to pom.xml.

<dependencies>
    <!-- Dubbo Spring Boot Starter -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>2.7.8</version>
    </dependency>
</dependencies>

1. Create a public project

According to Dubbo’s official development recommendations, create an interface project that only defines the interface and model class. This project is an ordinary maven project.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.zl</groupId>
  <artifactId>dubbo-maven-1</artifactId>
  <version>1.0-SNAPSHOT</version>
  
</project>

model class

package com.zl.model;

import java.io.Serializable;

public class Student implements Serializable {
    private static final long serialVersionUID = 7924975682459971235L;
    private Integer id;
    private String name;
    private Integer age;

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", age=" + age +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

Service interface

package com.zl.service;

import com.zl.model.Student;


public interface StudentService {
    Student queryStudent(Integer id);
}

2. Create provider project provider

It is a SpringBoot project, so a new project needs to be created!

Step 1: Create a SpringBoot project and introduce core dependencies

pom.xml

Core dependencies: gav, dubbo dependencies, and zookeeper dependencies of public projects.

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.15</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.zl</groupId>
<artifactId>dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-provider</name>
<description>dubbo-provider</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<!--Add gav to public projects-->
<dependency>
<groupId>com.zl</groupId>
<artifactId>dubbo-maven-1</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--dubbo dependency-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<!--zookeeper dependency-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.8</version>
<type>pom</type>
<exclusions>
<!-- Exclude log4j dependency -->
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Step 2: Implement the interface of the Service public project and expose the service

Note: Use the @DubboService annotation to expose the service; use the interfaceClass attribute to specify the interface class, the version attribute to specify the version number, and timeout to specify the delay time.

package com.zl.service.impl;

import com.zl.model.Student;
import com.zl.service.StudentService;
import org.apache.dubbo.config.annotation.DubboService;

//Expose the service
@DubboService(interfaceClass = StudentService.class,version = "1.0"
,timeout = 5000)
public class StudentServiceImpl implements StudentService {
    @Override
    public Student queryStudent(Integer id) {
        Student student = new Student();
        if (1001 == id){
            student.setId(1001);
            student.setName("Jack");
            student.setAge(18);
        }else if(1002 == id){
            student.setId(1002);
            student.setName("Rose");
            student.setAge(22);
        }

        return student;
    }
}

Step 3: Configure dubbo properties with application.properties

#Configure server name
spring.application.name=studentService-provider
#Configure scanned packages
dubbo.scan.base-packages=com.zl.service
#Configure dubbo protocol
#dubbo.protocol.name=dubbo
#dubbo.protocol.port=20881
#registrationcenter
dubbo.registry.address=zookeeper://localhost:2181

Step 4: Start the class

Note: Adapt the @EnableDubbo annotation to start dubbo. This is a compound annotation, including @EnableDubboConfig and

@DubboComponentScan function.

package com.zl;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo // Start Dubbo
public class DubboProviderApplication {

public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}

}

3. Create consumer project

Step 1: Create a SpringBoot project and introduce core dependencies, which are the same as the provider; add a web dependency to facilitate testing

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

Step 2: The controller calls the remote service

Note: Use the @DubboReference annotation to call the remote service, and inject the created proxy object into the studentService.

package com.zl.controller;

import com.zl.model.Student;
import com.zl.service.StudentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DubboController {
    // You can also omit the interfaceClass attribute here
    @DubboReference(interfaceClass = StudentService.class,version = "1.0")
    private StudentService studentService;

    @GetMapping("/query")
    public String queryStudent(){
        Student student = studentService.queryStudent(1001);
        return "Call the remote interface and get the object:" + student;
    }

}

Step 3: Configure dubbo properties with application.properties

#Specify service name
spring.application.name=consumer-application
#Designated registration center
dubbo.registry.address=zookeeper://localhost:2181

Step 4: Start the class

package com.zl;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class DubboConsumerApplication {

public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}

}

4. Installation of Zookeeper in the registration center

Unzip the downloaded Zookeeper compressed package:

C:\dev\Zookeeper\apache-zookeeper-3.5.5-bin\conf\zoo.cfg for configuration

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgment
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.

#dataDir=/tmp/zookeeper
#Modify the directory where temporarily generated data is stored
dataDir=C:/dev/Zookeeper/apache-zookeeper-3.5.5-bin/data

# the port at which the clients will connect
#The port number
clientPort=2181
#You need to start another service. The default port is 8080. Modify it to prevent conflicts.
admin.serverPort=8888
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
#Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

Execute the zkServer.cmd program to start Zookeeper

Startup classes for provider and consumer projects

to visit

Book recommendation: “Python Automated Office Application Encyclopedia”

1 book is given away this time!
Activity time: Until 2023-09-29 00:00:00.

Lottery method: Use the program to conduct a lottery.

How to participate: Follow the blogger (for fan benefits only), like and collect, and the comment area will be randomly selected, with a maximum of three comments!

Key Points

1. Easily realize office automation with the help of ChatGPT and Python.

2. Excel Home is created by several Microsoft global MVP experts. It uses a large number of examples to introduce the use of Python to operate Excel, Word, PPT and various objects involved in daily office work.

3.Novel way Details how to use ChatGPT to supplement learning knowledge points and how to quickly generate the required code, further reducing the cost of learning programming for people with zero basic knowledge.

4. Rich content Focusing on Excel data processing and analysis, it extends to modern offices such as Word, PPT, email, pictures, videos, audios, local file management, and web page interaction. Various forms of data that need to be processed.

5. Practical cases Use a large number of easy-to-reference cases to help users learn to use automation technology in various scenarios.

6. Author authority Planned by the Excel Home team and created by multiple Microsoft global Most Valuable Professionals (MVPs) to ensure that each case is practical and friendly to programming novices.

7. Allow ordinary office workers without programming experience to control Python, realize office automation in multiple scenarios, and improve efficiency!

Introduction

Focusing on Excel data processing and analysis, it extends to Word, PPT, emails, pictures, videos, audios, local file management, web page interaction and other forms of data that need to be processed in modern offices.

Dangdang.com link: “Comprehensive Collection of Python Automated Office Applications (ChatGPT Version)”

JD.com’s link: JD.com Security

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Cloud native entry-level skills treeHomepageOverview 15,905 people are learning the system