Maven + springmvc + dubbo + zookeeper, mavenspringmvc

Source: Internet
Author: User

Maven + springmvc + dubbo + zookeeper, mavenspringmvc
Author original reprint please explain: Why to use dubbo http://www.cnblogs.com/c9999/p/6019307.html? Let's explain it officially: http://dubbo.io/User+Guide-zh.htm http://dubbo.io/General nginx + tomcat | ----> Controller1 ---------> service1 request -----> nginx | -----> Controller2 ---------> service2 request into the Controller only one way can go after using dubbo | -------> service1 request ------> Controller ----> | ----------> in simple words, service2 can deploy multiple services as a Contoller. Generally, the mvc project contains three layers: Controller, Servicei, ServiceImpl, and dao. We can use doubbo to split the project: controller as "consuming" a project ServiceImpl + dao as "provider" a project Servicei "interface" can be used as a project we can deploy multiple "supplied "...... ............. Zookeeper is the Registration Center of Dubbo services. Dubbo was originally a database-based registration center and did not use Zookeeper. Zookeeper, a distributed service framework, is a tree-type directory service data storage, the data can be managed by clusters. Dubbo can be used as the Registration Center for Dubbo services. Dubbo can deploy clusters with Zookeeper. When the provider suffers an abnormal shutdown, such as power failure, the Zookeeper registration center can automatically delete provider information. When the provider restarts, It can automatically restore registration data and subscribe to requests. We will first install Zookeeper on linux. We will install Zookeeper 1 on the simplest single-point Windons, and download the windons version on the Zookeeper official website. (Baidu will not be downloaded.) It is very easy to install a single machine, you only need to obtain the Zookeeper compressed package and decompress it to a directory. For example, open the startup script of Zookeeper in the bin directory. For Windows, the startup script is zkServer. cmd. Before executing 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. After opening

# The number of milliseconds of each ticktickTime=2000# The number of ticks that the initial # synchronization phase can takeinitLimit=10# The number of ticks that can pass between # sending a request and getting an acknowledgementsyncLimit=5# the directory where the snapshot is stored.dataDir=/tmp/zookeeper# the port at which the clients will connectclientPort=2181

  

  • TickTime: This time is used as the interval between the Zookeeper server or between the client and the server to maintain the heartbeat, that is, each tickTime will send a heartbeat.
  • DataDir: As the name implies, it is the directory where Zookeeper saves data. By default, Zookeeper also stores log files that write data in this directory.
  • ClientPort: the port connecting the client to the Zookeeper server. Zookeeper listens to the port and accepts access requests from the client.
2. dubbo-admin. Dubbo console installation http://dubbo.io/download-zh.htmdownload dubbo-admin-2.5.3.war unzip: Modify the META-INF/dubbo. properties File
dubbo.registry.address=zookeeper://127.0.0.1:2181dubbo.admin.root.password=rootdubbo.admin.guest.password=guest

Address: the IP address of zookeeper is followed by the port number, which is the same as the port number configured in zookeeper.

After the modification is complete, you need to open the tomcat \ webapps \ ROOT directory, which stores the tomcat homepage and deletes all files, copy all the files in dubbo-admin after decompression to \ webapps \ ROOT,

 

Now we have configured. Now you can start tomcat (the idea is to start zookeeper before starting tomcat, as described earlier ). After tomcat is started, enter http: // localhost: 8080 (my name is 8083) in the browser to enter the homepage of the dubbo console.

The username and password are the default username: root and password: root configured in dubbo. properties.

Enter the user name and password to go to the homepage.

 

Of course, there are no consumers and providers yet. Now we are developing the consumer and provider to configure zookeeper and then build maven + springmvc + dubbo environment to create 3 maven Project 1, test-dubbo-provider, java project, as a provider, serviceImpl and dao Layer 2, test-public-interface, java project, stores public interfaces, servicei is the service interface (Note: 1 depends on 2, 3 also depends on 2) 3. test-web-consumer, web project, storage Controller and page. First, we will develop the service provider, namely test-dubbo-provider, the directory structure is the configuration file for the ApplicationContent-dubbo.xml dubbo Service (name casually) (to be discussed later) ApplicationContent. xml spring configuration files these two configuration files must be placed under the MATE-INF/spring/(reason later) UserServiceImpl. java Implementation class
package com.cl.user.serviceImpl;import org.springframework.stereotype.Service;import com.cl.user.servicei.UserService;@Service("userService")public class UserServiceImpl implements UserService{@Overridepublic String sayHello() {System.out.println("hello world----------------------------");return "hello world";}}
With the implementation class, you also need the interface, that is, the Project test-public-interface. This project is simple, and you only need the interface.
package com.cl.user.servicei;public interface UserService {public String sayHello();}

Of course, Project 1 depends on Project 2.

So in pom. xml of 1

 

1 <dependency> 2 <groupId> test-web </groupId> 3 <artifactId> test-pubilc-interface </artifactId> Add project 2 dependency 4 <version> 0.0.1-SNAPSHOT </version> 5 <scope> test </scope> 6 </dependency>

Then, the UserServiceImpl in 1 can implement the UserService interface.

Of course we need to have dubbo core jar packages when we use dubbo, so we have
1 <dependency>2     <groupId>com.alibaba</groupId> 3     <artifactId>dubbo</artifactId>4     <version>2.5.3</version>5 </dependency>

Zookeeper also exists, so in pom. xml of 1

1 <dependency>2         <groupId>org.apache.zookeeper</groupId>3         <artifactId>zookeeper</artifactId>4         <version>3.3.3</version>5 </dependency>

The following describes the ApplicationContent-dubbo.xml and ApplicationContent. xml in Project 1.

ApplicationContent. xml is mainly used for spring configuration.
1 <! -- Start the component scan and exclude the @ Controller component. This component is scanned by the SpringMVC configuration file --> 2 <context: component-scan base-package = "com. cl. user. serviceImpl "/>

ApplicationContent-dubbo.xml dubbo service configuration

 
1 <! -- Provider application information, used to calculate dependencies --> 2 <dubbo: application name = "hehe_provider"/> 3 <! -- Expose the service address port in the zookeeper registration center to 2181 --> 4 <dubbo: registry address = "zookeeper: // 127.0.0.1: 2181"/> 5 <! -- <Dubbo: registry address = "multicast: // 224.5.6.7: 1234"/> --> 6 <! -- Expose services on port 20880 using dubbo protocol --> 7 <dubbo: protocol name = "dubbo" port = "20880"/> 8 <! -- Specific implementation bean --> 9 <bean id = "userService" class = "com. cl. user. serviceImpl. UserServiceImpl"/> 10 <! -- Declare the service interface to be exposed --> 11 <dubbo: service interface = "com. cl. user. servicei. UserService" ref = "userService"/>

Idea: Maybe your configuration file does not recognize <dubbo:> go online to find a solution

 

Here, our "provider" means that the service has been developed. How can we start the service? In Project 1, there is a start. java

 1 package com.test; 2  3 import org.apache.log4j.PropertyConfigurator; 4  5 public class start { 6      static{   7             PropertyConfigurator.configure("src/main/resources/log4j.properties");   8         }   9     public static void main(String[] args) {10         11         com.alibaba.dubbo.container.Main.main(args);12     }13 }

The idea contains the main method, which can be directly run and started. Don't worry !!! The problem was that the ApplicationContent-dubbo.xml and ApplicationContent. xml must be placed under/MATE-INF/spring /.

Because com. alibaba. dubbo. container. Main. main (args) loads the configuration file under/MATE-INF/spring/by default.

Let's take a look at the source code

After reading the source code, I can see why !!!!!!!!!!!!!!!!! After starting the service, we can see in dubbo-admin that the "provider" 192.168.56.1 we just developed is the local LAN address. This is the "provider" and there is no "consumer". Next we need to develop the "consumer "."

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.