Dubbo website
What is the Dubbo?
What are its features? What are its advantages?
Why use it? What function does it have? What can Dubbo do?
What is the Dubbo?
Dubbo is a distributed services framework.
Note: If there is no distributed requirements, it is not necessary to use, only in the distributed time, there is a distributed service framework such as Dubbo requirements
2.Dubbo is an RPC framework that provides SOA service governance solutions
Description: Remote Procedure Call Protocol (RPC) refers to two servers A and B. Application A is deployed on server A, and application B is deployed on server B. Application A wants to Call the function (method) provided by application B on server B, but the Call cannot be directly invoked because the memory space is different. The semantics of the call and the data of the call need to be expressed through network communication, protocol and addressing. The beauty of RPC is that it hides the details of remote calls and makes calling remote services as easy as calling local services
RPC is mainly based on TCP/IP protocol, while HTTP service is mainly based on HTTP protocol. We all know that HTTP protocol is above TCP, HTTP is an application layer protocol, while TCP is a transport layer protocol. Therefore, IN terms of efficiency, RPC is of course superior.
Essentially, dubbo is a service invocation.
Service-oriented Architecture (SOA) : A service-oriented Architecture, also known as a resource scheduling and governance center. The so-called service, take project 2 as the simplest example, the commodity, order, search and other core business extracted as an independent service. In contrast to object-oriented, an object-oriented program is made up of classes one by one, while a service-oriented architecture is made up of services one by one. If multiple systems or terminals (PC, APP, etc.) want to call my service, it can be called through the same set of protocol and interface specifications. When more and more services are available, and capacity and resource utilization are different, we can add a scheduling center to manage cluster capacity based on access pressure in real time, thus improving cluster utilization. Therefore, Dubbo can serve as the resource (service) scheduling and governance center in the middle.
dubboThe architecture of the:
There are five roles/core components: Container, Provider, Registry, Consumer, and Monitor.
The container is responsible for starting, loading, and running the service provider.
At the same time, service providers register their services with the registry at startup.
Consumers subscribe to the registry for their services;
The registry returns the list of service providers to the consumer, and if there are changes, the registry pushes the change data to the consumer based on the long connection;
For service consumers, one provider is selected from the provider address list based on the soft load balancing algorithm. If the call fails, another one is selected.
Service consumers and providers, the accumulative call times and call time in memory, regularly send statistical data every minute to the monitoring center;
Dubbo provides four load balancing strategies: 1. Random balancing algorithm: Set random probability by weight, default
- Weight polling balancing algorithm: the polling ratio is set according to the weight of the convention, which can take into account the performance of each server, so it is common in practical applications.
- Minimum number of active calls equalization algorithm
- Consistent hash balancing algorithm: Requests with the same parameters are always sent to the same provider
What are its features? What are its advantages? What can Dubbo do?
1. It’s high-performance
2. It’s lightweight
3. It’s open source
4. It integrates seamlessly with the Spring framework
5. Transparent remote method invocation or interface oriented remote method invocation: call remote methods as local methods; Simple configuration, no API intrusion.
6. Soft load balancing and fault tolerance mechanism or intelligent fault tolerance and load balancing Description: Can replace hardware load balancers such as NGINx LVS on the Intranet.
7. Service registry automatic registration and service discovery & configuration management: There is no need to write the service provider address, the registry automatically query the provider IP based on the interface name. Using a distributed coordination service such as ZooKeeper as a service registry allows you to move most of your project configuration into the ZooKeeper cluster.
8. Service interface monitoring and governance Description: Dubbo-admin and Dubbo-Monitor provide perfect service interface management and monitoring functions. They can manage multiple versions, protocols, and registries for different interfaces of different applications.
Why use it?
Background:
When the size of the website reaches a certain level, the common MVC framework can no longer meet our needs, so the distributed service framework and flowing architecture are highlighted.
Single application Architecture
When site traffic is low, only one application is needed to deploy all functions together to reduce deployment nodes and costs. At this point, data access frameworks (ORM) that simplify the work of adding, deleting, modifying and reviewing are key.
Vertical application Architecture
When the volume of traffic gradually increases, the acceleration caused by the increase of a single application machine is getting smaller and smaller. The application is divided into several unrelated applications to improve efficiency. At this point, a Web framework (MVC) for accelerating front-end page development is key.
Distributed Service Architecture
With the increasing number of vertical applications, the interaction between applications is inevitable. Core businesses are extracted as independent services, gradually forming a stable service center, so that front-end applications can respond to changing market demands more quickly. At this point, the distributed Services framework (RPC) for business reuse and integration is key.
Mobile Computing Architecture
As the number of services increases, 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 to improve cluster utilization. At this point, a resource scheduling and Governance center (SOA) for improving machine utilization is key.
Before large-scale servitization, applications might simply expose and reference remote services through tools such as RMI or Hessian, invoke services by configuring service URLS, and load balance through hardware such as F5.
Requirements:
As there are more and more services, service URL configuration management becomes very difficult and the single point of pressure on the F5 hardware load balancer increases. In this case, a service registry is needed to dynamically register and discover services and make the location of services transparent. In addition, by obtaining the address list of the service provider from the consumer, the software load balancing and Failover can be implemented to reduce the dependence on the F5 hardware load balancer and reduce part of the cost.
As the dependencies between services become so complex that it is not even clear which application should be launched before which, the architect cannot fully describe the application’s architectural relationships. In this case, dependency diagrams between applications need to be drawn automatically to help the architect clean up the relationships.
Then, as more and more services are invoked, the capacity of the service is exposed. How much machine support does the service require? When should you add machines? In order to solve these problems, the first step is to calculate the service’s current daily call usage and response time as a reference index for capacity planning. Secondly, it is necessary to adjust the weight dynamically. On the line, the weight of a machine has been increased, and the change of response time is recorded in the process of increasing, until the response time reaches the threshold value, the page view is recorded at this time, and then the total capacity is reversed by the page view multiplied by the number of machines.
Options in real world scenarios:
- Spring Cloud: Spring family bucket, very comfortable to use, only you think, without it can not do. Unfortunately, due to the late release, there are no successful cases in China. Most of them are testing the waters, but after all, with the endorsement of Spring, they are still relatively optimistic.
- Dubbox: Dubbo supports REST, which is probably one of the most important reasons for companies to choose Dubbox. However, if Dubbo’s RPC call method is used, there will still be a strong API dependency between services.
- Thrift: If you’re a bit cold, you can build your own abstract custom framework based on Thrift.
- Montan: Maybe because it came out late, except for sina Weibo in early 2016,
- Hessian: If you are a startup or there are no more than five systems, I recommend this one because it is lightweight and simple in terms of development speed, operation and maintenance cost, and difficulty of getting started. Even if you migrate to SOA later, it will be seamless.
- RPCX /gRPC: In the absence of serious performance issues with the service or changes in the technology stack, it may never be introduced, and even if it is introduced, it may only be optimized for a small number of modules.
Suppose module A writes an interface, module B writes the implementation class of the interface, and now module A needs to query user data with ID 5. How does this work?
What is the architecture of it?
So how do we use Dubbo?
Actual case operation dubbo:
The first step:
Preparing the registry: Start the Zookeeper server. / opt/zookeeper 3.4.9 / bin/zkServer. Sh start
Creating the Common Project
Store the Java entity class: Employee
RemoteEmployeeService holds the interface that encapsulates remote method functionality: RemoteEmployeeService
Creating a Provider Project
Provides a RemoteEmployeeService interface implementation class
Expose services based on the RemoteEmployeeService interface
Add dependencies to pom.xml
<dependencies> <! Alibaba </groupId> <artifactId> Dubbo </artifactId> <version>2.5.5</version> </dependency> <! --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> The < version > 1.6.8 < / version > < / dependency > <! --> <dependency> <groupId>com.github. Sgroschupf </groupId> <artifactId> <version>0.1</version> </dependency> <dependency> <groupId>com.dubbo</groupId> <artifactId>Dubbo_Common</artifactId> < version > 0.0.1 - the SNAPSHOT < / version > < / dependency > < / dependencies >Copy the code
spring-provider.xml
<! Dubbo: Application name="provider"/> <! RemoteEmployeeService --> <bean id=" RemoteEmployeeService" class="com.api.impl.RemoteEmployeeServiceImpl"/> <! Dubbo :registry address="192.168.0.100:2181" protocol="zookeeper"/> <! - configuration to expose services - > < dubbo: service interface = "com. API. RemoteEmployeeService" ref = "RemoteEmployeeService" / > <! <dubbo:protocol name="dubbo" port="20880"/>Copy the code
web.xml
<! -- needed for ContextLoaderListener --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-provider.xml</param-value> </context-param> <! -- Bootstraps the root web application context before servlet initialization --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>Copy the code
RemoteEmployeeService Interface implementation class
Data changes can be obtained
Create the Consumer Project
pom.xml
<dependencies> <! -- servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> The < version > 2.5 < / version > < scope > provided < / scope > < / dependency > <! -- jsp-api --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> < version > 2.1.3 - b06 < / version > the < scope > provided < / scope > < / dependency > <! --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> The < version > 1.6.8 < / version > < / dependency > <! Alibaba </groupId> <artifactId> Dubbo </artifactId> <version>2.5.5</version> </dependency> <! --> <dependency> <groupId>com.github. Sgroschupf </groupId> <artifactId> The < version > 0.1 < / version > < / dependency > <! -- SpringMVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> < version > 4.3.10. RELEASE < / version > < scope > compile < / scope > < / dependency > <! Dubbo </groupId> <artifactId>Dubbo_Common</artifactId> < version > 0.0.1 - the SNAPSHOT < / version > < / dependency > < / dependencies >Copy the code
form.jsp
The form < form action = "${pageContext. Request. ContextPath} / query/emp" method = "post" > ID: <input type="text" name="empId" value="1" /> <input type="text" name="empName" value=" ABC "/><br/> < form>Copy the code
spring-consumer.xml
<context:component-scan base-package="com.handler"/> <mvc:annotation-driven/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"/> <property name="suffix" value=".jsp"/> </bean> <! Dubbo: Application name="consumer"/> <! Dubbo :registry address="192.168.0.100:2181" protocol="zookeeper"/> <! - reference service - > < dubbo: reference id = "RemoteEmployeeService interface =" ". Com API. RemoteEmployeeService "/ >Copy the code
Handler method
@Autowired
private RemoteEmployeeService remoteEmployeeService;
@RequestMapping("/handler")
public String queryEmployee(Employee employee,Model model) {
Employee queryEmployeeByCondition = remoteEmployeeService.queryEmployeeByCondition(employee);
model.addAttribute("employee", queryEmployeeByCondition);
return "target";
}
Copy the code
web.xml
<! -- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-consumer.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <! -- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>Copy the code
The project download
Start running the project
To run the provider
When running consumer, change the port number to avoid errors
page
Click on the submit
We know that ZooKeeper can be used as a registry for Dubbo. What is the node hierarchy after Dubbo registers service information on ZooKeeper server?
Figure 1:
Use the client to log in to the server. Before you log in, start the server. We started the server before, now you can log in directly
After logging in and viewing providers, you can see that it is encrypted
Dubbo in [Dubbo, Zookeeper, fruit] corresponds to the level of ROOT in Figure 1
[com API. RemoteEmployeeService] and interface name is consistent, the corresponding Service in figure 1
[Consumers, configurators, routers, providers] correspond to the Type in Figure 1
[dubbo % 3 a % 2 f % 2 f192. 168.0.1%3 a20880%2 fcom) API. RemoteEmployeeService % 3 fanyhost % 3 dtrue % 26 application % 3 dprovider % 26 dubbo % 3 d2 Generic 26. 5.5% % 3 dfalse % 26 interface % 3 dcom. The API. RemoteEmployeeService % 26 the methods % 3 dqueryemployeebycondition % 26 pid % 3 d10092%26 Revision % 3d0.0.1 -SNAPSHOT% 26Side %3Dprovider% 26TIMESTAMP %3D1545392365086] corresponds to the URL in Figure 1
We can decrypt it
public static void main(String[] args) throws UnsupportedEncodingException { String encode = "Dubbo % 3 a % 2 f % 2 f192 168.0.1%3 a20880%2 fcom) API. RemoteEmployeeService % 3 fanyhost % 3 dtrue % 26 application % 3 dprovider % 26 dubbo % 3 d2 Generic 26. 5.5% % 3 dfalse % 26 interface % 3 dcom. The API. RemoteEmployeeService % 26 the methods % 3 dqueryemployeebycondition % 26 pid % 3 d10092%26 Revision % 3 d0. 0.1 the SNAPSHOT % 26 side % 3 dprovider % 26 timestamp % 3 d1545392365086 "; String decode = URLDecoder.decode(encode, "UTF-8"); System.out.println("decode="+decode); }Copy the code
The Provider registration information is decrypted
Decode = dubbo: / / 192.168.0.1:20880 / com API. RemoteEmployeeService? Anyhost = true&application = provider&dubbo = 2.5.5 & generic = false&interface = com. API. RemoteEmployeeService&methods = queryEmploye EByCondition&pid = 10092 & revision = 0.0.1 – SNAPSHOT&side = provider×tamp = 1545392365086
Now that the information is clear, I’m not going to explain it, but you can test the consumer yourself, so I’m not going to test it.
The use of dubbo
Dubbo itself is not a service. Zookeeper is a JAR package that allows your Java application to connect to ZooKeeper and use ZooKeeper to consume and provide services. So you don’t have to start a Dubbo service on Linux.
But to make it easier for users to manage and monitor the many Dubbo services, officials have provided a visual monitoring program that will not affect use even if it is not installed.
Install monitoring software:
Materials: Tomcat8 + Dubbo-admin
Copy tomcat8 and dubo-admin to the /opt directory
Click to download Dubo-admin-2.6.0.war
Change permission chmod 777 dubo-admin-2.6.0.war
Unzip dubo-admin-2.6.6. war -d dubbo to the dubbo directory
Go to server.xml in tomcat conf
<Context docBase=”/opt/dubbo/” path=”dubbo” reloadable=”true” />
Then start the Tomcat service
Then type your IP in your browser: View the IP address: ifconfig
The user name and password are root
This is where you can monitor your services, but of course you haven’t added services yet.
Setting boot
[root@localhost tomcat4dubbo]# vim /etc/init.d/dubbo |
---|
Press Esc +:wq to save the configuration and exit
#!/bin/bash
#chkconfig:2345 20 90
#description:dubbo-admin
#processname:dubbo-admin
CATALANA_HOME=/opt/apache-tomcat-7.0.75
export JAVA_HOME=/opt/jdk1.8.0_152
case $1 in
start)
echo "Starting Tomcat..."
$CATALANA_HOME/bin/startup.sh
;;
stop)
echo "Stopping Tomcat..."
$CATALANA_HOME/bin/shutdown.sh
;;
restart)
echo "Stopping Tomcat..."
$CATALANA_HOME/bin/shutdown.sh
sleep 2
echo
echo "Starting Tomcat..."
$CATALANA_HOME/bin/startup.sh
;;
*)
echo "Usage: tomcat {start|stop|restart}"
;; esac
Copy the code
CATALANA_HOME = / opt/apache tomcat – 7.0.75 export JAVA_HOME = / opt/jdk1.8.0 _152
I want these two to be my own
Example Add permission chmod 777 dubbo
Chkconfig –add dubbo adds the script to the service
In this case, restart Tomcat.