Not long ago, we discussed nginx+tomcat composed of clusters, which is already very flexible clustering technology, but when our system encountered a larger bottleneck, the entire application of a single point of the server has not been able to meet our needs, at this time, we have to consider another, we are familiar with the content is distributed, And now the popular Dubbo framework, we can not ignore, here, let's discuss the use of this framework.
One, background
We used to call other people's interfaces remotely, and we did this:
The problem we encountered:
(1) With more and more services, service URL configuration management becomes very difficult, and the F5 hardware load Balancer's single point of pressure is getting bigger.
At this point, a service registry, dynamic registration and discovery services are required to make the location of the service transparent.
It also reduces the reliance on the F5 hardware load balancer by obtaining a list of service provider addresses in the consumer, which can reduce the partial cost.
(2) When further development, inter-service dependencies become a complex, even unclear which application to start before which application, the architect can not fully describe the application's architectural relationship.
At this point, you need to automatically draw a dependency graph between applications to help the architect clean up the relationship.
(3) Then, the service is more and more calls, the capacity of the service problem is exposed, how much machine support this service needs? When should I add a machine?
To solve these problems, the first step is to count the number of calls and response times for the service now, as a reference for capacity planning.
Secondly, to be able to dynamically adjust the weight, on-line, the weight of a machine has been increased, and in the process of increasing the time to record the change in response times, until the response time to reach the threshold, record the amount of traffic at this time, and then the amount of this traffic multiplied by the number of machines to push back total capacity.
To solve these problems, what did Dubbo do for us:
Load Balancing:
This is known as Soft load balancing!
Now let's come into contact with this excellent framework:
Brief introduction
Architecture
Node role Description:
Provider: The service provider that exposes the service.
Consumer: Invokes the service consumer of the remote service.
Registry: Registration Center for service Registration and discovery.
Monitor: The monitoring center of the call times of the statistics service and the call time.
Container: The service runs the container.
Call Relationship Description:
0. The service container is responsible for starting, loading, and running the service provider.
1. Upon launch, the service provider registers its services with the registry.
2. When the service consumer starts, it subscribes to the registration center for the services it needs.
3. The registry returns the service provider address list to the consumer, and if there is a change, the registry will push the change data to the consumer based on a long connection.
4. Service consumers, from the provider address list, based on the soft load equalization algorithm, select a provider to make the call, if the call fails, then choose another call.
5. Service consumers and providers, the cumulative number of calls in memory and call time, timed to send statistics every minute to the monitoring center.
Dubbo provides a lot of protocols, Dubbo Protocol, RMI Protocol, Hessian protocol, we look at Dubbo source code, there are various protocols implemented:
Before we used Dubbo, most used Hessian to use the exposure and invocation of our services, using Hessianproxyfactory to invoke the remote interface.
The above is a reference to the Dubbo official network introduction, Next we introduce SPRINGMVC, Dubbo, zookeeper integrated use.
Third: Dubbo and Zookeeper, SPRINGMVC integrated use
First step: Install Zookeeper on Linux
Zookeeper as the Dubbo Service Registration Center, Dubbo The original database-based registry, did not use Zookeeper,zookeeper a distributed service framework, is a tree-based directory service data storage, can achieve cluster management data, Here can be very good as Dubbo Service Registration Center, Dubbo can do with zookeeper cluster deployment, when the provider has a power outage, such as abnormal downtime, zookeeper registry can automatically delete the provider information, when the provider restarts, can automatically recover the registration data, as well as the subscription request. We first install the Zookeeper on Linux, we install the simplest single point, the cluster is more troublesome.
You'll need to install the JDK first, download it from Oracle's Java Web site, and install it very simply, no longer detailed.
Stand-alone mode
Single-machine installation is very simple, as long as the Zookeeper to obtain a compressed package and extract to a directory such as: C:/ZOOKEEPER-3.4.5/, Zookeeper startup script under the Bin directory, Windows startup script is Zkserver.cmd.
Before you execute the startup script, there are a few basic configuration items that need to be configured, Zookeeper configuration files in the Conf directory, this directory has zoo_sample.cfg and log4j.properties, what you need to do is Zoo_ Sample.cfg renamed Zoo.cfg because Zookeeper will find this file as the default profile at startup. The meanings of each configuration item in this configuration file are described in detail below.
<span style= "FONT-SIZE:18PX;" ># the number of milliseconds of each tick ticktime=2000 # The number of ticks that the initial # synchronization P Hase can take initlimit=10 # The number of ticks so can pass between # Sending a request and getting an Acknowledgem ENT synclimit=5 # The directory where the snapshot is stored. # do not use/tmp for storage,/tmp here is just # example sakes. Datadir=c://zookeeper-3.4.5//data Datalogdir=c://zoo Keeper-3.4.5//log # The port at which the clients would connect clientport=2181 # # is sure to read the maintenance section Of the # Administrator guide before turning on Autopurge. # # Http://zookeeper.apache.org/doc/current/zookeeperAdmin . html#sc_maintenance # # of snapshots to retain in DataDir #autopurge. snapretaincount=3 # Purge Task interval I N Hours # Set to ' 0 ' to disable Auto purge feature #autopurge .purgeinterval=1</span>
- Ticktime: This time is the time interval between the Zookeeper server or between the client and the server to maintain the heartbeat, that is, each ticktime time sends a heartbeat.
- DataDir: As the name implies is Zookeeper to save the data directory, by default, Zookeeper will write the data log file is also stored in this directory.
- Datalogdir: As the name implies Zookeeper the directory where the log files are saved
- ClientPort: This port is the port that the client connects to the Zookeeper server, Zookeeper listens to the port and accepts the client's access request.
When these configuration items are configured, you can start Zookeeper now, after starting to check whether Zookeeper is already in service, you can check with the Netstat–ano command to see if there is a ClientPort port number you configured in the Listening service.
Step Two: Configure the Dubbo-admin Management page to facilitate our management of the page
(1) Download Dubbo-admin-2.4.1.war package, in the Tomcat deployment of Windows, first put dubbo-admin-2.4.1 under Tomcat Webapps/root, then unzip
(2) then to Webapps/root/web-inf, there is a dubbo.properties file, which points to zookeeper, using the Zookeeper registry:
<span style= "FONT-SIZE:18PX;" >dubbo.registry.address=zookeeper://127.0.0.1:2181 Dubbo.admin.root.password=root dubbo.admin.guest.password= Guest</span>
(3) then start the Tomcat service, username and password: root, and access the service, display the landing page, indicating the success of the Dubbo-admin deployment:
Step three: Integration of SPRINGMVC and Dubbo, the MAVEN management project used here
First: We first develop service registration, is to provide services, project structure:
(1) The TEST-MAVEN-API project joins a service interface with the following code:
Public interface Testregistryservice {public string Hello (string name);}
(2) Test-maven-console in Pom.xml to add Dubbo and zookeeper jar package, reference Test-maven-api jar package, the code is as follows:
<span style= "FONT-SIZE:18PX;" ><dependency> <groupId>cn.test</groupId> <artifactid>test-maven-api</ artifactid> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency > <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> < version>2.5.3</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></span>
(3) Test-maven-console implementation of the specific services, the code is as follows:
@Service ("Testregistryservice") public class Testregistryserviceimpl implements Testregistryservice {public string Hello (string name) { return "hello" +name; }}
(4) We serve and implement well, at this time to expose the service, the code is as follows:
<?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:jee= "Http://www.springframework.org/schema/jee" xmlns:tx= " Http://www.springframework.org/schema/tx "<span style=" color: #cc0000; " >xmlns:dubbo= "Http://code.alibabatech.com/schema/dubbo" </span> xmlns:context= "http// Www.springframework.org/schema/context "xsi:schemalocation=" Http://www.springframework.org/schema/beans/http Www.springframework.org/schema/beans/spring-beans-3.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/HTTP Www.springframework.org/schema/tx/spring-tx-3.1.xsd Http://www.springframework.org/schema/jee/HTTP Www.springframework.org/schema/jee/spring-jee-3.1.xsd <span style= "color: #990000;" >http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span> Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-3.1.xsd "default-lazy-init=" false "> <!--Provider app name information, this is equivalent to a name, Our Dubbo Management page is clearer which app is exposed--<dubbo:application name= "Dubbo_provider" ></dubbo:application> <!-- Exposing service addresses with zookeeper registry--<dubbo:registry address= "zookeeper://127.0.0.1:2181" check= "false" Subscribe= "false "Register=" "></dubbo:registry> <!--service interface to expose--<dubbo:service interface=" Cn.test.dubbo.registry . Service. Testregistryservice "ref=" Testregistryservice "/> </beans>
Description
Dubbo:registry label Some of the properties of the description:
1) Register is registered to this registry service, if set to False, will only subscribe, not registered.
2) If the Check Registration center does not exist, whether to error.
3) Subscribe whether to subscribe to this registry service, if set to False, will only register, do not subscribe.
4) Timeout Registration center request time-out (ms).
5) address can be zookeeper cluster configuration, multiple addresses can be separated by commas and so on.
Some property descriptions of the Dubbo:service tags:
1) path to the interface service interface
2) ref references the bean ID of the corresponding implementation class
3) Registry registered with the designated registry, used at multiple registries, with a value of <dubbo:registry> ID attribute, multiple registry IDs separated by commas, and if you do not want to register the service with any registry, set the value to N/a
4) Register By default True, whether the service of the Protocol is registered to the registry.
(5) Start the project and then we show on the Dubbo Management page that the service has been exposed, but the display is not yet consumer, as we have not yet implemented consumer services:
Second: We are developing a service consumer, that is invoking the service, and we are creating a new consumer project:
(1) Test-maven-server-console Pom.xml introduces Dubbo and zookeeper jar packages, Test-maven-api jar packages, because the Test-maven-api jar package is introduced, We call in the project as if it were called locally. The code is as follows:
<span style= "FONT-SIZE:18PX;" ><dependency> <groupId>cn.test</groupId> <artifactid>test-maven-api</ artifactid> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency > <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> < version>2.5.3</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></span>
(2) The specific implementation of the Test-maven-server-console project, the code is as follows:
@Controller public class Indexcontroller { @Autowired private testregistryservice testregistryservice; @RequestMapping ("/hello") public String index (model model) { string Name=testregistryservice.hello ("zz"); System.out.println ("xx==" +name); Return ""; } }
(3) The address we are referring to, the code is as follows:
<?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:jee= "Http://www.springframework.org/schema/jee" xmlns:tx= " Http://www.springframework.org/schema/tx "<span style=" "><span style=" color: #990000; " >xmlns:dubbo= "Http://code.alibabatech.com/schema/dubbo" </span></span> xmlns:context= "http// Www.springframework.org/schema/context "xsi:schemalocation=" Http://www.springframework.org/schema/beans/http Www.springframework.org/schema/beans/spring-beans-3.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/HTTP Www.springframework.org/schema/tx/spring-tx-3.1.xsd Http://www.springframework.org/schema/jee/HTTP Www.springframework.org/schema/jee/spring-jee-3.1.xsd <span style= "color: #990000;" >http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span> Http://www.springframework.org/schema/context httP://www.springframework.org/schema/context/spring-context-3.1.xsd "default-lazy-init=" false "> <dubbo: Application Name= "Dubbo_consumer" ></dubbo:application> <!--using zookeeper registry to expose the service address--<dubbo:reg Istry address= "zookeeper://192.168.74.129:2181" check= "false" ></dubbo:registry> <!--the service to be referenced-- <dubbo:reference interface= "Cn.test.dubbo.registry.service.TestRegistryService" id= "Testregistryservice" > </dubbo:reference> </beans>
Description
Descriptions of some of the properties of Dubbo:reference:
1) interface invocation of the service interface
2) Check startup checks if provider is present, true error, false ignore
3) Registry Register for a list of services from a designated registry, use at multiple registries, value <dubbo:registry> ID attribute, multiple registry IDs separated by commas
4) LoadBalance Load Balancing strategy, selectable values: random,roundrobin,leastactive, respectively: Random, round robin, least active call
(4) Project launch, Dubbo Management page, can see the consumer,:
(5) then accessing the consumer project, the controller layer can invoke the specific implementation of the service as if it were local:
Summarize:
After a series of operations, we feel, indeed very simple, Dubbo to us to encapsulate a lot of operations, so that we do not need to consider the specific implementation details, configuration to build our application, such ideas, now in the IT industry is prevailing!
Java cluster Optimization--dubbo+zookeeper build a highly available distributed cluster