Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Background introduction of Dubbo and the integration and use of Zookeeper and SpringMVC

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

The following brings you the background introduction of Dubbo and the integration and use of Zookeeper and SpringMVC, hoping to give you some help in practical application. Load balancing involves more things, there are not many theories, and there are many books on the Internet. Today, we will use the accumulated experience in the industry to do an answer.

With the development of the Internet and the continuous expansion of the scale of website applications, the conventional vertical application architecture has been unable to cope with. Distributed service architecture and mobile computing architecture are imperative. Dubbo is a distributed service framework, which was born in this case. Now the core business is extracted as an independent service, so that the front-end application can respond more quickly and stably.

First: introduce the background of Dubbo

Before large-scale service, the application may simply expose and reference the remote service through tools such as RMI or Hessian, invoke it by configuring the URL address of the service, and load balance through hardware such as F5.

(1) when there are more and more services, the configuration management of service URL becomes very difficult, and the single point of pressure on F5 hardware load balancer is also increasing.

At this point, a service registry is needed to dynamically register and discover services to make the location of services transparent.

By obtaining the address list of service providers in the consumer, soft load balancing and Failover can be realized, which can reduce the dependence on F5 hardware load balancer and reduce part of the cost.

(2) when further development, the dependency relationship between services becomes complex, and even can not tell which application should be launched before which application, the architect can not fully describe the architectural relationship of the application.

At this point, you need to automatically draw a dependency graph between applications to help the architect clean up the relationship.

(3) then, as the amount of service transfer becomes larger and larger, the problem of service capacity will be exposed. how many machines do this service need to support? When should I add the machine?

In order to solve these problems, the first step is to count the daily call volume and response time of the service as a reference index for capacity planning.

Secondly, the weight can be adjusted dynamically, on-line, the weight of a machine is increased all the time, and the change of response time is recorded in the process of increasing, until the response time reaches the threshold, and the number of visits at this time is recorded. Then multiply the number of visits by the number of machines to deduce the total capacity.

Second: a brief introduction to Dubbo

Node role description:

Provider: the provider of the exposed service.

Consumer: the service consumer that invokes the remote service.

Registry: the registry for service registration and discovery.

Monitor: the monitoring center that calculates the call time and call time of the service.

Container: the service running container.

Description of invocation relationship:

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

1. Upon startup, the service provider registers the services it provides with the registry.

two。 Service consumers subscribe to the services they need from the registry when they start up.

3. The registry returns a list of service provider addresses to the consumer, and if there is a change, the registry will push the change data to the consumer based on the persistent connection.

4. The service consumer, from the provider address list, chooses one provider to call based on the soft load balancing algorithm, and then chooses another one if the call fails.

5. Service consumers and providers accumulate the number of calls and call time in memory and regularly send statistics to the monitoring center every minute.

Dubbo provides many protocols, such as Dubbo protocol, RMI protocol and Hessian protocol. Let's take a look at the Dubbo source code and see the implementation of various protocols, as shown in the figure:

Before we used Dubbo, most of us used Hessian to use the exposure and invocation of our service, using HessianProxyFactory to invoke the remote interface.

The above is based on the introduction of the official website of Dubbo, and then we will introduce the integrated use of SpringMVC, Dubbo and Zookeeper.

Third: the integrated use of Dubbo, Zookeeper and SpringMVC

Step 1: install Zookeeper on Linux

Zookeeper as the registration center of Dubbo service, Dubbo was originally based on the database registration center, did not use Zookeeper,Zookeeper a distributed service framework, is a tree directory service data storage, can achieve cluster management data, here can be very good as the Dubbo service registry, Dubbo and Zookeeper can cluster deployment, when the provider power outage and other abnormal downtime, the Zookeeper registry can automatically delete provider information When the provider restarts, the registration data can be automatically restored, as well as subscription requests. We first install Zookeeper on linux, we install the simplest single point, the cluster is more troublesome.

(2) We put it in a folder under Linux and decompress it:

# tar zxvf zookeeper-3.4.6.tar.gz

(3) then there is a file under the corresponding zookeeper-3.4.6/conf, zoo_sample.cfg, which is configured with some information such as the port to listen for client connections. Zookeeper will find zoo.cfg as the default configuration file when starting, so we copy a file named zoo.cfg, as shown in the figure:

Let's take a look at some configuration information in this file, as shown in the figure:

Description:

ClientPort: the port that listens for client connections.

TickTime: basic event unit, in milliseconds. It is used to control heartbeats and timeouts, and by default the minimum session timeout is twice the tickTime.

We can configure the port of the configuration file, etc., or do advanced configuration and cluster configuration, for example: maxClientCnxns: limit the number of clients connected to ZooKeeper, etc.

(4) start the Zookeeper service, as shown in the figure:

Here, the installation and configuration of Zookeeper is complete.

Step 2: configure the management page of dubbo-admin to facilitate us to manage the page

(1) download the dubbo-admin-2.4.1.war package, deploy it in Linux's tomcat, put the dubbo-admin-2.4.1 under tomcat's webapps/ROOT, and then decompress it:

# jar-xvf dubbo-admin-2.4.1.war

(2) then under webapps/ROOT/WEB-INF, there is a dubbo.properties file that points to Zookeeper and uses the registry of Zookeeper, as shown in the figure:

(3) then start the tomcat service with user name and password: root, and visit the service to display the login page, indicating that the dubbo-admin has been deployed successfully, as shown in the figure:

Step 3: the integration of SpringMVC and Dubbo, the Maven management project used here

First: we first develop service registration, which is to provide services. The project structure is shown in the figure:

(1) A service interface has been added to the test-maven-api project, with the following code:

Java code

Public interface TestRegistryService {

Public String hello (String name)

}

(2) test-maven-console adds the jar package of Dubbo and Zookeeper in pom.xml, and references the jar package of test-maven-api as follows:

Java code

Cn.test

Test-maven-api

0.0.1-SNAPSHOT

Com.alibaba

Dubbo

2.5.3

Org.apache.zookeeper

Zookeeper

3.4.6

Com.github.sgroschupf

Zkclient

0.1

(3) test-maven-console implements specific services. The code is as follows:

Java code

@ Service ("testRegistryService")

Ublic class TestRegistryServiceImpl implements TestRegistryService {

Public String hello (String name) {

Return "hello" + name

}

(4) when we have finished our service and implementation, we need to expose the service as follows:

Java code

Description:

Description of some attributes of the dubbo:registry tag:

1) whether register registers the service with this registry. If set to false, it will only subscribe and will not register.

2) whether an error is reported when the check registry does not exist.

3) whether subscribe subscribes to this registry. If set to false, it will only register and not subscribe.

4) timeout registry request timeout (milliseconds).

5) address can be configured in Zookeeper cluster, and multiple addresses can be separated by commas, etc.

Some of the attributes of the dubbo:service tag describe:

1) the path to the interface service interface

2) ref references the ID of the Bean of the corresponding implementation class

3) registry registers with the specified registry, which is used in multiple registries. The value of the id attribute is the value. The ID of multiple registries is separated by commas. If you do not want to register the service with any registry, you can set the value to N A.

4) register default true, whether the service of this protocol is registered with the registry.

(5) start the project, and then we display the exposed services on the Dubbo management page, but show that there are no consumers, because we have not implemented the consumer services yet, as shown in the figure:

Second: we are developing service consumers, that is, invoking services, and we are creating a new consumer project structure as shown in the figure:

(1) the pom.xml of test-maven-server-console introduces the jar package of Dubbo and Zookeeper, and the jar package of test-maven-api. Because the jar package of test-maven-api is introduced, we call it in the project as if it were called locally. The code is as follows:

Java code

Cn.test

Test-maven-api

0.0.1-SNAPSHOT

Com.alibaba

Dubbo

2.5.3

Org.apache.zookeeper

Zookeeper

3.4.6

Com.github.sgroschupf

Zkclient

0.1

(2) the specific implementation of the test-maven-server-console project, the code is as follows:

Java code

@ 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 want to quote is as follows:

Java code

Description:

Description of some properties of dubbo:reference:

1) Service interface called by interface

2) check whether the provider exists when check starts. True reports an error, but false ignores it.

3) registry obtains the list of services from the specified registry registration, which is used in multiple registries, with the value of the id attribute, and the ID of multiple registries separated by commas

4) loadbalance load balancing policy. Available values: random,roundrobin,leastactive, which means random, round robin, and least active calls.

(4) when the project is launched, the Dubbo management page can see the consumers, as shown in the figure:

(5) then visit the consumer project, and the Controller layer can invoke the specific implementation of the service as if it were called locally, as shown in the figure:

Dubbo provides a variety of fault tolerance solutions, including load balancing, as shown in the figure:

1. Use Alibaba Druid connection pool (efficient, powerful and scalable database connection pool, monitor database access performance, support Common-Logging, Log4j and JdkLog, monitor database access)

two。 Provide high concurrency JMS message processing mechanism

3. All functional modularization, all module service, all service atomization, provide an extensible service model, so that the program runs stably and never downtime.

4. Provides Wink Rest and Webservice services, so it can be deployed as an independent service platform

Framework integration:

Springmvc + Mybatis + Shiro (permission) + REST (Service) + WebService (Service) + JMS (message) + Lucene (search engine) + Quartz (scheduled scheduling) + Bootstrap Html5 (support for PC, IOS, Android)

Introduction to the framework:

Project Maven construction, real large-scale Internet architecture, to achieve high concurrency, big data processing, the whole project uses customized service ideas, to provide modular, service-oriented, atomized solution, the functional modules will be split, can be shared to all projects. The architecture adopts distributed deployment architecture, and all modules are split, so that the project is absolutely decoupled and stability trumps everything.

Continuous integration:

1. My to-do workflow service (providing Webservice services)

two。 My to-do workflow integrates JMS message services (supports high concurrency and can support thousands of system integration)

3. My tasks provide Rest services, complete daily work management, and dynamically generate my tasks, cycle tasks, scheduled mail reminders to complete tasks, etc., through the scheduled scheduling platform.

4. File upload, multithreaded download service, sending mail, short message service, department information service, product information service, information release service, my subscription service, my task service, public link, my collection service, etc.

System module:

1. User Management:

User information management (add, delete, modify, user authorization, user column management, query, etc.)

User group management (add, delete, modify, user group column authorization, column authorization, query, user group personnel add query, etc.)

User role management (add, delete, modify, user role authorization, user role column information query settings, etc.)

two。 Article Management:

Column management: query infinite pole column tree, create infinite pole column tree classification (navigation column, picture list column, article list column, article content column, etc.), delete, modify column information.

Article management: create, delete, modify articles, multi-dimensional article queries, including published, unpublished, all articles, etc. Article rich text editor, article multi-file upload, article status control and so on.

3. System Settings:

Data dictionary management: support Chinese and English information, support unlimited level classification configuration, dynamic control whether available, etc.

Department information management: support unlimited level department information in Chinese and English to add, delete, modify operation, department list, tree heart query and so on.

Log management: system log list query, online viewing, online download, etc.

Route management: integrate Baidu map API, provide line query management function

Druid Monitor (monitoring): integrate Alibaba connection pool, provide online connection pool monitoring program, including: data source, SQL monitoring, URL monitoring, Session monitoring, Spring monitoring, etc.

Website information management: operate website content through system configuration files, including mail cloud server configuration, company basic information configuration and so on.

4. Integrated REST service, which can be used as an independent service platform (provides a large number of examples and testing platforms, including: file uploads and downloads, email messages, departments, products, public connections, my favorites, my tasks, information release, etc.)

5. Integrated Quartz scheduling, which can be used as a timing scheduling platform (dynamically configure scheduling classes, scheduling time, so that programs automatically perform certain services)

6. Lucene search engine, which can index documents and support document content search, keyword search, highlighted keywords, etc., so that the information can be extracted and queried in milliseconds.

7. User setting functions: including modifying user information, changing passwords, sending messages, modifying personal pictures, viewing roles, viewing user groups, administrators modifying roles, users, user groups, and so on.

8. Integrate Webservice platform, including jaxws service, CXF framework, configure double encryption authority authentication. Make service integration more secure.

9. Bootstrap html5 provides two sets of foreground environments, including CMS and e-commerce sites, to make your development more concise.

Technical points:

1. Springmvc + Mybatis integration, SpringSecurity permission control, Spring AOP transaction processing.

2. Wink Rest service, Webservice service: jaxws, CXF, etc.

3. IO stream uploads and downloads files and operates with multiple threads

4. Send mail, configure mail server, send mail based on html, plain text format

5. MD5 encryption (login password verification, encryption, etc.), unified Session, Cookie management, unified CAPTCHA verification, etc.

6. Unified configuration of database connection pool

7. Quartz scheduled task integration (directly through configuration)

8. Httpclient cracked the CAPTCHA and logged in to Unicom's recharge platform.

9. Chinese characters, English split, can be used as document keyword search and so on.

10. Base64 image processing, which supports PC,Android,IOS

11. Service Socket, Client Socket communication technology (GPRS data acquisition has been done and used in the project)

twelve。 Provides a large number of tool classes that can be used directly

13. Maven project building, you can directly do the architecture, you can improve your learning ability and make you a true architect.

After reading the above background introduction of Dubbo and the integration and use of Zookeeper and SpringMVC, if there is anything else you need to know, you can find out what you are interested in in the industry information or find our professional and technical engineer to answer, the technical engineer has more than ten years of experience in the industry.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report