About Spring Cloud

First attach a summary of a previous interview:
Insert picture description hereInsert picture description hereInsert picture description hereInsert picture description hereInsert picture description hereInsert picture description here
Insert picture description hereInsert picture description hereInsert picture description hereInsert picture description hereInsert picture description hereInsert picture description here
Insert picture description hereInsert picture description here
Insert picture description here
Insert picture description hereInsert picture description hereInsert picture description hereInsert picture description hereInsert picture description hereInsert picture description hereInsert picture description hereInsert picture description here
This is a summary made when I was a beginner in Spring Cloud. Because I was a beginner, many knowledge points were written very one-sided, and even a summary in one sentence, which also led to the above The company encountered many problems when applying it to the project. Now that I have time, it is also for my own interview and future work to read it easily, so I made a more in-depth summary.
First of all: Which components of Spring Cloud will we use in enterprise-level development?
It’s so practical that it’s completely dependent on work. Every component of Sprig Cloud can help us solve many problems at work and simplify our development (shit!) In the

afternoon, I also spent some time to build a simple one myself. Shelf:
Insert picture description here
Basically these components are enough!
The above summary of the specific components is basically written, I refer to the official introductory manual for this demo!
Now let’s get to the main topic:
Question 1: How does service A need to access service B and obtain the data returned by service B?
Answer: Use Feign (declarative client)

@FeignClient(value = ApplicationConstant.服务名, fallback = CouponServiceHystrux.class)
public interface CouponServiceClient {
    
    
    @GetMapping(value = "/coupon/getAllErpCoupon")
    ResultResp<Page<ErpCouponListDTO>> getAllCoupon(@RequestParam(value = "pageable") Pageable pageable);

   
    @GetMapping(value = "/coupon/{id}")
    ResultResp<CouponDTO> getOneCoupon(@RequestParam(value = "id") Long id);
}

Here is an introduction to the underlying principle of Feign (novices don't spray!) When
starting, the program will scan the package, scan all @FeignClient annotated classes under all packages, and inject these classes into the spring IOC container. When the defined Feign interface is called, the RequestTemplate is generated through the dynamic proxy of the JDK .
RequestTemplate contains all the information of the request, such as request parameters, request URL, etc.
RequestTemplate generates the Request, and then hands the Request to the client for processing. This client is HTTPUrlConnection of JDK by default, or it can be OKhttp, HTTPClient of Apache, etc.
Finally, the client is encapsulated into LoadBaLanceClient, and the call is initiated in conjunction with ribbon load balancing.

RequestTemplate is a remote HTTP calling tool, provided by Spring BOOT
Method:
getForObject (address, parameter, encapsulated object type)
postForObject (address, parameter, encapsulated object type) is
similar to HttpClient, Jsoup
integrates RequextTemplate in Ribbon to provide retry And load balancing function.

There is also Hystrix downgrading and fusing. Feign integrates ribbon and Hystrix. In development, we usually write:

    @Override
    public ResultResp<Page<ErpCouponListDTO>> getAllCoupon(Pageable pageable) {
    
    
        return ResultResp.code(ResultCode.INTERNAL_SERVER_ERROR).build();
    }
``可以返回给前端一些错误信息,也可以返回null!
`

Guess you like

Origin blog.csdn.net/DoChengAoHan/article/details/103183314