Big data development pre-skills __Dubbox quick start

1. Introduction to Dubbox
1.1 What is SOA architecture
SOA is the acronym for Service-Oriented Architecture, and it is an architectural style that supports service-oriented. From the perspective of service, service-based development and service results, service-oriented is a way of thinking. In fact, SOA architecture is more used in Internet project development.
Why do Internet projects adopt SOA architecture? With the development of the Internet, the scale of website applications continues to expand, and conventional vertical application architectures can no longer cope with it. Distributed service architecture and mobile computing architecture are imperative, and a governance system is urgently needed to ensure an orderly evolution of the architecture.
Let's take a look at the evolution of the development architecture. As shown below:
Insert picture description here

 Single application architecture
 When website traffic is very small, only one application is needed and all functions are deployed together to reduce deployment nodes and costs. At this time, the data access framework (ORM) is the key to simplify the workload of adding, deleting, modifying and checking.
 Vertical application architecture
 When the number of visits gradually increases, the acceleration caused by the addition of a single application to the machine becomes smaller and smaller. The application is divided into several unrelated applications to improve efficiency. At this point, the web framework (MVC) used to accelerate the development of front-end pages is the key.
 Distributed service architecture
 When there are more and more vertical applications, the interaction between applications is inevitable. The core business is extracted as an independent service, gradually forming a stable service center, so that front-end applications can respond more quickly to changing conditions Market demand. At this time, the Distributed Service Framework (RPC) for improving business reuse and integration is the key.
 Mobile computing architecture
 When there are more and more services, the evaluation of capacity and the waste of small service resources gradually appear. At this time, it is necessary to add a dispatch center to manage the cluster capacity in real time based on the access pressure and improve the utilization rate of the cluster. At this time, the resource scheduling and governance center (SOA) used to improve machine utilization is the key.
The Dubbox we are going to explain today is a mobile computing architecture.
1.2 Introduction to
Dubbox Dubbox is a distributed service framework. Its predecessor was Alibaba's open source project Dubbo. It was used in domestic e-commerce and Internet projects. Later, Alibaba stopped the maintenance of the project, and Dangdang.com optimized it on the basis of Dubbo. And continue to maintain, in order to distinguish from the original Dubbo, so it is named Dubbox.

Dubbox is committed to providing high-performance and transparent RPC remote service invocation solutions and SOA service governance solutions. Simply put, dubbox is a service framework. If there is no distributed requirement, it is actually unnecessary. Only when it is distributed, there is a requirement for a distributed service framework like dubbox, and it is essentially a service call. Dongdong, to put it bluntly, is a distributed framework for remote service invocation.
Insert picture description here

Node role description:
 Provider: The service provider that exposes the service.
 Consumer: The service consumer who calls the remote service.
 Registry: The registry for service registration and discovery.
 Monitor: A monitoring center that counts the call order and call time of the service.
 Container: The service runs the container.
Calling relationship description:
 0. The service container is responsible for starting, loading, and running the service provider.
 1. When the service provider starts, it registers its services with the registration center.
 2. When the service consumer starts, he subscribes to the registration center for the service he needs.
 3. The registration center returns the list of service provider addresses to the consumer. If there is a change, the registration center will push the
change data to the consumer based on the long connection .
 4. Service consumers, from the provider address list, based on the soft load balancing algorithm, select one provider to call, and
if the call fails, select another to call.
 5. Service consumers and providers accumulate the number of calls and call time in the memory, and send statistical
data to the monitoring center every minute .
1.3 Dubbox new features (understand)
Dubbox evolved from Dubbo and retains the original Dubbo configuration method. Here we introduce the new features of Dubbox.
Support REST-style remote calls (HTTP + JSON/XML): Based on the very mature JBoss RestEasy framework, REST-style (HTTP + JSON/XML) remote calls are implemented in dubbox to significantly simplify cross-language interaction within the enterprise. Significantly simplify the development of external Open API, wireless API and even AJAX server for enterprises. In fact, this REST call also allows Dubbox to provide basic support for today's particularly popular "microservice" architecture. In addition, REST calls have also achieved relatively high performance. Under benchmark tests, there is only about 1.5 times the gap between HTTP + JSON and Dubbo 2.x's default RPC protocol (ie TCP + Hessian2 binary serialization).
Supports efficient Java serialization implementation based on Kryo and FST: Based on today's more well-known Kryo and FST high-performance serialization libraries, a new serialization implementation has been added to Dubbo's default RPC protocol, and its serialization system has been optimized and adjusted, which is more significant Improved the performance of Dubbo RPC.
Supports Jackson-based JSON serialization: Based on the most widely used Jackson serialization library in the industry, a new JSON serialization implementation is added to Dubbo's default RPC protocol.
Support HTTP remoting system based on embedded Tomcat: implement dubbo's HTTP remoting system based on embedded tomcat (namely dubbo-remoting-http) to gradually replace the old version of embedded Jetty in Dubbo, which can significantly improve the remoteness of REST etc. Call performance, and upgrade the Servlet API support from 2.5 to 3.1. (Note: Except for REST, the WebServices, Hessian, HTTP Invoker and other protocols in dubbo are all based on this HTTP remoting system).
Upgrade Spring: Upgrade the Spring in dubbo from 2.x to the most commonly used 3.x version to reduce the trouble caused by version conflicts.
Upgrade ZooKeeper client: Upgrade the zookeeper client in dubbo to the latest version to correct the bugs contained in the old version.
Supports Dubbo configuration based entirely on Java code: Spring-based Java Config realizes a completely non-XML pure Java code way to configure dubbo.
Adjust the Demo application: temporarily adjust and rewrite the dubbo demo application to demonstrate the REST function and the newness of the Dubbo protocol. Serialization method, Spring configuration based on Java code, etc.
Note: Dubbox and Dubbo 2.X are compatible and have not changed any existing functions and configuration methods of Dubbo (except for the upgrade of Spring and other versions)
Insert picture description here
2. Dubbox development environment construction
2.1 Registration center Zookeeper
2.1.1 Zookeeper introduction
official It is recommended to use the zookeeper registry. The registration center is responsible for the registration and search of the service address, which is equivalent to a directory service. Service providers and consumers only interact with the registration center at startup, and the registration center does not forward requests, so the pressure is less.
Zookeeper is a sub-project of Apacahe Hadoop. It is a tree-type directory service that supports change pushes. It is suitable as a registration center for Dubbox services. It has high industrial strength and can be used in a production environment.
2.1.2 Zookeeper installation in Linux system
Installation steps:
Step 1: Install jdk
Step 2: Upload the zookeeper compressed package to the Linux system.
Alt+P enter SFTP, enter put d:\zookeeper-3.4.6.tar.gz to upload
Step 3: Unzip the compressed package
tar-zxvf zookeeper-3.4.6.tar.gz
Step 4: Enter the zookeeper-3.4.6 directory and create a data folder.
mkdir data
Step 5: Enter the conf directory and rename zoo_sample.cfg to zoo.cfg
cd conf
mv zoo_sample.cfg zoo.cfg
Step 6: Open zoo.cfg and modify the data attribute: dataDir=/root/zookeeper-3.4. 6/data
2.1.3 Zookeeper service start
Enter the bin directory, start the service and enter the
command./zkServer.sh start
output the following content to indicate a successful start

Shut down the service input command.
/zkServer.sh stop
output the following prompt message

Check the status:
./zkServer.sh status
If the startup status, prompt

If it is not started, prompt:

2.2 Dubbox local JAR package deployment and installation The
Dubbox jar package is not deployed to the central warehouse of Maven. You can find in the central warehouse of Maven that the final version of Dubbo is 2.5.3. After Alibaba disbanded the Dubbo team, Dangdang Net continues to maintain this project and renamed it Dubbox, the coordinates remain unchanged, and the version has changed, but it has not been submitted to the central warehouse.
We now need to manually install the Dubbox jar package into my local warehouse.
First put the dubbo-2.8.4.jar package in d:\setup, and then enter the command
mvn install:install-file -Dfile=d:\setup\dubbo-2.8.4.jar -DgroupId=com.alibaba -DartifactId= dubbo -Dversion=2.8.4 -Dpackaging=jar
2.3 Configure offline constraints
Address:
link
3. Getting started with Dubbox
3.1 Configuration method

3.1.1 Framework construction
(1) Create a Maven project (POM) dubboxdemo-parent and introduce dependencies in pom.xml

<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>cn.itcast.dubboxdemo</groupId>
  <artifactId>dubboxdemo-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>  
    <properties>		
		<spring.version>4.2.4.RELEASE</spring.version>
   </properties>    
	<dependencies>
		<!-- 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>
		<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.8.4</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.maven.plugins</groupId>  
	          <artifactId>maven-compiler-plugin</artifactId>  
	          <version>2.3.2</version>  
	          <configuration>  
	              <source>1.7</source>  
	              <target>1.7</target>  
	          </configuration>  
	      </plugin>  	      
	  </plugins>  
    </build> 
</project>
(2)创建Maven模块(JAR)  dubboxdemo-interface  

(3)创建Maven模块(WAR) dubboxdemo-service  
     在pom.xml 中,添加tomcat7插件
 <build>  
	  <plugins>
	      <plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<!-- 指定端口 -->
					<port>8081</port>
					<!-- 请求路径 -->
					<path>/</path>
				</configuration>
	  	  </plugin>
	  </plugins>  
    </build>
 在webapp下创建WEB-INF/web.xml 
<?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_2_5.xsd"
	version="2.5">	
	<!-- 加载spring容器 -->
	<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>	
</web-app>
(4)创建Maven模块(WAR) dubboxdemo-web   

In pom.xml, add tomcat7 plug-in


```bash
 <build>  
	  <plugins>
	      <plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<!-- 指定端口 -->
					<port>8082</port>
					<!-- 请求路径 -->
					<path>/</path>
				</configuration>
	  	  </plugin>
	  </plugins>  
    </build>
 在webapp下创建WEB-INF/web.xml 
 
<?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_2_5.xsd"
	version="2.5">	
   <!-- 解决post乱码 -->
	<filter>
		<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>	
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载-->
  	<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>
</web-app>

(5)构建依赖,让dubboxdemo-service 模块和dubboxdemo-web模块都依赖dubboxdemo-interface模块

3.1.2服务提供者
开发步骤:
(1)创建业务接口
在dubboxdemo-service 模块创建包cn.itcast.dubboxdemo.service,用于存放业务接口,创建接口UserService 

```bash
package cn.itcast.dubboxdemo.service;
/**
 * 业务接口
 * @author Administrator
 *
 */
public interface UserService {	
	public String getName();	
}
(2)创建业务实现类
创建包cn.itcast.dubboxdemo.service.impl ,用于存放业务实现类。创建业务实现类:
package cn.itcast.dubboxdemo.service.impl;
import org.springframework.stereotype.Service;
import cn.itcast.dubbodemo.service.UserService;
@Service
public class UserServiceImpl implements UserService {
	public String getName() {		
		return "itheima";
	}
}

(3) Write the configuration file
Create applicationContext-service.xml under src/main/resources, the content is as follows:
<context:component-scan base-package="cn.itcast.dubboxdemo.service.impl" />

<dubbo:application name=“dubboxdemo-service”/>
<dubbo:registry address=“zookeeper://192.168.17.129:2181”/>

<dubbo:service protocol=“dubbo”
timeout=“2000” connections=“100”
interface=“ cn.itcast.dubboxdemo.service.UserService" ref="userServiceImpl" />
(4) Test run
tomcat7:run
3.1.3 Service consumer
Development steps:
(1) Write Controller

package cn.itcast.dubboxdemo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.itcast.dubbodemo.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
    
    
	@Autowired
	private UserService userService;	
	@RequestMapping("/name")
	@ResponseBody
	public String showName(){
    
    
		return userService.getName();
	}		
}

(2) Write spring configuration file
Create springmvc.xml under src/main/resources

<context:component-scan base-package="cn.itcast.dubboxdemo.controller" />
<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="dubboxdemo-web" />
<dubbo:registry address="zookeeper://192.168.17.129:2181"/>
<dubbo:reference interface="cn.itcast.dubboxdemo.service.UserService" id="userService" />

(3) Test run
tomcat7:run
, enter http://localhost:8082/user/name.action in the browser, and check the browser output.
3.2 Annotation method
3.2.1 The framework is built
and the configuration method is the same (step omitted)
3.2.2 The service provider
(1) creates the package cn.itcast.dubboxdemo.service.impl to store the business implementation classes. Create a business implementation class:

package cn.itcast.dubboxdemo.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import cn.itcast.dubboxdemo.service.UserService;
@Service
public class UserServiceImpl implements UserService {
    
    
	public String getName() {
    
    		
		return "itcast";
	}
}

Note: The Service annotation is different from the original one. It needs to be imported under the com.alibaba package
(2) Create applicationContext-service.xml under src/main/resources, the content is as follows:
<dubbo:annotation package=“cn.itcast.dubboxdemo.service ”/>
<dubbo:application name=“dubboxdemo-service”/>
<dubbo:registry address=“zookeeper://192.168.17.129:2181”/>
Note: dubbo:annotation is used to scan @Service annotations, no need to write services Released configuration.
3.2.3 Service Consumer
(1) Write Controller

package cn.itcast.dubboxdemo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.itcast.dubbodemo.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
    
    
	@Autowired
	private UserService userService;	
	@RequestMapping("/name")
	@ResponseBody
	public String showName(){
    
    
		return userService.getName();
	}		
}

(2) Write spring configuration file
Create springmvc.xml under src/main/resources

	<!-- 引用dubbo 服务 -->
 <dubbo:annotation package="cn.itcast.dubboxdemo.controller" />  	
<dubbo:application name="dubboxdemo-web" />
<dubbo:registry address="zookeeper://192.168.17.129:2181"/>

(3) Test run
tomcat7:run
, enter http://localhost:8082/user/name.action in the browser, and check the browser output results

Guess you like

Origin blog.csdn.net/xianyu120/article/details/114686416