SpringBoot+Dubbo builds microservices

With the development of the Internet, the scale of website applications continues to expand, and conventional vertical application architectures can no longer cope with it. Distributed service architecture and mobile computing architecture are imperative, and a governance system is urgently needed to ensure an orderly evolution of the architecture.

1. First come a picture

Speaking of Dubbo, I believe everyone will be familiar with it! A high-performance and excellent service framework open sourced by Alibaba Company can enable applications to implement service output and input functions through high-performance RPC, and can be seamlessly integrated with the Spring framework.

 

 

Dubbo architecture diagram

Node role description:

  • Provider : The service provider that exposes the service
  • Consumer : The service consumer who calls the remote service
  • Registry : The registry for service registration and discovery
  • Monitor : A monitoring center that counts the number of service calls and call time
  • Container : Service running container

Two, realization of ideas

Today, we use the process of a user to choose a product to place an order and split it into three business services: user center, product center, and order center . Use Springboot + Dubbo to implement a small Demo!

The service interaction process is as follows:

This article mainly introduces the framework integration and development practice of Springboot and Dubbo. The real business service splitting is a very complicated process, which is much more complicated than the one we introduced. The three services mentioned above are just for project demonstration , Don’t be too entangled why you want to split like this !

Okay, let's not talk too much nonsense, let's open it!

  • 1. Create 4 centos7 in the virtual machine, choose any one to install zookeeper
  • 2. Build a microservice project and write code
  • 3. Deploy microservices on centos7
  • 4. Remote service call test

Three, zookeeper installation

在使用 Dubbo 之前,我们需要一个注册中心,目前 Dubbo 可以选择的注册中心有 zookeeper、Nacos 等,一般建议使用 zookeeper!

First, before installing Zookeeper, you need to install and configure JDK. This machine uses Oracle Java8 SE.

  • Install JDK (it can be ignored if already installed)
yum -y install java-1.8.0-openjdk
  • View java installation
java -version

 

  • After the JDK installation is complete, download and install Zookeeper
#创建一个zookeeper文件夹
cd /usr
mkdir zookeeper

#下载zookeeper-3.4.14版本
wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz

#解压
tar -zxvf zookeeper-3.4.14.tar.gz

Create data and log directories

#创建数据和日志存放目录
cd /usr/zookeeper/
mkdir data
mkdir log

#把conf下的zoo_sample.cfg备份一份,然后重命名为zoo.cfg
cd conf/
cp zoo_sample.cfg zoo.cfg

Configure zookeeper

#编辑zoo.cfg文件
vim zoo.cfg

  • Start Zookeeper
#进入Zookeeper的bin目录
cd zookeeper/zookeeper-3.4.14/bin

#启动Zookeeper
./zkServer.sh start

#查询Zookeeper状态
./zkServer.sh status

#关闭Zookeeper状态
./zkServer.sh stop

The following message appears, indicating successful startup!

4. Project introduction

  • springboot version: 2.1.1.RELEASE
  • zookeeper version: 3.4.14
  • dubbo version: 2.7.3
  • mybtais-plus version: 3.0.6
  • Database: mysql-8
  • Build tool: maven
  • Service modules: user center, product center, order center

Five, code practice

5.1, initialize the database

First, create 3 databases on the mysql client, namely: dianshang-user,, dianshang-platformand dianshang-business.

  • In the dianshang-user database, create the user table tb_user and initialize the data

  • In the dianshang-platform database, create the product table tb_product and initialize the data

  • In the dianshang-platform database, create the order table tb_order and order detail table tb_order_detail

5.2, create a project

After the database table design is complete, create a name for the IDEA in dianshangthe Springbootproject.

The final catalog is as follows:

Directory structure description:

  • dianshang-common: mainly stores some public tool libraries, all services can be used
  • dianshang-business: Order center, where the apimodule mainly provides the dubbo service exposure interface, and the providermodule is a springbootproject that provides service processing operations
  • dianshang-user: User center, where apimodules and providermodules are similar in design
  • dianshang-platform: Commodity center, where apimodules and providermodules are similar in design

pomAdd dubboand zookeeperclient in the parent file , all dependent projects can be used.

<!-- lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.4</version>
    <scope>provided</scope>
</dependency>

<!-- Dubbo Spring Boot Starter -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.7.3</version>
</dependency>
<!-- 因为使用的是 zookeeper 作为注册中心,所以要添加 zookeeper 依赖 -->
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.13</version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
        <exclusion>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!--使用curator 作为zookeeper客户端-->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>4.2.0</version>
</dependency>
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>4.2.0</version>
</dependency>

Reminder : When I set up the environment, the editor found a pit. The zookeeperversion that the project relies on should be consistent with the version of the server. For example, zookeeperthe version of the server in this example is 3.4.14, so when relying on the zookeeperfile library, try to keep it Consistent, if you rely on the 3.5.xversion zookeeper, all kinds of monsters and ghosts will report errors when the project starts!

5.3, create a user center project

In IDEA, create dianshang-usersubmodules and rely on dianshang-commonmodules

<dependencies>
    <dependency>
        <groupId>org.project.demo</groupId>
        <artifactId>dianshang-common</artifactId>
        <version>1.0.0</version>
    </dependency>
</dependencies>

At the same time, create dianshang-user-providerand dianshang-user-apimodule.

  • dianshang-user-api: mainly provides interface exposure to other services
  • dianshang-user-provider: similar to a web project, mainly responsible for basic business crud, and relying on dianshang-user-apimodules

5.3.1, configure dubbo service

Configure the service in dianshang-user-providerthe application.ymlfile dubboas follows:

#用户中心服务端口
server:
  port: 8080
#数据源配置
spring:
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: "jdbc:mysql://localhost:3306/dianshang-user"
      username: root
      password: 111111
#dubbo配置
dubbo:
  scan:
    # 包名根据自己的实际情况写
    base-packages: org.project.dianshang.user
  protocol:
    port: 20880
    name: dubbo
  registry:
    #zookeeper注册中心地址
    address: zookeeper://192.168.0.107:2181

5.3.2, write service exposure interface and implementation class

In the dianshang-user-apimodule, create an UserApiinterface and return the parameter object UserVo!

public interface UserApi {

    /**
     * 查询用户信息
     * @param userId
     * @return
     */
    UserVo findUserById(String userId);
}

Among them UserVo, serialization is required, as follows:

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class UserVo implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 用户ID
     */
    private String userId;

    /**
     * 用户中文名
     */
    private String userName;
}

In the dianshang-user-providermodule, write the UserApiinterface implementation class as follows:

@Service(interfaceClass =UserApi.class)
@Component
public class UserProvider implements UserApi {

    @Autowired
    private UserService userService;

    @Override
    public UserVo findUserById(String userId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("user_id",userId);
        User source = userService.getOne(queryWrapper);
        if(source != null){
            UserVo vo = new UserVo();
            BeanUtils.copyProperties(source,vo);
            return vo;
        }
        return null;
    }
}

 

To be continued. . . .

Guess you like

Origin blog.csdn.net/suifeng629/article/details/107134562