One, foreword

When there are more and more services, problems such as capacity evaluation and waste of small service resources gradually emerge. In this case, a scheduling center needs to be added to manage cluster capacity in real time based on access pressure and provide cluster utilization. Among them, the resource scheduling and governance center for improving machine utilization is the key.

Second, Dubbo introduction

2.1 concept

Dubbo is a distributed service framework for Alibaba’s open source project. It is committed to providing high-performance and transparent RPC remote invocation solutions and SOA service governance solutions.

2.2 the principle

Dubbo schematic diagram

Call relationship description:

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

Node Role description

node The role that
Container Service run container
Provider The service provider that exposes the service
Consumer The service consumer that invokes the remote service
Registry A registry for service registration and discovery
Monitor Statistics service calls here and call time monitoring center

Quick start

Dubbo uses the full Spring configuration mode to transparently access applications without any API intrusion. You only need to load the Dubbo configuration with Spring.

3.1 Installing the Registry

Zookeeper is officially recommended as the registry, so this test uses Zookeeper and places it on the VM whose IP address is 192.168.2.14.

Gz -c /usr/cd /usr mv zookeeper-3.4.8 zookeeper CD /usr/zookeeper/conf Cp zoo_sample. CFG zoo. CFG # start zookeeper/usr/zookeeper/bin/zkServer. Sh # start to check the zookeeper running state, if there is a Mode: Standalone run/usr/zookeeper success/bin/zkServer. Sh statusCopy the code

3.2 Service Providers

Create a Maven project (a Web project called Dubo-Service).

Pom. XML configuration:

The < 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" > The < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Light < / groupId > < artifactId > dubbo - service < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > < packaging > war < / packaging > < dependencies > < the dependency > < the groupId > org. Springframework < / groupId > < artifactId > spring - the core < / artifactId > < version > 4.3.10. RELEASE < / version > </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId> zkClient </artifactId> <version>0.9</version> </dependency> </dependencies> </project>Copy the code

The web.xml configuration:

<? The XML version = "1.0" encoding = "utf-8"? > <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" Version = "3.0" > < display - name > dubbo - service < / display - name > <! - the spring container start - > < listener > < listener - class > org. Springframework. Web. Context. ContextLoaderListener < / listener - class > </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-dubbo.xml</param-value> </context-param> <! End --> </web-app>Copy the code

Interface:

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

Implementation class:

public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "Hello," + name; }}Copy the code

ApplicationContext – dubbo. XML configuration:

<? The 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.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="hello-demo"/> <dubbo:registry Address = "zookeeper: / / 192.168.2.14:2181" / > < dubbo: protocol name = "dubbo" port = "20880" / > < dubbo: service interface="com.light.dubbo.service.HelloService" ref="helloService"/> <bean id="helloService" class="com.light.dubbo.service.impl.HelloServiceImpl"/> </beans>Copy the code

3.3 Service consumers

Create a Maven project (a Web project called Dubo-Consumer).

Pom. XML configuration:

The < 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" > The < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Light < / groupId > < artifactId > dubbo - consumer < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > < packaging > war < / packaging > < dependencies > < the dependency > < the groupId > org. Springframework < / groupId > < artifactId > spring - the core < / artifactId > < version > 4.3.10. RELEASE < / version > </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> < version > 4.3.10. RELEASE < / version > < / dependency > < the dependency > < groupId > com. Fasterxml. Jackson. Core < / groupId > < artifactId > Jackson - databind < / artifactId > < version > 2.9.3 < / version > < / dependency > < the dependency > <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId> zkClient </artifactId> <version>0.9</version> </dependency> </dependencies> </project>Copy the code

The web.xml configuration:

<? The XML version = "1.0" encoding = "utf-8"? > <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" Version = "3.0" > < display - name > dubbo - consumer < / display - name > <! - the spring container start - > < listener > < listener - class > org. Springframework. Web. Context. ContextLoaderListener < / listener - class > </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-dubbo.xml</param-value> </context-param> <! End --> <! -- SpringMvc container start --> <servlet> <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>/</url-pattern> </servlet-mapping> <! -- SpringMVC container end --> </web-app>Copy the code

Copy the HelloService interface from the Dubo-Service project into the project (Dubo-Consumer).

Control layer:

@Controller public class HelloController { @Autowired private HelloService helloService; @RequestMapping("hello") @ResponseBody public String hello(String name) { return this.helloService.sayHello(name); }}Copy the code

ApplicationContext – dubbo. XML configuration:

<? The 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.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="hello-demo"/> <dubbo:registry Address = "zookeeper: / / 192.168.2.14:2181" / > < dubbo: protocol name = "dubbo" port = "20880" / > < dubbo: reference interface="com.light.dubbo.service.HelloService"/> </beans>Copy the code

For springmvc. XML configuration:

<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd "> <! - scan only contain @ Controller annotation class - > < context: component - scan base - package = "com. Light. Dubbo. Controller" > < context: include - filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <! < MVC :annotation-driven/> </beans>Copy the code

Start the project for the service provider (8080) and then the project for the service consumer (8081). Open a browser to http://localhost:8081/hello? Name = Jack, the result is as follows:

image

Four, monitoring,

4.1 Obtaining source Code

Git clone - branch dubbo - server https://github.com/alibaba/dubbo.gitCopy the code

After downloading, use IDE tools to introduce the subproject dubbo-sample\dubbo-monitor-sample for compilation and packaging. Dubbo-monitor-simple-2.6.0-assembly.tar. gz is generated in the target directory of the project.

4.2 Modifying the Configuration

1) Decompress dubbo-monitor-simple-2.6.0-assembly.tar.gz package and modify dubbo-monitor-simple-2.6.0\conf\dubbo.properties:

Dubbo. Registry. Address = zookeeper: / / 192.168.2.14:2181Copy the code

2) Add to the service provider configuration file:

<! <dubbo:monitor protocol="registry"/>Copy the code

Finally, start dubbo-monitor-simple-2.6.0\bin\start.bat. Open a browser and visit http://localhost:8080/. The effect picture is as follows:

image

5. Management console

Dubbo provides a set of management console for online management services. This management console is an internal customized version of Alibaba. The open source part mainly includes routing rules, dynamic configuration, service degradation, access control, weight adjustment and load balancing.

5.1 Obtaining running Projects

In the duboo source code downloaded in Section 4, the subproject dubo-admin is introduced through IDE tools for compilation and packaging. The dubo-admin-2.6.0.war zip file is generated in the target directory of the project.

5.2 Modifying The Configuration

Copy and paste the dubo-admin-2.6.6. war files and folders into tomcat ROOT directory and modify webapps\ROOT\ web-INF \ dubo.properties:

Dubbo. Registry. Address = zookeeper: / / 192.168.2.14:2181 dubbo. Admin. Root. The password = root dubbo. Admin. Guest. Password = guestCopy the code

There are two users: root and guest.

Finally, start the Tomcat container, open a browser and visit http://localhost:8080/. The page requires you to enter your account and password. The following is the effect picture after login:

image

Vi. Reference materials

Dubbo website

  • By moonlightL
  • Links to this article: www.extlight.com/2018/02/22/…
  • Copyright Notice: All posts on this blog are original, under a CC BY-NC-SA 4.0 license, unless otherwise stated. Please indicate the link of the original text and relevant information of the author at the beginning of the article, clearly indicate the modification (if any), and inform us through E-mail and other ways, thank you for your cooperation!