preface

After the Dubbo service Exposure series, the plan was to start the Dubbo service reference tutorial. But now that it’s the end of the year, some of my friends are talking to me about moving on next year. Job-hopping is nothing more than two reasons, one is not enough money, the other is the heart wronged. First of all, I’m not going to say much about not being paid enough, because everybody feels like they’re not being paid enough. Then why do you feel wronged? As a technical person, I think there are only two reasons why I feel wronged. One is that I can’t get attention in the company, and the other is that I can’t learn anything and grow up. It feels like the latter tends to dominate what I hear. As children, we value hobbies and interests. As adults, we value investments and returns.

Working overtime is the norm in the Internet industry. First of all, I am not opposed to working overtime. But the impact of overtime also has to be taken seriously. First of all, for the boss, the more overtime employees work, the more profits they get. Such as:

Our boss came to work in a brand new Lamborghini. I said, “Wow, that’s a great car,” and he said, “If you work hard, put your heart into it, and make it great, I’ll have another one next year.”

Third, once you work more overtime, you will have less time to learn new skills, so you will become less and less confident, naturally afraid to jump ship. But working overtime and studying are not in conflict. Even if I have to work overtime (for example, I am still working overtime on Saturday), you will see me every week. Please rest assured to keep a close eye on me.

If you change jobs because you can’t learn something, then you will often encounter a problem, that is, the company’s project is too low, when looking for a job. Technically, add, delete, modify and check this kind of thing is definitely not available. I use Freemarker + Mybatis Generator to create the code generator, which can run directly with one click. In business, the business of each company is different, so the discussion is not meaningful. Want to look for some data to install force on the net, but discover however do not know how to start. It was not easy to find some technical salon forced high PPT, unfortunately, they are empty theory, can not connect with their own company’s project. But fortunately, I inadvertently paid attention to the Fat Dynasty.

Let’s start by looking at the architecture of a typical Java project for a small to medium sized company I mentioned in three diagrams.

As you can see, the typical structure is three layers,

  • Access layer.Logic layer.Data storage layer.

It can also be divided into four layers

  • Access layer.Logic layer.Atomic service layer.Data storage layer.

Of course it can be divided into five layers

  • Access layer.Serialization layer (asynchronous message queue).Atomic service layer.The data layer.Data storage layer.

Of course, there are several layers depending on their own business. A good architecture is not achieved overnight, but a gradual process of evolution. As the title suggests, this article focuses on logical layer design (when is the rest? One a week anyway, that’s 48 a year. After the Dubbo series is over, the next series is up to you, so you can make your own estimates). Since it is a design, we should not be an armchair strategist, but must stand on the shoulders of giants. For example, the 58.com architecture design shared by Sun Xuan is of great reference significance. I’ve done a quick summary with mind mapping. As follows:


See here have a friend may not be happy, do not pull these principles, Lao Tze picked up the keyboard is dry.


What I want to say is that without reading ten thousand books, even if I travel ten thousand miles, I am still a postman. Now that we know the theory, let’s get down to business.

We interrupt the interview questions

  • Talk about the architecture of your project (many people will answer SSH or SSM when answering this question, note that SSH is the technology selection, not the architecture design)

  • Since you are using Dubbo in your project, can you tell us how you achieved service degradation through Dubbo? What are the ways of degradation and what are the differences?

  • The Dubbo monitoring platform can dynamically change some Settings of the interface. How does it work?

  • Since you said you read the dubbo source code, do you encounter any bugs? (High degree of differentiation, but also check whether read the source of the touchstone)


Straight into the theme

We analyze it from two perspectives, one is why service degradation is needed, the other is how to do service degradation

Why is service degradation required

When introducing a new technology, we must see what problem the new technology solves. Like service downgrades, what problems did he solve? As we know from the mind map above, when the site is at its peak, with high concurrency and limited service capacity, we can only temporarily block the marginal business. So what are the specific examples?

For example, when you shop in a certain treasure, you will be recommended some products when the payment is completed. But in big 11, there is too much concurrency. We need to keep the core business of “payment” running, so we don’t need to call the edge business of “recommend goods”, thus reducing some concurrency. But if DOUBLE 11, I first block the code of the interface of “Recommended products”, and I will open it later. This kind of simplistic approach is definitely not our ideal pursuit, at which point we need something like a “service switch”. This switch is service degradation

How to do service degradation

It’s not enough to know these design principles from the mind map. Let’s take Dubbo as an example to see service degradation in action. First, service downgrades in Dubbo are split into two

  • Shield (mock = force)

  • Fault tolerance (mock = fail)

What’s the difference between these two? We refer to the documentation

Mock =force:return+ NULL mock=force:return+ NULL Used to mask the impact on the caller when the unimportant service is unavailable.

Mock =fail:return+null mock=fail: Return +null = mock=fail: Return +null = mock=fail To tolerate the impact on the caller when the unimportant service is unstable.

So the use of fault tolerance is demonstrated separately below

First, we set a breakpoint, causing the request to time out


Configuration fault tolerance


The error message is cleared immediately with the following result


Shielding is not demonstrated, configuration is similar, the effect of their own debugging.

In fact, we can answer the difference between the two from the documentation. But older drivers may enjoy the naked principle more.

Source code analysis

First of all, I assume that you have read the source code of Dubbo weekly, so you are familiar with the MockClusterInvoker class, so we will directly look at the core code (according to the group of friends, try to code without maps).

No mock(normal),force:direct mock(shielding), and fail-mock(fault-tolerance) can also be seen in the no mock(normal),force:direct mock(shielding), and fail-mock(fault-tolerance) situations. Masking is very violent, directly not even call, just return a value set before.

public Result invoke(Invocation invocation) throws RpcException {
    Result result = null;
    
    String value = directory.getUrl().getMethodParameter(invocation.getMethodName(), Constants.MOCK_KEY, Boolean.FALSE.toString()).trim(); 
    if (value.length() == 0 || value.equalsIgnoreCase("false")){
        //no mock
        result = this.invoker.invoke(invocation);
    } else if (value.startsWith("force")) {
        if (logger.isWarnEnabled()) {
            logger.info("force-mock: " + invocation.getMethodName() + " force-mock enabled , url : " +  directory.getUrl());
        }
        //force:direct mock
        result = doMockInvoke(invocation, null);
    } else {
        //fail-mock
        try {
            result = this.invoker.invoke(invocation);
        }catch (RpcException e) {
            if (e.isBiz()) {
                throw e;
            } else {
                if (logger.isWarnEnabled()) {
                    logger.info("fail-mock: " + invocation.getMethodName() + " fail-mock enabled , url : " +  directory.getUrl(), e);
                }
                result = doMockInvoke(invocation, e);
            }
        }
    }
    return result;
}
Copy the code

Type on the blackboard and underline

There is a saying that all letters are better than no books. Dubbo also has some bugs. For example, I mentioned before in dubbo source code parsing -router, this monitoring platform is buggy, and now it appears again


You’ll notice that when you click Delete or enable and disable, there will be several. The solution is the same as before. Clear the node information on ZooKeeper (if you don’t understand it, please go back to the router article. This is what I have repeatedly stressed before, and it must be learned systematically, because I often skip the previous mentioned).

From this bug solving, we should also have some reverse thinking. Why is this monitoring platform so amazing that it can dynamically change some of the default Settings of the interface? When you clear the ZooKeeper node, some configuration information on the monitoring platform disappears. Apparently, the principle is to change the information of the nodes registered on ZooKeeper. The zooKeeper notification is used to generate invoker. (The details are detailed in ZooKeeper node creation, ZooKeeper connection, and ZooKeeper subscription, which will not be repeated here.)

Of course, in addition to these pits,dubbo polling in the cluster fault tolerance algorithm has a pit, need to adjust the current time to solve (because this is not used much, here is not detailed), but dangdang dubbox has a pit is more obvious. In the figure below, when you pass a null argument, there is a very obvious null pointer


Dangdang also solved this problem in the back


Of course, the code he fixed could also create an interview question. Of course, I’m not going to solve this one

What is the difference between Java | | and |

Grasp the key

You have to get to the point here. As you can see from the title, this article has three key words: Dubbo, logical layer design, service degradation

Just because I’m using Dubbo to show you one way to degrade a service doesn’t mean there’s only one way, but if you look closely at the mind map, there are many other ways to do it. In addition, if you think your project is relatively low, you can imagine that if you use these principles of mind mapping to design, then how to design, what are the advantages and disadvantages? Then try to reform yourself, this process of thinking and action, is your most valuable harvest, but also I want to convey the idea of learning. If you focus on dubbo configuring service downgrades, chances are you’ll only see additions, deletions, changes and checks, no matter how good the project is.

Write in the last

When I write here, it is already four o ‘clock in the morning. In 996, it was a challenge for me to stick to one post per week, but I also enjoyed the challenge. Every time I get off work, MY acquaintances will ask me why I get off work so late and work so much overtime, so much overtime pay. I said, no overtime pay, this time will be very natural to retort to, no overtime pay that why overtime. In the same way, some friends see me blogging, will also ask, do you make money every week blogging, I said no, this time the normal logic is also refuted, no money why do. In fact, sometimes blogs are written for others as well as themselves. The most important thing is to see a person’s determination to do something

Looking forward to meeting you next week. In view of my talent and learning, the wrong place also hope to be done right, also welcome to pay attention to my Jane book, the name of fat dynasty