Getting started with Dubbo in one minute

What is Dubbo? What can be done?

Dubbo is a distributed services framework and SOA governance solution. Its functions mainly include: high-performance NIO communication and multi-protocol integration, service dynamic addressing and routing, soft load balancing and fault tolerance, dependency analysis and degradation, etc.

What scenarios is Dubbo suitable for?

When the website becomes larger, it is inevitable to split the application for service, so as to improve development efficiency, optimize performance, and save key competitive resources.

When there are more and more services, the URL address information of the services will explode, configuration management will become very difficult, and the single point pressure of the hardware load balancer will also increase.

With further development, the dependencies between services become intricate, and it is not even clear which application should be started before which application, and architects cannot fully describe the architectural relationship of the application.

Then, the call volume of the service is increasing, and the capacity problem of the service is exposed. How many machines does this service need? When should the machine be added? etc

When encountering these problems, Dubbo can be used to solve them.
See the following link http://dubbo.apache.org/books/dubbo-user-book/preface/background.html

How does Dubbo perform?

Dubbo reduces handshakes through long connections, processes messages concurrently on a single connection through NIO and thread pools, and compresses data through binary streams, which is faster than short-connection protocols such as conventional HTTP. Inside Alibaba, it supports more than 2,000 services every day, 30 More than 100 million visits, the largest single machine supports nearly 100 million visits per day.

See the following link http://dubbo.apache.org/books/dubbo-user-book/perf-test.html

Dubbo Architecture

 

Node role description

node Role description
Provider The service provider that exposes the service
Consumer Service consumer calling the remote service
Registry Registry for Service Registration and Discovery
Monitor A monitoring center that counts service calls and call times
Container service running container


Description of calling relationship

1. The service container is responsible for starting, loading, and running the service provider.

2. When the service provider starts, it registers the service it provides with the registry

3. When the service consumer starts, it subscribes to the registration center for the services it needs.

4. The registry returns the service provider address list to the consumer. If there is a change, the registry will push it to the consumer based on the long connection.

5. The service consumer, from the provider address list, selects a provider to call based on the soft load balancing algorithm, and if the call fails, select another provider to call.

6. Service consumers and providers accumulate the number of calls and call time in the memory, and regularly send statistical data to the monitoring center every minute.

Quick Start Case

Provider service code

Create a new maven project to introduce related jars

pom.xml introduces spring and dubbo related jars

 <properties>
    <spring.version>4.2.4.RELEASE</spring.version>
  </properties>
  <dependencies>
    <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>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jms</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</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.10</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.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <configuration>
          <port>8081</port>
          <path>/</path>
        </configuration>
      </plugin>
    </plugins>
  </build>

configure web.xml

   <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext*.xml</param-value>
    </context-param>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>

Configure applicatinsContext.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://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://code.alibabatech.com/schema/dubbo  
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!--dubbo应用中心 通常项目名-->
    <dubbo:application name="dubbo_service"/>
    <!--配置dubbo注册中心 这里zk注册中心,需要提前启动zk-->
    <dubbo:registry address="zookeeper://192.168.16.199:2181"/>
    <!--包扫描-->
    <dubbo:annotation package="com.muzidao.dubbo.service.impl"/>
</beans>

Configure the service interface. This interface needs to be provided to consumers. It can be packaged into a jar package or copied to the consumer service code.

public interface HelloService {
    public String sysHello(String name);
}

Configure the class that implements the interface

import com.alibaba.dubbo.config.annotation.Service;
import com.muzidao.dubbo.service.HelloService;

/**
 * 服务实现类
 * @author: astali
 */
 //特别需要注意的地方,这个@Service不再是Spring,而是dubbo
@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public String sysHello(String name) {
        System.out.println("我是dubbo服务接口实现者。");
        return "Hello 木子道 ,欢迎关注" + name;
    }
}

Configure the consumer (Coosumer) service code

pom.xml is the same as the provider

configure web.xml

  <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:springmvc.xml</param-value>
        </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>

Place springmvc.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo 
 http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  <mvc:annotation-driven>
     <mvc:message-converters register-defaults="false">
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
             <constructor-arg value="UTF-8"/>
        </bean>
      </mvc:message-converters>
   </mvc:annotation-driven>

  <!--dubbo应用中心 通常项目名-->
  <dubbo:application name="dubbo_consumer"/>
  <!--配置dubbo注册中心  需提前启动zk-->
  <dubbo:registry address="zookeeper://192.168.16.199:2181"/>
  <!--包扫描 -->
  <dubbo:annotation package="com.muzidao.dubbo.controller"/>
</beans>

Configure consumer consumption code

package com.muzidao.dubbo.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.muzidao.dubbo.service.HelloService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: astali
 * Created by Administrator on 2018/5/3 17:25
 */
@RestController
@RequestMapping("/dubbo")
public class HelloController {
    //需要注意这里是引用dubbo引入注解
    @Reference
    private HelloService helloService;  
    //这个HelloService其实就是提供者的那个接口,
    这里我这是把接口复制到这个项目中,其实是可以打成jar引入进来。

    @RequestMapping("/hello")
    public String hello(){
        System.out.println("--木子道--正在说 -- Hello" );
        return  helloService.sysHello("dubbo");
    }
}

Now the configuration is basically completed, now start to see the effect

 

Related Reading

Zookeeper cluster construction

Why use ActiveMQ cluster

Play with ActiveMQ cluster

▼Long press the following QR code to follow▼

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325387982&siteId=291194637