Grain mall — Distributed Basics P1~P27
In march of last year’s grain mall distributed base, advanced, senior has just started to learn when I was, but midway because something is interrupted, the result didn’t have time to start learning until now, and see so many people are now online learned, really feel very ashamed, rearrange the study notes, subsequent update.
[Grain Mall — Distributed Basics P1~P27] : blog.csdn.net/Empire_ing/…
directory
🔥1. Basic concepts of distributed
1. The service
2. Distributed & cluster & Node
3. Remote call & load balancing
4. Registry & Configuration Center
5. Service circuit breaker & service downgrade
6. The API gateway
🔥2. Project architecture
1. Architecture process
2. Project technology
🔥3. Distributed project startup error
1. The NPM install failed
< % if (process.env.node_env === ‘production’) {% > < %}else {% > < %} % >
3. The Renren-fast project fails to start
4.Nacos startup failure:
🔥4. Distributed project content
1.Nacos registry and configuration center
2.OpenFeign remote call
3. The GateWay GateWay
4. Achieve results
🔥1. Basic concepts of distributed
1. The service
Take a large monolithic application and break it down into individual microservices that are deployed and run independently
2. Distributed & cluster & Node
- Distributed: Jd’s different businesses are distributed in different places (referring to one way of implementation)
- Cluster: Each service is implemented by multiple servers (refers to the physical form)
- Node: a server in a cluster
3. Remote call & load balancing
- Remote invocation: each microservice calls each other (HTTP+JSON mode in springcloud)
- Load balancing: The above call or user access is implemented by evenly distributing requests to A,B,C and other machines, so that no machine is too busy and no machine is too idle (based on various load balancing algorithms: polling, minimum connection, hash).
4. Registry & Configuration Center
- Registration center: each micro service is registered in the registration center, so that which services can be known, convenient unified management
- Configuration center: As stated above, each microservice is implemented by multiple servers, so modifying the configuration related to each microservice needs to be managed by the configuration center.
5. Service circuit breaker & service downgrade
- Service circuit breaker: When microservice A calls Microservice B, if MICROservice B hangs up, the circuit breaker protection mechanism should be enabled for frequent access of MICROservice A and A default data should be returned to avoid time-consuming waiting and wasted request resources
- Service degradation: During the peak period of the system, system resources are strained, and non-core businesses are degraded to deal with other businesses. For example, Taobao in the Tmall Double 11, can not access to return the order of micro services, here is to return the order of service demoted, let the service down, don’t let you access, or error, and so on.
6. The API gateway
- API GateWay: intercepts user requests, including load balancing, circuit breaker, authentication, and traffic limiting. It’s like gate security
🔥2. Project architecture
1. Architecture process
2. Project technology
🔥3. Distributed project startup error
Node environment: node.js– version V10.20.0 (the following commands must be started as administrator)
Note: in many cases NPM will report an error, but using CNPM will download and start normally
1. The NPM install failed
- There is no package.json solution: blog.csdn.net/weixin_4016…
- Can not find taobao library: use
cnpm install
Starting as an administrator
So my overall startup process is:
#Switch admin to Renren-fast-vue (node.js-v10.20.0)
npm config set registry http://registry.npm.taobao.org/
cnpm install
npm run dev
Copy the code
< % if (process.env.node_env === ‘production’) {% > < %}else {% > < %} % >
CNPM rebuild node-sass --save-dev NPM uninstall node-sass CNPM install [email protected] NPM run devCopy the code
3. The Renren-fast project fails to start
-
Error in starting project — scheduled tasks cannot be injected
Solution: blog.csdn.net/shenlf_bk/a…
4.Nacos startup failure:
-
Cannot start tomcat:
Solution: blog.csdn.net/rengn/artic…
🔥4. Distributed project content
After configuring the basic environment of the front and back ends above, each project startup needs to prepare the environment in advance: NACOS startup, Renren-fast startup. Then you can start the project and write the code.
1.Nacos registry and configuration center
All microservices (including gateways) are registered in the registry and can be managed and configured in a unified manner. The main import process is divided into the following steps
- Importing Dependency Configuration
-
Add the startup class to the @enableDiscoveryClient auto-configuration
-
Configuration file import nacos find spring. Cloud. Nacos. Discovery. The server – addr = 127.0.0.1:8848 (of course there is also a namespace, group, and the extension – configs to configuration)
2.OpenFeign remote call
Microservice A wants to invoke the interface in Microservice B. The main import process is divided into the following steps
- Importing Dependency Configuration
- Start the class to join ` ` @ EnableFeignClients (basePackages = “com. Empirefree. Gulimall. Member. Feign”) ` automatic configuration,
- Write the Feignservice file
package com.empirefree.gulimall.member.feign;
import com.empirefree.common.utils.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient("gulimall-coupon")
public interface CouponFeignService {
@RequestMapping("/coupon/coupon/member/list")
public R membercoupones(a);
}
Copy the code
- (Feignservice finds the invoked service interface from the NACOS registry and can then invoke it from the caller, so nacOS must be started first.)
@Autowired
private CouponFeignService couponFeignService;
Copy the code
3. The GateWay GateWay
The configuration of nacOS registry and Gateway routes is simple and can be changed later.
spring:
cloud:
nacos:
config:
server-addr: 127.0. 01.: 8848
spring:
cloud:
gateway:
routes:
- id: product_route
uri: lb://gulimall-product
predicates:
- Path=/api/product/**
filters:
Copy the code