头图
A true master always has the heart of an apprentice. Hello! Hello everyone, my name is Xiaoqi. Xiaoqi, an apprentice in the programmer world, intends to share some techniques in a light and humorous dialogue. If you think you have learned something through Xiaoqi's article, then give Xiaoqi a like. Continue to update, you can search [Xiaoqi JAVA Interview] on WeChat to read it for the first time, reply to [Information] and there are more benefits I have prepared for you! Reply to [Project] There are some project source codes that I have prepared for you. Reply [Resume Template] There is a resume template that I have prepared for you.

 title=

@[TOC]


foreword

I picked up the book from the last book. I played the game for a day yesterday. I was like a tiger. At first glance, the record was 0-5. In the end, the network manager couldn't stand it anymore and pulled the switch, saying that I affected the overall winning rate of the Internet cafe. I begged and left the Internet cafe (primary student: Big brother, I beg you, go away, we still want to play).

When I got home yesterday, I had nothing to do, so I continued to submit my resume. Don’t pick any company, it’s Haitou. Do you know what Haitou means? It means that your resume is as silent as it was cast in the ocean (incisive~).

Fortunately, Bole finally showed up. She found my Maxima and made an appointment with me for an interview the next day, and then we exchanged WeChat.

Don't be idle after adding WeChat, first look at the circle of friends, and judge whether this Bole is good or not according to the circle of friends (hehe~).

 title=

After reading the circle of friends, Bole is quite upright, but I don't know if Wen is gentle or not, is she as gentle and considerate as Aunt Liu~

1. Interview

1. Local stub

Interviewer: I see on your resume that you are proficient in Dubbo, so can you tell me what the local stub of Dubbo is?

Me: I think local stubs are similar to AOP facet-oriented to implement some functions, but the difference is that some logic is executed on the consumer side when the consumer side calls the server side.

For example, our consumer can verify the parameters on the consumer before calling the server, and if an exception occurs after calling the server, we can perform some custom exception handling on the consumer.

The explanation on the official website is as follows: use local stubs in Dubbo to execute part of the logic on the client side

After the remote service, the client usually only has the interface left, and the implementation is all on the server side, but the provider sometimes wants to execute some logic on the client side, such as: do ThreadLocal cache, verify parameters in advance, forge fault-tolerant data after the call fails, etc. Wait, at this time, you need to bring a Stub to the API. The client generates a Proxy instance, passes the Proxy to Stub 1 through the constructor, and then exposes the Stub to the user. The Stub can decide whether to adjust the Proxy.

在这里插入图片描述
Configured in the spring configuration file as follows:

 <dubbo:service interface="com.foo.BarService" stub="true" />

or

 <dubbo:service interface="com.foo.BarService" stub="com.foo.BarServiceStub" />

Provide the implementation of Stub:

 package com.foo;
public class BarServiceStub implements BarService {
    private final BarService barService;
    
    // 构造函数传入真正的远程代理对象
    public BarServiceStub(BarService barService){
        this.barService = barService;
    }
 
    public String sayHello(String name) {
        // 此代码在客户端执行, 你可以在客户端做ThreadLocal本地缓存,或预先验证参数是否合法,等等
        try {
            return barService.sayHello(name);
        } catch (Exception e) {
            // 你可以容错,可以做任何AOP拦截事项
            return "容错数据";
        }
    }
}

2. Local camouflage

Interviewer: Well, can you tell me what Dubbo's local camouflage is?

Me: As the name suggests, local disguise is to disguise a return parameter locally on the consumer side and return it directly to the consumer side. The original process is that the consumer side calls the server side, and the server side returns whatever the consumer side returns.

But now if the server is down, the consumer will not be able to return the data of the parameter type that it wants to return when calling the server. At this time, we can disguise a returned parameter on the consumer, so that the call to the server fails, or it does not return at all. When calling the server, you can directly return the local disguised data.

If there is a business scenario now, that is, the consumer calls the server and returns parameters normally, and returns a "fault-tolerant data" when abnormal.

Then we first configure it in the spring configuration file as follows: This is equivalent to BarService being a local masquerading interface

 <dubbo:reference interface="com.foo.BarService" mock="true" />

Then we provide the implementation of the Mock local masquerading interface in the project:

 package com.foo;
public class BarServiceMock implements BarService {
    public String sayHello(String name) {
        // 你可以伪造容错数据,此方法只在出现RpcException时被执行
        return "容错数据";
    }
}

In this way, when an exception occurs on the server, the client can return the data it wants to return.

Interviewer: Nice guy, when can I go back to Beijing for a job?

me: uh. . . Wait a minute, there are still many companies waiting to negotiate salary, I have to pick a suitable one.

Interviewer: I'll give you as much as you want, come to me

me: uh. . . Then the monthly salary is 100 W.

Interviewer: Hello, I can't hear what you said, the signal is not good. . .

Me: hey hey hey (beep beep beep beep beep...).

2. Summary

The relevant content here has not been sorted out, and the article will continue to be updated later, and it is recommended to collect it.

The commands involved in the article must be knocked several times like me. Only in the process of knocking can you find out whether you really master the commands.

If you think my article is not bad, please like it. In addition, you can search for [Xiaoqi JAVA Interview] on WeChat to read it as soon as possible, and reply to [Information] and there are more benefits I have prepared for you! Reply to [Project] There are some project source codes that I have prepared for you. Reply [Resume Template] There is a resume template that I have prepared for you.


小奇Java面试
12 声望7 粉丝