Dubbo (a): Dubbo operating principle

Preface:

  At the beginning of the entry Javaweb, learn basically MVC development model, a project is basically a model, view, controller three. But gradually as the system's service Cadogan, SOA model is more suitable for the current project development. The SOA pattern in Java development process is basically Dubbo and SpringCloud world. So today, we take a look at the operating principle of Dubbo.

A, SOA pattern

  First, a brief SOA model, it is helpful to understand Dubbo behind us.

  What SOA model is?

  SQA (Service-Oriented Architecture) i.e., service-oriented architecture, different functional units of the application (here understood as services) have been resolved. In this framework the project will not directly interact with the database, but to access the database by calling the various service interfaces.

  Mode advantage in what?

  So that the most immediate benefit is to solve the code redundancy, if multiple projects simultaneously access the database must be the same table. For example, access to a user table. We can call customer service inside interface development, without the need to write it again each project additions and deletions to change search user table. In addition to this, SOA can bring us benefits is the ability to allow developers to more rapid, more reliable and more reusable structure of the entire business system. MVC development model than in the past, to SOA-based systems can more calmly to face dramatic changes in the business .

  SOA diagram:

Two, Dubbo basic components

  Talk over SOA, now look at the internal architecture of Dubbo, I believe we more or less have seen this picture below:

  Many people will look line leak above diagram, explain the following:

  Purple dotted line : at the completion of start function Dubbo  blue-gray line : running functional process is executed, the broken line is an asynchronous operation, the solid line is a synchronous operation

  Provider: provider, service publisher. If the model is the use of SOA development, and this is the interface to interact with the database, which is the main service on the producer side

  Consumer: Consumers, call the service side. The front-facing Controller mainly in the side, producers can invoke methods remotely, the list will be updated in real time when consumers call the producers changed. Here are the specific look

  Container: Dubbo container, dependent on the Spring container. Here more attention is Dubbo is dependent on the Spring container. It must be combined with the use and Spring

  Registry: Registry when the Registry started the Container list of all the services that can be provided to register. Role: Tell Consumer provide what services and where services Party.

  Monitor: Listener

Three, Dubbo operating principle

  On the above map to see the architecture of the operating principle of Dubbo:

  0.Start: starting container, corresponding to the start Dubbo Provider, and create a corresponding directory structure, for example, code named common interface com.learnDubbo.demo.DemoService, creates /dubbo/com.learnDubbo.demo .DemoService directory, then create a directory providers, and then write their own URL address in the directory providers.

  1.Register: will go to registration centers to register after the start, can provide a list of all registered services. That subscribe to all providers and consumers URL addresses under /dubbo/com.learnDubbo.demo.DemoService directory.

  2.Subscribe: Consumer at startup, not only will register itself to ... / consumers / directory, while also subscribe ... / providers directory, real-time access URL string information on the Provider. When the service consumers start: created / consumers in /dubbo/com.learnDubbo.demo.DemoService directory directory and write their own URL address / consumers directory.

  3.notify: When the Provider modified, the message will be pushed to the registry Consummer. Provider is the registry will be observed here is the use of design patterns in observer mode. Zookeeper registered to center, for example, Dubbo has doSubscribe ZookeeperRegistry method is carried out in the producer subscribe and listen. Following analysis of the source code to see if the subscription process

@Override
     protected  void doSubscribe ( Final the URL of url, Final NotifyListener listener) {
         the try {
             IF (Constants.ANY_VALUE.equals (url.getServiceInterface ())) { // the URL of the service interface is obtained according to the *, that is, all 
                String root = toRootPath (); 
                the ConcurrentMap <NotifyListener, ChildListener> = zkListeners.get the listeners (URL); // pick listener at the URL 
                IF (the listeners == null ) { // does not exist, create it 
                    zkListeners.putIfAbsent (URL, new new of ConcurrentHashMap <NotifyListener, ChildListener>()); 
                    The Listeners = zkListeners.get (URL); 
                } 
                ChildListener zkListener = listeners.get (listener); // get the listener subdirectory 
                IF (zkListener == null ) { // can not be obtained subdirectory listener, then It will be a new one. 
                    listeners.putIfAbsent (listener, new new ChildListener () { 
                        @Override 
                        public  void childChanged (String parentPath, List <String> currentChilds) {
                             for (String Child: currentChilds) { 
                                Child =URL.decode (Child);
                                 IF (! {AnyServices.contains (Child)) 
                                    anyServices.add (Child); 
                                    // If the consumer's interface is *, will each subscribe to a url, will trigger another branch of logic
                                     // here is used to / dubbo callback method provided below are added, corresponding to the incremental 
                                    Subscribe (url.setPath (Child) .addParameters (Constants.INTERFACE_KEY, Child, 
                                            Constants.CHECK_KEY, String.valueOf ( to false )), listener) ; 
                                } 
                            } 
                        } 
                    }); 
                    zkListener= Listeners.get (listener); 
                } 
                zkClient.create (root, to false );
                 // add listener returns child nodes 
                List <String> = zkClient.addChildListener Services (root, zkListener); // subscription in the root directory sub-elements, such as: /dubbo/com.learnDubbo.demo.DemoService/providers 
                IF (! Services = null &&! services.isEmpty ()) {
                     for (String-Service: Services) { 
                        -Service = URL.decode (-Service); 
                        anyServices .add (service);
                        Subscribe (url.setPath (-Service) .addParameters (Constants.INTERFACE_KEY,-Service, 
                                Constants.CHECK_KEY, String.valueOf ( to false )), listener); 
                    } 
                } 
            } the else {
                 // here is clear for a subscription interface logic 
                List < the URL> = URLs new new the ArrayList <the URL> ();
                 // listens for each category path 
                for (String path: toCategoriesPath (URL)) { 
                    the ConcurrentMap <NotifyListener, ChildListener> = the listeners zkListeners.get (URL);
                     IF (the listeners == null ) { 
                        zkListeners.putIfAbsent (URL,new ConcurrentHashMap<NotifyListener, ChildListener>());
                        listeners = zkListeners.get(url);
                    }
                    ChildListener zkListener = listeners.get(listener);
                    if (zkListener == null) {
                        //封装回调逻辑
                        listeners.putIfAbsent(listener, new ChildListener() {
                            @Override
                            public void childChanged(String parentPath, List<String> currentChilds) {
                                ZookeeperRegistry.this.notify(url, listener, toUrlsWithEmpty(url, parentPath, currentChilds));
                            }
                        });
                        zkListener = listeners.get(listener);
                    }
                    //创建节点
                    zkClient.create(path, false);
                    //增加回调
                    List<String> children = zkClient.addChildListener(path, zkListener);
                    if (children != null) {
                        urls.addAll (toUrlsWithEmpty (url, path, Children)); 
                    } 
                } 
                //And subscription services under the URL will listen and will invoke real-time update of the list of Consumer, enabling calls. This method does not start speaking 
                Notify (URL, listener, URLs); 
            } 
        } the catch (the Throwable E) {
             the throw  new new RpcException ( "the Failed to Subscribe" + URL + "to ZooKeeper" + getUrl () + ", the cause is:" + E .getMessage (), E); 
        } 
    }

  4, invoke: according to the acquired Provider address, call the Provider real functions. This is the only way a synchronized because consumers want to get data from the producer to the next step, but Dubbo is a framework RPC, RPC's core lies only know the interface can not know the internal implementation. So in Consumer square proxy design pattern, to create a proxy object of a class party Provider, the Provider get real function through a proxy object that can protect the Provider real function.

  invoke portion of source code analysis

  Interested can look invoke the calling process

  . 5, Monitor: Consumer and Provider every minute Monitor to send statistics, the statistical information includes, visits, frequency

IV Summary

  Here are just a little on the principle of do a little analysis Dubbo, Dubbo also want to understand the point of view requires a combination of source code. Although many times we just a user, but it can understand the internal operation principle not only allow us to better use, while the inside of the programming ideas, design patterns and our need to learn. We will be back to do more on the principle of Dubbo resolution.

  

Guess you like

Origin www.cnblogs.com/Cubemen/p/11409068.html