What dubbo is: Dubbo is an open source distributed service framework developed by Alibaba. It is dedicated to providing high-performance and transparent RPC remote service invocation solutions. Dubbo is the core framework of alibaba Group’s member sites, supporting more than 3,000,000,000 visits per day to more than 2,000 services. Dubbo is an optimization at the application level in a system. I will share the optimization at the database level in the system later. Here we will only study Dubbo

What dubbo solves: As websites get bigger, it’s inevitable to split applications and servitize them to improve system performance and development efficiency. And it requires that each service to be split can flexibly configure its cluster quantity and control the granularity of service according to needs (the granularity of service means the number of services released in the service system I run).

Dubbo’s advantages: 1, simple to use 2, deployment light 3, convenient secondary development of Dubbo these are based on the framework of secondary development

Before we look at Dubbo, let’s look at distributed applications

Distributed applications: Applications are distributed on different computers to perform a task together over a network. Usually in server/client mode. Server /client can be divided into two ends (Server /client), three ends (Server /middleware/client), and N ends (Multiple server/ Multiple minddle/ Multiple Client). That is to say, the distributed application needs to split the service and deploy it in a Web server separately. The controller calls the service through the network, which forms the mode of client and server. The client is only responsible for running the Controller code and processing data requests and responses. The server is only responsible for business processing, data calculation, etc. In addition, a system needs to process both user requests and responses as well as business processes, so the operating pressure will be relatively high, thus affecting the performance of the entire system. Now the system is split in two, which greatly improves the overall performance of the system

As mentioned above, distributed applications are divided into client and server, which run in their own servers respectively. The controller of the client invokes the service of the server, not in an application, but through network transmission. This technique of making a controller call a remote service over a network is called RPC.

Remote Procedure Call Protocol (RPC) — Remote Procedure Call Protocol (RPC), which is a principle of requesting service RPC from Remote computer programs over the network: Dubbo is the serialization of objects, deserialization and transfer of serialized data, DUbbo (using binary files,TCP) Common RPC framework in the Java field: Distributed technology in the Java field or distributed framework called RPC framework are: Dubbo, ZeroICE

What are the advantages of using distributed patterns? 1. In terms of development, it is divided into multiple systems, which is conducive to the allocation of development tasks, and each is responsible for its own system development. 2. In operation, load pressure is distributed to multiple servers to improve the operating performance of the whole system. 3. As the distributed development mode is to publish the business or function in the form of service, it can improve the service reuse rate

Taking advantage of dubbo? Splitting applications for servization improves system performance and development efficiency. In addition, the number of service clusters can be flexibly configured and services can be controlled

The functions of the registry: 1, the registration of server services and the call of client services 2, improve the availability of the system 3, improve the system scalability 4, centralized management of services

Zookeeper: ZooKeeper is a distributed, open source distributed application coordination service that is an important component of Hadoop and Hbase. It provides consistency services for distributed applications, including configuration and maintenance, domain name service, distributed synchronization, and group service.

Although there are many technologies that can be used to make a registry for Dubbo applications, we used ZooKeeper, because it is officially recommended by Dubbo. In Springboot-Dubbo, ZooKeeper is also integrated by default. In addition, ZooKeeper is an important component of Hadoop and HBase. If the project needs to develop big data services in the future, it can seamlessly access the technology of big data. So we use ZooKeeper as the registry for a distributed system based on the Dubbo framework.

CFG file zoo_sample. CFG is changed to zoo. CFG (you can copy the file and then modify it) DataDir = F: / / zookeeper – 3.4.5 / / data

Address for Storing Logs

DataLogDir = F: / / zookeeper 3.4.5 / / logCopy the code

3. Double-click zkServer. CMD in the bin directory and run ZooKeeper (if the directory is Mac, go to the bin directory and run sh zkserver. sh start).

Start by writing a Hello World program using XML

The tool I use here is idea creates a parent project (dubbo_parent) and then creates three modules

1. Create three projects

One for the server, one for the client, and one for the interface Server: server Client: client Interface: API then we modify the pom.xml configuration. The API project only provides that the interface does not need to inherit the dubbo_parent dependency, so we uninherit the configuration in the POM.xml API. At the same time, we need API interfaces in the server and client, so we need to import API dependencies in the server and client projects

<dependencies>
  <dependency>
     <groupId>com.jd.dubbo</groupId>
     <artifactId>api</artifactId>
  </dependency>
</dependencies>
Copy the code

Add springBoot dependencies and import Dubbo dependencies (added in pom.xml of dubbo_parent project)

Springboot parent project: spring-boot-starter-parent SpringbootWeb Related project spring-boot-starter-web

<! --> <parent> <groupId>org.springframework. Boot </groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <dependencies> <! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <! Alibaba </groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> <! -- Zookeeper --> <dependency> <groupId>com.101tec</groupId> <artifactId> zkClient </artifactId> <version>0.2</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> The < version > 3.4.5 < / version > < / dependency > < / dependencies >Copy the code
3. Write interface programs

Implement the Serializable interface

4. Write server-side programs (interface implementation classes) and create startup classes

Create a startup class for the server

@SpringBootApplication
@ImportResource("classpath:dubbo-server.xml") public class StartApp { public static void main(String[] args) { SpringApplication.run(StartApp.class,args); }}Copy the code
5. Write server-side XML configuration (dubo-service.xml)
<! Hand over the concrete service implementation classes to the Spring container for management --> <bean id="helloWorld" class="com.jd.dubbo.service.impl.HelloWorldServiceImpl"/ > <! <dubbo:application name="dubbo-server"/ > <! <dubbo:protocol name="dubbo" port="20880"/ > <! -- Set the register address to zookeeper and register propertiestrueIf fortrue, you need to register the service address of the published service with ZooKeeper iffalse, there is no need to register the service address with ZooKeeper --> <! --<dubbo:registry address=Zookeeper: / / "127.0.0.1:2181" register="false"/>-->

    <dubbo:registry address=Zookeeper: / / "127.0.0.1:2181" register="true" file="${user.home}/dubbo.cache"/ > <! <dubbo:service interface="com.jd.dubbo.service.IHelloWorldService" ref="helloWorld"/>
Copy the code

6. Write the client XML configuration
<! <dubbo:application name="dubbo-client"/ > <! <dubbo:registry address=Zookeeper: / / "127.0.0.1:2181"/ > <! <dubbo: Reference interface="com.jd.dubbo.service.IHelloWorldService" id="helloWorldService"/>
Copy the code

7. Write the client program (startup class)
@SpringBootApplication
@ImportResource("classpath:dubbo-client.xml")
@Controller
public class StartApp {

    @Autowired
    private IHelloWorldService helloWorldService;
    public static  void main (String[] args){
        SpringApplication.run(StartApp.class,args);
    }

    @RequestMapping("/sayHello")
    @ResponseBody
    public String sayHello(){
        HelloWorld helloWorld =helloWorldService.sayHello(new HelloWorld());
            returnhelloWorld.getName(); }}Copy the code

At this point we are done with XML configuration. Let’s start the server first and then the client

Build e-commerce architecture (based on springBoot full annotations)

1, SpringBoot provides us with a complete set of development packages, import a dependency can use it to provide us with the corresponding functionality, Improve our development efficiency; 2. Reduce the number of dependency packages imported by our application; 3. Eliminate tedious XML configuration files; 4

Development steps:

I. Create each project

Shop_parent shop_API_goods (API) shop_API_ORDER (API) shop_server_goods (API) Shop_client_mgrsite (background management system) Shop_Clinet_website (mobile terminal interface)

Create a parent project and then create other modules, adding the following dependencies to shop_parent’s POM.xml

<! --> <parent> <groupId>org.springframework. Boot </groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <dependencies> <! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <! --> <dependency> <groupId> io.dubo. springboot</groupId> The < artifactId > spring - the boot - starter - dubbo < / artifactId > < version > 1.0.0 < / version > <! -- Remove netty from Dubbo > <exclusions> <artifactId>netty</artifactId> <groupId>org.jboss.netty</groupId> </exclusion> </exclusions> </dependency> </dependencies> <! <dependencies> <dependency> <dependencies> <groupId>com.jd.shop</group > <artifactId> shop_API_goods </artifactId> <version>1.0</version> </dependency> <dependency> <groupId>com.jd.shop</groupId> The < artifactId > shop_api_order < / artifactId > < version > 1.0 < / version > < / dependency > <! Alibaba </groupId> <artifactId>druid</artifactId> <version>1.0.25</version> </dependency> <! <dependency> <groupId> org.mybtes.spring. Boot </groupId> < artifactId > mybatis - spring - the boot - starter < / artifactId > < version > 1.2.0 < / version > < / dependency > < the dependency > < the groupId > org. Projectlombok < / groupId > < artifactId > lombok < / artifactId > < version > 1.16.18 < / version > < / dependency > </dependencies> </dependencyManagement> <build> <plugins> <! <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>Copy the code

Add a springboot-Web dependency. Add a dependency for connecting to the database. Add a dependency for dubbo

Modified pom. XML

In the shop_API_goods module, shop_server_goods module does not need to inherit shop_parent module, so it needs to be modified in their POM.xml. Add dependencies in shop_server_goods (goods system) and shop_server_ORDER (order system), respectively,

<! --shop_server_goods--> <dependency> <groupId>com.jd.shop</groupId> <artifactId>shop_api_goods</artifactId> </dependency>Copy the code
<! --shop_server_order--> <dependency> <groupId>com.jd.shop</groupId> <artifactId>shop_api_order</artifactId> </dependency>Copy the code
<! --shop_server_order--> <! --shop_server_order--> <dependencies> <! --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <! Alibaba </groupId> <artifactId>druid</artifactId> </dependency> <! <dependency> <groupId> org.mybtes.spring. Boot </groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> The < version > 1.3.2 < / version > < configuration > < verbose >true</verbose>
        <overwrite>false</overwrite> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> </dependencies> </dependencies> </plugin> </plugins> </build>Copy the code

Create two server startup classes StartApp shop_server_goods and shop_server_ORDER

@SpringBootApplication
@EnableTransactionManagement
@MapperScan("com.jd.server.goods.mapper") public class StartApp { public static void main(String[] args) { SpringApplication.run(StartApp.class,args); }}Copy the code

Iii. Mybatis shall be integrated into each service system

1. Add the following configuration information to the application.properties configuration file :(the two services need to be partially modified, which is marked in () below)

server.port=9090(server.port=9091)
Database connection address
spring.datasource.url=jdbc:mysql://localhost:3306/jd_shop
# MySQL driver
spring.datasource.driverClassName=com.mysql.jdbc.Driver
# database account
spring.datasource.username=root
# database password
spring.datasource.password=123456
# Alibaba Druid connection pool
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource 
Mybatis mapper.xml configuration file path
mybatis.mapperLocations=classpath:mapper/*.xml
#mybatis mapper.xml configuration file domain object alias scan path
mybatis.typeAliasesPackage=com.jd.api.goods.domain    
(mybatis.typeAliasesPackage=com.jd.api.order.domain   )
Copy the code

2, add the following annotation to the startup class :(the above startup class code has been added)

// Scan mybatis interface annotation @mapperscan ("com.jd.server.goods.mapper") / / @ EnableTransactionManagement open transaction managementCopy the code

Iv. Each service system integrates mybatis- Generator plug-in

1, Add mybatis-generator plugin to poM file of each service system (already added in pom. XML) 2, add Generatorconfig. XML file to resources directory (appropriate modification for both service systems)

V. Each service system shall integrate dubbo to release service procedures

1. Add the following configuration information to the application.properties configuration file:

# integration dubbo
Set the service name of the application
spring.dubbo.application.name=shop-server-goods
(spring.dubbo.application.name=shop-server-order)
Set the RPC protocol
spring.dubbo.protocol.name=dubbo
# Set the RPC port number
spring.dubbo.protocol.port=20880
Set the address of the registrySpring. Dubbo. Registry. Address = zookeeper: / / 127.0.0.1:2181Set the path to publish service annotation scan
spring.dubbo.scan=com.jd.server.goods.service.impl
(spring.dubbo.scan=com.jd.server.order.service.impl)

Copy the code

Vi. Publish a service (take publishing goods and services as an example)

Mybatis – Genenrator plugin to generate domain class, mapper interface, mapper. XML file 2. Each domain class should be in the corresponding API project 3

Attach the @service annotation to the implementation class. This annotation means that the class needs to publish the Service

Note: 1, the annotations are com. Alibaba. Dubbo. Config. The annotation package under the annotation is not spring annotations 2, labeled the annotation service implementation class must be in the above configuration items spring. The dubbo, able to scan to scan = XXX path

Vii. Each client integrates dubbo reference service program

1. Add the following configuration information to the application.properties configuration file:

 # dubbo application name
spring.dubbo.application.name=shop-client-mgrsite
Connect to the registry addressSpring. Dubbo. Registry. Address = zookeeper: / / 127.0.0.1:2181# reference the path of the service scan
spring.dubbo.scan=com.jd.client.mgrsite.controller
Copy the code

8. Reference service (take commodity Controller as an example)

Put a @refrence annotation on the dependency of the controller that created the commodity that calls the service that invokes the commodity. This annotation will automatically inject the service that needs to be referenced. Note: The annotated service dependency must be in a path that can be scanned by the configuration item spring.dubbo.scan= XXX above

@Controller
public class ProductController {

    @Reference
    IProductService productService;
    @RequestMapping("/getAllProduct")
    @ResponseBody
    public List<Product> getAllProduct() {
        returnproductService.getAllProduct(); }}Copy the code