Dubbo overview

What is a distributed system?

  • Distributed System Principles and Paradigms:
    • “A distributed system is a collection of independent computers that act like a single related system to the user.”
    • A distributed system is a software system built on a network.
    • Simply put: Multiple people working together to do one thing!
    • Any server can not meet the data throughput of Taobao double 11, must be a lot of servers to complete the common.
  • Xiehou: “two heads are better than zhuge liang”, is the true portrayal of distributed system

Single application Architecture

  • When the website traffic is very small, only one application is needed, and all the functions are deployed together (all the business is placed in one Tomcat), thus reducing the deployment nodes and costs;
  • At this point, the data access framework (ORM) is the key to simplify the workload of adding, deleting, modifying and reviewing.
  • For example: a supermarket’s cashier system, a company’s staff management system

ORM: Object Relational Mapping

  • advantages
    • Small projects develop quickly and cost less
    • Architecture is simple
    • Easy to test
    • Easy to deploy
  • disadvantages
    • Large projects with serious module coupling are difficult to develop and maintain and high communication costs
    • New Business difficulties
    • Core business and marginal business mixed together, problems affect each other

Vertical application Architecture

  • When the traffic gradually increased, a single application to increase the acceleration of the machine is getting smaller and smaller, the application will be divided into several mutually unrelated several applications, in order to improve efficiency;
  • According to the MVC hierarchical model, the large module is divided into several unrelated small modules, and each small module has an independent server
  • At this point, a Web framework (MVC) for accelerating front-end page development is key; Because each widget has its own page

MVC: Model View Controller

  • Disadvantages:
    • Modules can not be completely without intersection, common modules can not be reused, development waste

Distributed Service Architecture

  • When more and more vertical applications, the interaction between applications is inevitable, the core business is extracted as an independent business, gradually forming a robust service center, so that the front-end application can respond to the changing market demand more quickly;
  • At this time, users improve service reuse and integrated distributed service framework (RPC) remote call is the key;

RPC: Calls between independent application servers depend on The Romote Procedure Call (RPC)

  • Logistics services are not busy, with 100 servers; Goods and services are particularly busy, also 100 servers;
    • How to optimize resource allocation? left

Mobile Computing Architecture

  • When there are more and more services, problems such as capacity evaluation and waste of small service resources gradually appear. In this case, a scheduling center should be added to manage cluster capacity in real time based on access pressure to improve cluster utilization.
  • In this case, the resource scheduling and Governance Center (SOA) for increasing machine utilization is key;

SOA: Service-oriented Architecture, simply understood as “Service governance”, for example: “dispatcher” at bus station

Introduction of Dubbo

  • Dubbo is a distributed service framework, which is an open source project of Alibaba and is being maintained by Apache
  • Dubbo focuses on RPC remote service invocation solutions for improved performance and transparency, as well as SOA service governance solutions
  • Simply put, Dubbo is a service framework that is not needed without a distributed requirement

RPC

  • Remote Procedure Call (RPC) refers to Remote Procedure Call, which is a form of interprocess communication
  • Basic communication principles of RPC
    1. Serialize the object on the client side
    2. The underlying communication framework uses NetTY (SOCKET based on TCP protocol) to send serialized objects to the service provider
    3. After the service provider obtains the data file through the socket, it deserializes and obtains the object to be operated on
    4. After the object data operation is complete, the new object is serialized and then returned to the client through the socket of the service provider
    5. The client retrieves the serialized data, deserializes it, and returns the latest data object, at which point the request is completed

  • RPC two core modules: communication (Socket), serialization.

The node role

node The role that
Provider Service Provider (Bath Centre)
Consumer Consumer of service (guest)
Registry Service registration and discovery Registry (Convenience service Center, all restaurants and entertainment venues are registered in this center)
Monitor Statistics center that monitors services (counts the number of times a service is invoked)
Container Service operation container (barbecue street, bath street)

Call relationship

1. The service container is responsible for starting, loading, and running the service provider; 2. Service providers register their services with the registry at startup; 3. At startup, service consumers subscribe to the registry for the services they need; 4. Return the service provider address list to consumers in the registry. If there is any change, the registry will push the change data to consumers based on the long connection; 5. The service consumer selects one provider from the provider address list to call based on the soft load balancing algorithm. If the call fails, it selects another one to call. 6. Service consumers and providers accumulate call times and call time in memory, and regularly send statistical data to the monitoring center once a minute;

Quick start

dubbo.apache.org/

The registry

Zookeeper

  • The ZooKeeper registry is officially recommended.
  • The registry is responsible for the registration and search of service addresses, equivalent to directory services;
  • Service providers and consumers interact with the registry only at startup, without forwarding requests during registration, which is less stressful;
  • Zookeeper is a subproject of Apache Hadoop. It is a tree-shaped directory service that supports change push. It is suitable for dubbo service registry with high industrial intensity and can be used in production environment.

Dubbo is both a job seeker and a recruiter, while ZooKeeper is a job market/recruitment site.

The installation

2. Copy apache-Zookeeper-3.6.0-bin.tar. gz to the opt directory. 3

[root@localhost opt]# tar -zxvf apache-zookeeper-3.6.0-bin.tar.gz
Copy the code

4. Rename

[root@localhost opt]# mv apache-zookeeper-3.6.0-bin zookeeper
Copy the code

5. Create zkData and zkLog directories under /opt/zookeeper/

[root@localhost zookeeper]# mkdir zkData
[root@localhost zookeeper]# mkdir zkLog
Copy the code

6. Go to /opt/zookeeper/conf, copy the zoo_sample. CFG file, and name it zoo.cfg

[root@localhost conf]# cp zoo_sample.cfg zoo.cfg
Copy the code

7, edit zoo. CFG file, modify dataDir path:

dataDir=/opt/zookeeper/zkData
dataLogDir=/opt/zookeeper/zkLog
Copy the code

8. Start Zookeeper

[root@localhost bin]# ./zkServer.sh start
Copy the code

9. Check the status:

[root@localhost bin]# ./zkServer.sh status
Copy the code

Service Provider

An empty Maven project. 2. Provide a service interface

Pom.xml for the server

Please strictly follow the following versions for all dependencies

<packaging>war</packaging>

<properties>
    <spring.version>5.0.6. RELEASE</spring.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <! --dubbo -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.5.7</version>
    </dependency>
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.6</version>
    </dependency>
    <dependency>
        <groupId>com.github.sgroschupf</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.1</version>
    </dependency>
    <dependency>
        <groupId>javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.11.0. GA</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven </groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <configuration>
                <port>8001</port>
                <path>/</path>
            </configuration>
            <executions>
                <execution>
                    <! When the package is complete, run the service
                    <phase>package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Copy the code

Service side interface

public interface HelloService {
    String sayHello(String name);
}
Copy the code

Service side implementation

@com.alibaba.dubbo.config.annotation.Service
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String name) {
        return "Hello," + name + "!!!"; }}Copy the code

The server configuration file spring.xml


      
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <! --1. Alias of the service provider in ZooKeeper -->
    <dubbo:application name="dubbo-server"/>
    <! --2. Registry address -->
    <dubbo:registry address=Zookeeper: / / 192.168.204.141: "2181"/>
    <! --3. Scan classes (what package classes as service provider classes) -->
    <dubbo:annotation package="service.impl"/>
</beans>
Copy the code

Web. XML for the server


      
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         id="WebApp_ID" version="3.1">
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/spring.xml</param-value>
    </context-param>
</web-app>
Copy the code

Service consumer

Pom.xml for the consumer

Change the tomcat port to 8002

The consumer’s Controller

@RestController
public class HelloAction {
    @com.alibaba.dubbo.config.annotation.Reference
    private HelloService hs;
    @RequestMapping("hello")
    @ResponseBody
    public String hello( String name){
        returnhs.sayHello(name); }}Copy the code

Consumer interface

Note: The Controller relies on HelloService, so we create an interface; This is the consumer, and we don’t need the implementation because the implementation will let the service take care of it for us!

public interface HelloService {
    String sayHello(String name);
}
Copy the code

The consumer’s web.xml


      
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         id="WebApp_ID" version="3.1">

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
Copy the code

The consumer’s SpringMVC.xml


      
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <! --Dubbo application name, usually using project name -->
    <dubbo:application name="dubbo-consumer" />
    <! -- Set Dubbo registry address -->
    <dubbo:registry address=Zookeeper: / / 192.168.204.141: "2181" />
    <! Configure the Dubbo scan class and publish it as a service -->
    <dubbo:annotation package="controller" />
</beans>
Copy the code

Start service tests

Start the service side first, then start the consumer side. Visit: http://localhost:8002/hello? name=james

The monitoring center

As we develop, we need to know which services are registered in the registry so that we can develop and test them. Graphically displaying the list of services in the registry can be achieved by deploying a Web application version of the administrator.

Service management side

Installing the management Terminal

  1. Unpack the dubbo – admin – master. Zip
  2. Modifying a Configuration File

  1. Go back to the project root directory and package using Maven: MVN Clean Package

4. Run the jar file in the target directory java-jar dubo-admin-0.0.1 – snapshot.jar in DOS

5. Open the browser and enter:http://localhost:7001/ ;

You need to log in to the system for the first time. The password is root

Management end Use

  1. Start the server and register the service with ZooKeeper

  1. After starting the Dubo-server server side, refresh the management side and the service is registered successfully, but without consumers

  1. Click on the service name to enter the service provider page

  1. Run the consumer as well, refresh the service and display normal

  1. Viewing consumers

Monitoring and Statistics Center

Monitor: Statistics center that records how many times the service is called, etc

  1. Extract the dubbo – monitor – simple – 2.5.3. Zip
  2. Modify the dubbo – monitor – simple – 2.5.3 \ conf \ dubbo properties

  1. Double-click to run dubbo-monitor-simple-2.5.3\bin\start.bat
  2. Modify spring. XML for Dubbo-server and Dubbo-Consumer to add the following tags
<! <dubbo:monitor protocol="registry"/>Copy the code

Comprehensive practical

Configuration instructions

Check on startup

  • At startup, the registry checks whether dependent services are available and throws an exception if they are not
  • On the consumer side, write the main method to initialize the container (Tomcat startup mode, you must visit the action once to initialize spring).
public class Test {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring.xml"); System.in.read(); }}Copy the code
<! -- The default is true: throw an exception; <dubbo:consumer check="false" />Copy the code
  • Level of system logs, need with log4j output, add log4j under resources. The properties, contents are as follows:
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{yyyy-MM-dd  HH:mm:ss} %m%n log4j.appender.file=org.apache.log4j.FileAppender log4j.appender.file.File=dubbo.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %m%n log4j.rootLogger=error, stdout,fileCopy the code

timeout

  • An uncertain blocking state (timeout) during a call due to an unreliable network or server
  • The timeout period must be set to prevent client resources (threads) from being suspended due to timeouts
  • Add the following configuration to the service provider:
<! -- Set timeout to 2 seconds, default to 1 second -->
<dubbo:provider timeout="2000"/>
Copy the code
  • To test this, add simulated network latency to the service implementation helloServiceImp.java:
@Service
public class HelloServiceImpl implements HelloService {
    public String sayHello(String name) {
        try {
            Thread.sleep(3000);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "Hello,"+name+"!!!!!!!!"; }}Copy the code
  • The timeout setting is 2 seconds, and the simulated network delay is 3 seconds, exceeding the time limit, error!
  • Configuration principles:
    • Dubbo recommends configuring as many Consumer attributes on the Provider as possible:
    1. The provider of the service knows better than the consumer of the service performance parameters, such as the timeout of the invocation, the reasonable number of retries, and so on
    2. After the Provider is configured, if the Consumer is not configured, the Consumer uses the Provider configuration value. That is, the Provider configuration can be used as the default value for the Consumer.

Retry count

  • When a failure occurs, the system automatically switches and retries to other servers. The default value for dubbo retries is 2, which is optional
  • Configure on the provider side:
<! -- try again 3 times, total 4 times -->
<dubbo:provider timeout="2000" retries="3"/>
Copy the code
@Service
public class HelloServiceImpl implements HelloService {
    public String sayHello(String name) {
        System.out.println("============= is called once ===============");
        try {
            Thread.sleep(3000);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "Hello,"+name+"!!!!!!!!"; }}Copy the code
  • Not all methods are suitable for setting retry times
    • Idempotent methods: suitable (when the arguments are the same, the results are the same no matter how many times they are executed, e.g., query, modify)
    • Non-idempotent method: not suitable (when the parameters are the same, the execution result is different, e.g. delete, add)
  • Set a method separately
    1. The provider interface adds the sayNo() method and implements it
    public interface HelloService {
        String sayHello( String name );
        String sayNo(a);
    }
    Copy the code
    @Service
    public class HelloServiceImpl implements HelloService {
        public String sayHello(String name) {
        System.out.println("=============hello called once ===============");
        try {
            Thread.sleep(3000);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "Hello,"+name+"!!!!!!!!";
        }
    
        public String sayNo(a) {
            System.out.println("-------no is called once -------");
            return "no!"; }}Copy the code
    1. Add the sayNo() method declaration to the consumer interface
    public interface HelloService {
        String sayHello( String name );
        String sayNo(a);
    }
    Copy the code
    1. Consumer controller
    @Controller
    public class HelloAction {
    
        // @reference This annotation has been replaced by 
            
              in the XML file, so it can be automatically injected
            
        @Autowired
        private HelloService helloService;
    
        @GetMapping("hello")
        @ResponseBody
        public String sayHi(String name){
            return helloService.sayHello(name);
        }
    
        @GetMapping("no")
        @ResponseBody
        public String no(a){
            returnhelloService.sayNo(); }}Copy the code
    1. The consumer configures the number of method retries
    <dubbo:reference interface="service.HelloService" id="helloService">
        <dubbo:method name="sayHello" retries="3"/>
        <dubbo:method name="sayNo" retries="0"/> <! -- Do not retry -->
    </dubbo:reference>
    Copy the code

Many versions

  • An interface, with multiple implementation classes, can be introduced by defining versions
  • Define two implementation classes for the HelloService interface, and the provider modifies the configuration:
<dubbo:service interface="service.HelloService"
class="service.impl.HelloServiceImpl01" version="1.0.0"/>
<dubbo:service interface="service.HelloService"
class="service.impl.HelloServiceImpl02" version="2.0.0"/>
Copy the code
  • Consumers can then select a specific service version based on the version of Version
<dubbo:reference interface="service.HelloService" id="helloService" version="2.0.0">
    <dubbo:method name="sayHello" retries="3"/>
    <dubbo:method name="sayNo" retries="0"/>
</dubbo:reference>
Copy the code
  • Note: The consumer control layer needs to be auto-injected because the @Reference annotation conflicts with dubbo: Reference
@Controller
public class HelloAction {
    @Autowired
    private HelloService helloService;
}
Copy the code
  • When the consumer’s version is changed to version=”*”, the service provider’s version is randomly invoked
1.0 is called -- -- -- -- -- -- -- a -- -- -- -- -- -- -- -- -- -- -- -- -- -- 2.0 is called a -- -- -- -- -- -- -- -- -- -- -- -- -- -- 1.0 is called a -- -- -- -- -- -- -- -- -- -- -- -- -- -- 1.0 is called a -- -- -- -- -- -- -- -- -- -- -- -- -- -- 1.0 is called -- -- -- -- -- -- -- at a time -------2.0 is called once -------Copy the code

Local stub

  • A serious problem with our current distributed architecture is that all operations are consumer-initiated and performed by service providers
  • Consumers talk all the time but do nothing, which will make the provider very tired. For example, the consumer is fully competent in simple parameter verification, and sends the legitimate parameters to the provider for execution, which will be more efficient and the provider will not be so tired
  • For example: go to the real estate bureau for housing transfer, please bring your own documents and information, if nothing with, then the transfer procedures will be very troublesome, you have to investigate what you have loans, there is no mortgage, real estate certificate is not yourself, copy information and other operations. It’s gonna take more than a day. Come again tomorrow. If you can get these things ready and transfer them in advance, an hour is enough, which is “why real estate agents are so efficient.”
  • Without further ado, the process of processing some business logic in the consumer and then invoking the provider is called “local stubs”
  • The code implementation must be in the consumer, creating a HelloServiceStub class and implementing the HelloService interface
  • Note: you must use constructor injection
public class HelloServiceStub implements HelloService {

    private HelloService helloService;
        / / HelloService
        public HelloServiceStub(HelloService helloService) {
        this.helloService = helloService;
    }

    public String sayHello(String name) {
        System.out.println("Local stub data validation...");
        if(! StringUtils.isEmpty(name)){return helloService.sayHello(name);
        }
        return "i am sorry!";
    }

    public String sayNo(a) {
        returnhelloService.sayNo(); }}Copy the code
  • Modify consumer configuration:
<dubbo:reference interface="service.HelloService" id="helloService" version="1.0.0" stub="service.impl.HelloServiceStub">
    <dubbo:method name="sayHello" retries="3"/>
    <dubbo:method name="sayNo" retries="0"/>
</dubbo:reference>
Copy the code

Load Balancing Policy

  • Load balancing, in fact, is to spread the request to multiple operation units for execution, so as to jointly complete the work task.
  • Simply put, a lot of servers, not always let a server work, should “rain and dew”
  • Dubbo provides a total of four policies, the default is random random allocation call

  • Modify the provider configuration and start three providers for consumer access

    • Tomcat port 8001 8002 8003
    • Provider port 20881,20882,20883
    <dubbo:provider timeout="2000" retries="3" port="20881"/>
    Copy the code
    • Class HelloServiceImpl01, server 1, server 2, server 3
    public String sayNo(a) {
        System.out.println("---- server 1-- 1.0 is called once -------");
        return "no!";
    }
    Copy the code
    • Start consumer to test
  • The consumer modifies the weight

<dubbo:reference loadbalance="roundrobin" interface="service.HelloService" id="helloService" version="2.0.0" stub="stub.HelloServiceStub">
    <dubbo:method name="sayHello" retries="3"/>
    <dubbo:method name="sayNo" retries="0"/>
</dubbo:reference>
Copy the code
  • It is best to use the management side to change the weights

High availability

Zookeeper downtime

  • The ZooKeeper registry is down and the services exposed by Dubbo can be consumed
    • The breakdown of the monitoring center does not affect the use of the system, but only the loss of some sample data
    • After the database goes down, the registry can still provide a list of services query through the cache, but it cannot register new services
    • If one of the registry peer clusters fails, it will automatically switch to the other one
    • After all registries go down, service providers and service consumers can still communicate through local caches
    • The service provider is stateless. If any service provider breaks down, the service is not affected
    • When all service providers go down, the service consumer application becomes unavailable and reconnects indefinitely waiting for the service provider to recover
  • Testing:
    • Make a request normally
    • Run the./ zkserver. sh stop command to stop ZooKeeper
    • Consumers can still spend normally

Service degradation

  • Geckos lose their tails automatically when they are in danger, in order to lose unimportant things and keep the important ones
  • Service degradation means that some services are stopped or handled in a simple way according to the actual situation and traffic. In this way, server resources are released to ensure the normal running of core services

Why service downgrades

  • And why use service degradation, which prevents an avalanche of distributed services
  • What is an avalanche? Is the butterfly effect, when a request timeout happened, always waiting for service response, so under the condition of high concurrency, many request because it has been waiting for the response, until the service outage resource depletion, and after the outage can cause distributed other service calls the outage of services also can appear resources exhausted outage, it will cause the entire distributed services are paralyzed, This is an avalanche.

Service degradation implementation

  • Configure service degradation in the administrative console: Masking and fault tolerance
  • Mock =force:return+ NULL Mock =force:return+null Used to mask the impact on the caller when the unimportant service is unavailable.
  • Mock =fail:return+ NULL Mock =fail:return+null: Mock =fail:return+null To tolerate the impact on the caller when the unimportant service is unstable.

Integrate MyBatis to achieve user registration

Initializing the database

CREATE DATABASE smd
USE smd
CREATE TABLE users(
    uid INT(11) AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL.PASSWORD VARCHAR(50) NOT NULL,
    phone VARCHAR(50) NOT NULL,
    createtime VARCHAR(50) NOT NULL
)
Copy the code

Create aggregate projects – Project modularization

  • Lagou-dubbo (Project Directory)
  • Lagou-dubbo-parent (Parent project, aggregation project: dependency version for defining all modules)
<modelVersion>4.0.0</modelVersion>

<groupId>com.sunguoan</groupId>
<artifactId>sun-parent</artifactId>
<version>1.0 the SNAPSHOT</version>

<packaging>pom</packaging>

<properties>
    <spring.version>5.0.6. RELEASE</spring.version>
</properties>

<dependencies>
    <! -- JSP related -->
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <scope>provided</scope>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jsp-api</artifactId>
        <scope>provided</scope>
        <version>2.0</version>
    </dependency>
    <! -- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <! -- Mybatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.2.8</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.2.2</version>
    </dependency>
    <! -- Connection pool -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.0.9</version>
    </dependency>
    <! -- database -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.32</version>
    </dependency>
    <! --dubbo -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.5.7</version>
    </dependency>
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.6</version>
    </dependency>
    <dependency>
        <groupId>com.github.sgroschupf</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.1</version>
    </dependency>
    <dependency>
        <groupId>javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.11.0. GA</version>
    </dependency>
    <! -- fastjson -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.47</version>
    </dependency>
    <! -- junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>
Copy the code
  • Lagou-dubo-entity (Solid Project, JAR project)
  • Lagou-dubo-dao (Data Access Layer Project, JAR Project)
  • Lagou-dubo-interface (Service Interface Definition Project, JAR Project)
  • Lagou-dubo-service (Privoder Service Provider Project, JAR Project)
<parent>
<groupId>com.sunguoan</groupId>
<artifactId>sun-parent</artifactId>
<version>1.0 the SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>sun-service</artifactId>
<packaging>war</packaging>
<dependencies>
    <dependency>
        <groupId>com.sunguoan</groupId>
        <artifactId>sun-interface</artifactId>
        <version>1.0 the SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.sunguoan</groupId>
        <artifactId>sun-dao</artifactId>
        <version>1.0 the SNAPSHOT</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <configuration>
                <port>8001</port>
                <path>/</path>
            </configuration>
            <executions>
                <execution>
                    <! When the package is complete, run the service
                    <phase>package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Copy the code
  • Lagou-dubbo – Web (Consumer Services Consumer Engineering, WAR Project)
<! --> < span style = "max-width: 100%;
<filter>
    <filter-name>charset</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>utf-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>charset</filter-name>
    <url-pattern>/ *</url-pattern>
</filter-mapping>

<servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/spring-mvc.xml</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
Copy the code

Start the test

  1. First select the parent project (aggregation project) for full installation into jar
  2. Start the service service
  3. Launch the caller Web