[Dubbo practice] Dubbo + Zookeeper + Spring Integrated Application-Dubbo implements distributed services based on Zookeeper (2), dubbozookeeper

Source: Internet
Author: User

[Dubbo practice] Dubbo + Zookeeper + Spring Integrated Application-Dubbo implements distributed services based on Zookeeper (2), dubbozookeeper
Dubbo is integrated with Zookeeper and Spring

 

Dubbo uses the full spring configuration method to transparently access the application without any API intrusion to the application. You only need to use spring to load Dubbo configurations. Dubbo is loaded Based on Spring Schema extension.

 

I. Installing zookeeper in standalone Mode

 

1, download zookeeper registration center,: http://www.apache.org/dyn/closer.cgi/zookeeper/ download unzip to enter E: \ zookeeper-3.3.6 \ zookeeper-3.3.6 \ bin,

Double-click zkServer. cmd to start the registry service.

ZkServer. sh [Linux] Or zkServer. cmd [Windows]

 

2. Before you execute the startup script, you need to configure several basic configuration items. The Zookeeper configuration file is under the conf directory, which contains zoo_sample.cfg and log4j. properties, you need to change zoo_sample.cfg to zoo. cfg, because Zookeeper will find this file as the default configuration file at startup. The following describes in detail the meaning of each configuration item in this configuration file.

 

 

 

• TickTime: This is the interval between the Zookeeper server or between the client and the server to maintain the heartbeat, that is, each tickTime sends a heartbeat.

• DataDir: the directory where Zookeeper stores data. By default, Zookeeper stores log files that write data in this directory.

• DataLogDir: As the name implies, it is the directory where Zookeeper stores log files.

• ClientPort: the port connecting the client to the Zookeeper server. Zookeeper listens to this port and accepts access requests from the client.

 

After configuration, zookeeper listens to port 2181 of the local machine.

After these configuration items are configured, you can start Zookeeper now. After starting Zookeeper, check whether Zookeeper is in the service, you can run the netstat-ano command to check whether the clientPort number you configured is in the listening service.

 

Ii. Service Provider

 

Define a service interface: (this interface must be packaged separately and shared with the service provider and consumer)

  1. Package com. unj. dubbotest. provider;
  2. Import java. util. List;
  3. Public interface DemoService {
  4. String sayHello (String name );
  5. Public List getUsers ();
  6. }

 

 

Implement interfaces on the service provider: (hide implementations on the service consumer)

  1. Package com. unj. dubbotest. provider. impl;
  2. Import java. util. ArrayList;
  3. Import java. util. List;
  4. Import com. unj. dubbotest. provider. DemoService;
  5. Public class DemoServiceImpl implements DemoService {
  6. Public String sayHello (String name ){
  7. Return "Hello" + name;
  8. }
  9. Public List getUsers (){
  10. List list = new ArrayList ();
  11. User u1 = new User ();
  12. U1.setName ("hejingyuan ");
  13. U1.setAge (20 );
  14. U1.setSex ("f ");
  15. User u2 = new User ();
  16. U2.setName ("xvshu ");
  17. U2.setAge (21 );
  18. U2.setSex ("m ");
  19. List. add (u1 );
  20. List. add (u2 );
  21. Return list;
  22. }
  23. }

 

Declare the exposure service with Spring Configuration:

  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <Beans xmlns = "http://www.springframework.org/schema/beans"
  3. Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: dubbo = "http://code.alibabatech.com/schema/dubbo"
  4. Xsi: schemaLocation = "http://www.springframework.org/schema/beans
  5. Http://www.springframework.org/schema/beans/spring-beans.xsd
  6. Http://code.alibabatech.com/schema/dubbo
  7. Http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  8. ">
  9. <! -- Specific implementation bean -->
  10. <Bean id = "demoService" class = "com. unj. dubbotest. provider. impl. DemoServiceImpl"/>
  11. <! -- Provider application information for dependency calculation -->
  12. <Dubbo: application name = "xs_provider"/>
  13. <! -- Use the multicast broadcast registration center to expose the service address -->
  14. <! -- <Dubbo: registry address = "multicast: // 224.5.6.7: 1234"/> -->
  15. <! -- Use the zookeeper registration center to expose the service address -- that is, the IP address and port number of the server where zookeeper is located -->
  16. <Dubbo: registry address = "zookeeper: // 192.168.24.213: 2181"/>
  17. <! -- Expose services on port 20880 using dubbo protocol -->
  18. <Dubbo: protocol name = "dubbo" port = "20880"/>
  19. <! -- Declare the service interface to be exposed -->
  20. <Dubbo: service interface = "com. unj. dubbotest. provider. DemoService"
  21. Ref = "demoService"/>
  22. </Beans>

Load the Spring configuration, start the service (or build the project as a web project, configure spring startup in web. xml, and then throw it to tomcat to provide the service ):

  1. Package com. unj. dubbotest. provider. impl;
  2. Import org. springframework. context. support. ClassPathXmlApplicationContext;
  3. Public class Provider {
  4. Public static void main (String [] args) throws Exception {
  5. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (
  6. New String [] {"applicationContext. xml "});
  7. Context. start ();
  8. System. in. read (); // to ensure that the service is always on, use the congestion of the input stream to simulate
  9. }
  10. }
3. service consumers

Use Spring configuration to reference remote services:

  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <Beans xmlns = "http://www.springframework.org/schema/beans"
  3. Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: dubbo = "http://code.alibabatech.com/schema/dubbo"
  4. Xsi: schemaLocation = "http://www.springframework.org/schema/beans
  5. Http://www.springframework.org/schema/beans/spring-beans.xsd
  6. Http://code.alibabatech.com/schema/dubbo
  7. Http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  8. ">
  9. <! -- Consumer Application name, used to calculate dependencies. It is not a matching condition. Do not use the same name as the provider. -->
  10. <Dubbo: application name = "hjy_consumer"/>
  11. <! -- Use the zookeeper registration center to expose the service address -->
  12. <! -- <Dubbo: registry address = "multicast: // 224.5.6.7: 1234"/> -->
  13. <Dubbo: registry address = "zookeeper: // 192.168.24.213: 2181"/>
  14. <! -- Generate a remote service proxy and use demoService like a local bean -->
  15. <Dubbo: reference id = "demoService"
  16. Interface = "com. unj. dubbotest. provider. DemoService"/>
  17. </Beans>

 

Call service test:

  1. Package com. alibaba. dubbo. demo. pp;
  2. Import java. util. List;
  3. Import org. springframework. context. support. ClassPathXmlApplicationContext;
  4. Import com. unj. dubbotest. provider. DemoService;
  5. Public class Consumer {
  6. Public static void main (String [] args) throws Exception {
  7. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (
  8. New String [] {"applicationContext. xml "});
  9. Context. start ();
  10. DemoService demoService = (DemoService) context. getBean ("demoService ");
  11. String hello = demoService. sayHello ("hejingyuan ");
  12. System. out. println (hello );
  13. List list = demoService. getUsers ();
  14. If (list! = Null & list. size ()> 0 ){
  15. For (int I = 0; I <list. size (); I ++ ){
  16. System. out. println (list. get (I ));
  17. }
  18. }
  19. System. in. read ();
  20. }
  21. }

Test results:

 

Appendix: Dubbo Management page

 

Need to download: war package for dubbo-admin-2.5.3

: Http://download.csdn.net/detail/u013286716/7041185

 

The procedure is as follows:

 

1. Replace the content of the ROOT folder that comes with tomcat/webapps (that is, replace the tomcat startup homepage), decompress the downloaded war package to webapps/ROOT, and replace it directly.

 

Note: jdk does not use 1.8. This experiment uses 1.6.

 

2. start tomcat and access ip: 8080 or use localhost: 8080 if it is local.

Enter the username and password, which can be viewed in the dubbo. properties file under E: \ apache-tomcat-7.0.6-dubbo \ webapps \ ROOT \ WEB-INF, such:

 

3. Access http: // 192.168.24.213: 38080/

 

 

 

4. Start our service providers and consumers.

 

 

Overall description:

Zookeeper (Registry) is deployed on 213 of machines, and service providers and consumers run on 215 of machines, of course, we can also deploy service providers and service consumers on different machines.

 

 

 

Source code source: http://minglisoft.cn/technology

Add QQ: 2042849237

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.