Overall process of springboot Dubbo addition, deletion, modification and query

Keywords: Dubbo Spring Boot

springboot-dubbo

First, create a simple maven project

It stores entity classes and service classes

Entity class to inherit Serializable

Entity classes can introduce lombok and rely on an @ Data annotation

        <dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>

The service layer writes a simple interface

    List<User> getAll();//query

    Integer update(User user);//modify

    Integer add(User user);//add to

    Integer delete(Integer id);//Delete by id

Creating a provider project

  1. provider role
    The project provides the Service of Service interface and registers the Service in Zookeeper registry for Service consumers to subscribe and call.
  2. provider principle
    Configure the Zookeeper registry address in the project and expose the service implementation class to the registry. (the principle and application process of Zookeeper registration center will be described below)
  3. provider implementation steps
    1) Add springboot Dubbo dependencies: spring boot starter Dubbo and Zookeeper registry dependencies
        <!-- dubbo integrate springboot Dependence of -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
        <!-- zookeeper Registration Center -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
            <type>pom</type>
        </dependency>

Another is to introduce the interface dependency in the newly created api project
Open the newly created api project pom.xml file and copy it to the provider project pom.xml to introduce the interface dependency

Database dependency and mybatis dependency

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

The next step is to configure StringBoot to integrate Dubbo related server configuration information (application.properties)

# apply name
spring.application.name=dubbo-consumer

#Database connection address
spring.datasource.url=jdbc:mysql://localhost:3306/scdb20210906?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC

#Database user name & password
spring.datasource.username=root
spring.datasource.password=123456

#Database driven
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#Application service WEB access port
server.port=8082

#Configure dubbo service name
dubbo.application.name=dubbo-consumer
#Configure registry address
dubbo.registry.address=zookeeper://127.0.0.1:2181



mybatis.typeAliasesPackage=com.ck.entity
mybatis.mapperLocations=classpath:mapper/*.xml
spring.profiles.active=dev
  • Note: the address of Zookeeper registration center is: Zookeeper://127.0.0.1:2181 The provider project port number is
    8002, exposed service port number: 20882

Get ready to start writing interfaces

Create a mapper class

    List<User> getAll();//query

    Integer update(User user);//modify

    Integer add(User user);//add to

    Integer delete(Integer id);//Delete by id

Create the provided service (service.impl)

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public List<User> getAll() {
        return userMapper.getAll();
    }

    @Override
    public Integer update(User user) {
        return userMapper.update(user);
    }

    @Override
    public Integer add(User user) {
        return userMapper.add(user);
    }

    @Override
    public Integer delete(Integer id) {
        return userMapper.delete(id);
    }

Create mapper folder
Mapper.xml (storing sql statements)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.ck.mapper.UserMapper">
        <select id="getAll" resultType="user">
            select * from user
        </select>
        <update id="update">
            UPDATE `user` SET `name` = #{name}, `age` = #{age}, `sex` = #{sex} WHERE
            `id` = #{id};
        </update>
        <insert id="add">
            INSERT INTO `user` (`name`, `age`, `sex`) VALUES (#{name},#{age},#{sex});
        </insert>
        <delete id="delete">
            delete from user where id=#{id};
        </delete>
    </mapper>

namespace = "method of interface"

Publishing services

@MapperScan("com.ck.mapper")
@EnableDubbo
@SpringBootApplication
public class DubboServiceApplication {

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

}

Pay attention to a few notes
@MapperScan("scan interface package")
@Enable Dubbo

Create another springboot Dubbo consumer project

  1. consumer action
    The project consumes the Service of the Service interface and subscribes to call the Service service from the Zookeeper registry.

  2. consumer principle
    Configure the Zookeeper registry address in the project, and the project calls the remote Service according to the interface type.

  3. consumer implementation steps
    Add springboot Dubbo dependencies: spring boot starter Dubbo and Zookeeper registry dependencies

        <!-- dubbo integrate springboot Dependence of -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
        <!-- zookeeper Registration Center -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
            <type>pom</type>
        </dependency>

Configure the configuration information (application.properties) of the client related to the StringBoot integration Dubbo in the configuration file
(just the same as the provider project configuration)

# apply name
spring.application.name=dubbo-consumer

#Database connection address
spring.datasource.url=jdbc:mysql://localhost:3306/scdb20210906?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC

#Database user name & password
spring.datasource.username=root
spring.datasource.password=123456

#Database driven
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#Application service WEB access port
server.port=8082

#Configure dubbo service name
dubbo.application.name=dubbo-consumer
#Configure registry address
dubbo.registry.address=zookeeper://127.0.0.1:2181



mybatis.typeAliasesPackage=com.ck.entity
mybatis.mapperLocations=classpath:mapper/*.xml
spring.profiles.active=dev

Create service consumption class

@RestController
public class UserController {
    @Reference
    UserService userService;

    //query
    @RequestMapping("/getAll")
    public List<User> getAll(){
        return userService.getAll();
    }

    //modify
    @RequestMapping("/update")
    public Integer update(User user) {
        return userService.update(user);
    }

    //add to
    @RequestMapping("/add")
    public Integer add(User user) {
        return userService.add(user);
    }

    //add to
    @RequestMapping("/delete")
    public Integer delete(Integer id) {
        return userService.delete(id);
    }

Declare attributes of the same type in the controller layer (or other layers), and use @ Reference to automatically inject remote services
@The package of Reference is com.alibaba.dubbo.config.annotation.Reference

Start the SpringBoot project SpringBoot Dubbo consumer on the premise that the zookeeper registry and SpringBoot Dubbo provider have been started

Now let's talk about the zookeeper registry
I'm ready for you to download and unzip directly
Link: https://pan.baidu.com/s/1eCwsQHHWD0gVeLJ2qzTBFQ
Extraction code: 0zme
You can also go to the official website to download http://www.apache.org/dyn/closer.cgi/zookeeper After downloading, unzip it

Open bin

Double click to open the red box to run


It shows that 0.0.0.0:2181 runs successfully

Zookeeper precautions

  1. The connection port number of Zookeeper client is configured as 2181. Remember that we will use this port number for future connections. (after running the Zookeeper registry, ports 2181 and 8080 will be occupied)
  2. After Zookeeper is started, the command line window is always open. Do not turn off this cmd. Remember to start Zookeeper before Dubbo consumers and providers run.

Then run the project

Operation steps:

  1. Ensure the normal operation and successful operation of Zookeeper registration center
  2. Run provider project
  3. consumer project

Finally, make sure that the ports of the two projects are different

#Application service WEB access port
server.port=8082

After the project is started successfully, access localhost: (the port of the consumer project) / method name

Posted by cpjok on Wed, 15 Sep 2021 16:26:34 -0700