当前位置:网站首页>Distributed service framework dobbo

Distributed service framework dobbo

2022-07-06 03:07:00 Snowy solitary boat

Dubbo

Dubbo The first is Alibaba Open source distributed service framework , Its biggest characteristic is to construct in a hierarchical way , In this way, the layers can be decoupled ( Or loosen the coupling as much as possible ). From the perspective of the service model , Dubbo It's a very simple model , Either the provider provides the services , Or consumer services , the Based on this, service providers can be abstracted (Provider) And service consumers (Consumer) Two roles .

Service architecture role

  • Container: Service running framework ,dubbo Based solely on spring Realization
  • Provider: Provide remote call service
  • Registry: Service registration and Publishing Center
  • Consumer: Execute remote call service
  • Monitor: Monitoring Center for counting the number and time of service calls

Service startup process

  1. start-up spring Containers Container When the Provider start-up
  2. And then Provider Register relevant information to Registry in
  3. Consumer from Registry Subscription in Provider Information about
  4. Registry take Provider Send your message to Consumer
  5. Consumer according to Registry Notification information call Provider The method in
  6. Consumer and Provider Asynchronously send the call times information to Monitor Statistics

Dubbo Supported protocols

Dubbo agreement

  • advantage : use NIO Multiplexing a single long connection , And use thread pool to process requests concurrently , Reduce handshake and increase concurrent efficiency , Good performance ( Recommended )
  • shortcoming : When uploading large files , There may be problems ( Don't use Dubbo Upload files )

RMI(Remote Method Invocation) agreement

  • advantage :JDK Self contained
  • shortcoming : Occasionally the connection fails

Hessian agreement

advantage : But with the original Hessian interoperability , be based on HTTP agreement
shortcoming : Need to be hessian.jar Support ,http Short connections are expensive

Dubbo Supported registries

ZooKeeper

  • advantage : Support distributed , There are many peripheral products
  • shortcoming : Limited by ZooKeeper Software stability

Multicast

  • advantage : De centralization , There is no need to install the software separately
  • shortcoming :Provider、Consumer、Registry Do not cross the machine room ( route )

Redis

  • advantage : Support clusters , High performance
  • shortcoming : Require server time synchronization , Otherwise, cluster failure may occur

Simple

  • advantage : standard RPC service , No compatibility issues
  • shortcoming : No clustering support

Dubbo application

Application structure

  • parent: Logic Maven engineering , stay pom Introduce the required dependencies into the file
  • api: Inherit parent engineering , Define functional interfaces , In order to offer provider Realization ,consumer call
  • provider: Inherit parent engineering , Realization api Interface functions in
  • consumer: Inherit parent engineering , call api Function interface in

establish Parent engineering

  • pom file
<?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.lanh</groupId>
    <artifactId>parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>api</module>
        <module>provider</module>
        <module>consumer</module>
    </modules>

    <properties>
        <spring.version>5.2.5.RELEASE</spring.version>
        <dubbo.version>2.7.6</dubbo.version>
        <curator.version>4.2.0</curator.version>
        <registry-zookeeper.version>2.7.6</registry-zookeeper.version>
        <servlet.version>3.1.0</servlet.version>
        <jsp.version>2.0</jsp.version>
        <jstl.version>1.2</jstl.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo</artifactId>
                <version>${dubbo.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-registry-zookeeper</artifactId>
                <version>${registry-zookeeper.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>${curator.version}</version>
            </dependency>

            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>${servlet.version}</version>
                <scope>provided</scope>
            </dependency>

            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
                <version>${jsp.version}</version>
                <scope>provided</scope>
            </dependency>

            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>${jstl.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

establish api project

Create functional interfaces

public interface DemoDubboService {
    
    String showMsg(String msg);
}

establish provider project

  • To configure pom file
<dependencies>
     <dependency>
         <groupId>com.lanh</groupId>
         <artifactId>api</artifactId>
         <version>1.0-SNAPSHOT</version>
     </dependency>

     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-context</artifactId>
     </dependency>

     <dependency>
         <groupId>org.apache.dubbo</groupId>
         <artifactId>dubbo</artifactId>
     </dependency>

     <dependency>
         <groupId>org.apache.dubbo</groupId>
         <artifactId>dubbo-registry-zookeeper</artifactId>
     </dependency>

     <dependency>
         <groupId>org.apache.curator</groupId>
         <artifactId>curator-framework</artifactId>
     </dependency>
 </dependencies>

Be careful :provider The project and consumer Projects depend on api project

  • Implementation interface
package com.lanh.dubbo.service.impl;

import com.lanh.dubbo.service.DemoDubboService;
import org.apache.dubbo.config.annotation.Service;

/** * @Author Lanh **/
@Service
public class DemoDubboServiceImpl implements DemoDubboService {
    
    public String showMsg(String msg) {
    
        return "Helle Dubbo "+msg;
    }
}

It should be noted that , The annotations used here @Service No spring The annotations in , It is dubbo Annotations

  • Write the startup class
package com.lanh.dubbo;

import org.apache.dubbo.container.Main;

/** * Dubbo The start of class  * @Author Lanh **/
public class Start {
    
    public static void main(String[] args) {
    
        Main.main(args);
    }
}
  • The configuration file xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!--  Define service name  -->
    <dubbo:application name="myProvider" />

    <!-- Configure the registry -->
    <dubbo:registry address="ip:2181,ip:2182,ip:2183" protocol="zookeeper" timeout="10000"/>

    <!-- Configure the service protocol and listening port ( Any unoccupied port )-->
    <dubbo:protocol name="dubbo" port="2002" />

<!-- &lt;!&ndash; Registered interface &ndash;&gt;-->
<!-- <dubbo:service interface="com.lanh.dubbo.service.DemoDubboService" ref="demoService"/>-->
<!-- <bean id="demoService" class="com.lanh.dubbo.service.impl.DemoDubboServiceImpl" />-->

    <!-- Annotation development : Specify the location of the scan interface @Service-->
    <dubbo:annotation package="com.lanh.dubbo.service.impl" />

</beans>

Be careful :

  • The configuration file must be placed in resources/META-INF/spring/*.xml
  • When configuring the registry in the configuration file ip, You need to replace it with yourself zookeeper Of ip
  • I mentioned earlier @Service annotation , This corresponds to annotation development , another xml The form of development , I also wrote in the form of notes xml file

establish consumer project

web The project is in spring There is no need to write a startup class , It's through tomcat Plug in started

  • Definition Service Interface
package com.lanh.service;

public interface DemoService {
    
    String showInfo(String msg);
}

  • Realization Service Interface
package com.lanh.service.impl;

import com.lanh.dubbo.service.DemoDubboService;
import com.lanh.service.DemoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

/** * Consumer The business layer of  * @Author Lanh **/
@Service
public class DemoServiceImpl implements DemoService {
    

    @Reference
    private DemoDubboService demoDubboService;

    public String showInfo(String msg) {
    
        return this.demoDubboService.showMsg(msg);
    }
}

Be careful :

  • Used here @Service differ provider, This is spring Comments on the framework
  • meanwhile , When injecting here , Because this is not a local service , It is no longer used @Autowire annotation , It is dubbo Remotely called @Reference annotation
  • Page Jump
package com.lanh.web.controller;

import com.lanh.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/** * @Author Lanh **/
@RestController
public class DemoController {
    

    @Autowired
    private DemoService demoService;

    @RequestMapping("/getMsg")
    public String getMsg(String str){
    
        return this.demoService.showInfo(str);
    }
}

  • The configuration file
    applicationContext-spring.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <!-- Configure service name -->
    <dubbo:application name="myConsumer" />
    <!-- Configure the registry address and access protocol -->
    <dubbo:registry address="192.168.126.128:2181,192.168.126.128:2182,192.168.126.128:2183" protocol="zookeeper" timeout="10000"/>
    <!--dubbo Scan yourself @Reference annotation -->
    <dubbo:annotation package="com.lanh.service.impl"/>
</beans>

applicationContext-service.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.lanh.service" />

</beans>

springmvc.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.lanh.web.controller" />
    <mvc:annotation-driven/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext-*.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <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/springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--SpringMVC  Coding filter in -->
    <filter>
        <filter-name>encodeFilter</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>
    </filter>
    <filter-mapping>
        <filter-name>encodeFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

SpringBoot Integrate

establish api,maven The project is just

Set function interface

package com.lanh.dubbo.service;

public interface DemoDubboService {
    
    String showMsg(String str);
}

establish provider project

  • To configure pom file
<?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.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lanh</groupId>
    <artifactId>springbootdubbo_provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springbootdubbo_provider</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.lanh</groupId>
            <artifactId>springbootdubbo_api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.6</version>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-zookeeper</artifactId>
            <version>2.7.6</version>
        </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>

  • Realization api The interface of
package com.lanh.springbootdubbo_provider.service.impl;

import com.lanh.dubbo.service.DemoDubboService;
import org.apache.dubbo.config.annotation.Service;

/** * @Author Lanh **/

@Service
public class DemoDubboServiceImpl implements DemoDubboService {
    
    @Override
    public String showMsg(String str) {
    
        return "Hello Dubbo "+str;
    }
}

Again, this is @Service yes dubbo Annotations

  • Create startup class
    springboot It comes with its own startup class , No need to use dubbo The start of class
package com.lanh.springbootdubbo_provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootdubboProviderApplication {
    

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

}
  • To configure application.yml
# Specify the name of the service 
dubbo:
  application:
    name: myProvider
  registry:
    address: 192.168.126.128:2181,192.168.126.128:2182,192.168.126.128:2183
    protocol: zookeeper
    timeout: 10000
  # Configure the protocol used by the service 
  protocol:
    name: dubbo
    port: 2002
  scan:
    base-packages: com.lanh.springbootdubbo_provider.service.impl

establish consumer project

  • To configure pom rely on
<?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.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lanh</groupId>
    <artifactId>springbootdubbo_consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springbootdubbo_consumer</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.lanh</groupId>
            <artifactId>springbootdubbo_api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.6</version>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-zookeeper</artifactId>
            <version>2.7.6</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
            </exclusions>
        </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>

  • Definition Service Interface
package com.lanh.springbootdubbo_consumer.service;

public interface DemoService {
    
    String getMsg(String str);
}

  • Realization Service Interface
package com.lanh.springbootdubbo_consumer.service.impl;

import com.lanh.dubbo.service.DemoDubboService;
import com.lanh.springbootdubbo_consumer.service.DemoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

/** * @Author Lanh **/

@Service
public class DemoServiceImpl implements DemoService {
    

    @Reference
    private DemoDubboService demoDubboService;

    @Override
    public String getMsg(String str) {
    
        return this.demoDubboService.showMsg(str);
    }
}

Say it again : This @Service yes spring The annotations in , use @Reference without @Autowire

  • Configure page Jump logic
package com.lanh.springbootdubbo_consumer.controller;

import com.lanh.springbootdubbo_consumer.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/** * @Author Lanh **/

@RestController
public class DemoController {
    

    @Autowired
    private DemoService demoService;

    @RequestMapping("/getMsg")
    public String getMsg(String str){
    
        return this.demoService.getMsg(str);
    }
}
  • springboot The start of class
package com.lanh.springbootdubbo_consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootdubboConsumerApplication {
    

    public static void main(String[] args) {
    
        SpringApplication.run(SpringbootdubboConsumerApplication.class, args);
    }
}
  • To configure application.yml
# Specify the name of the service 
dubbo:
  application:
    name: myConsumer
  registry:
    address: 192.168.126.128:2181,192.168.126.128:2182,192.168.126.128:2183
    protocol: zookeeper
    timeout: 10000
  # Configure the protocol used by the service 
  protocol:
    name: dubbo

It's over , Welcome to discuss

原网站

版权声明
本文为[Snowy solitary boat]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202132338038225.html