当前位置:网站首页>Basic use of sringcloud & use of component Nacos
Basic use of sringcloud & use of component Nacos
2022-07-03 00:53:00 【I Want To IT Master】
Catalog
2、 Business scenario description SpringCloud
4、SpringCloud Core and common components
3、 The process of using components
5.1Spring Cloud Netflix first generation
5.2Spring Cloud Alibaba Second generation
5.3 Alibaba open source components
5.4 Alibaba commercialized components
Two 、SpringCloud And nacos build
1、 Create a maven Project as a parent project
2、 preparation , Import dependence
2.1 Import version number first
2.1 Import the required dependencies
4、 Import the dependencies in the parent project into the child project
5、 More configuration files in subprojects
6、 open nacos service ( Register producers and consumers )
7、 Write the method and inject comments
One 、SpringCloud Introduce
1、 What is? SpringCloud
springcloud The streaming application launcher is based on springBoot Of spring Integrated applications , Provide integration with external systems ,springcloud Task, A short-lived microservice framework , For fast building applications that perform limited data processing .
2、 Business scenario description SpringCloud
1、 Business scenario :
Develop an e-commerce website , To realize the function of payment order , The process is as follows :
1、 After creating an order , If the user pays for the order immediately , We need to update the order status to “ Paid ”
2、 Deduct the corresponding inventory of goods
3、 Inform the storage center , Make delivery
4、 Add corresponding points to the user's purchase
2、 Service analysis :
Order service 、 Inventory service 、 Warehousing services 、 Points service
3、 Process call :
After the user completes the payment for an order , I'll go to the order service , Update order status
Order service invokes inventory service , Complete the corresponding functions
The order service invokes the warehousing service , Complete the corresponding functions
Order service calls point service , Complete the corresponding functions
The above is the business scenario SpringCloud.
3、 What is microservice
Microservice architecture is an architecture pattern or an architecture style , He advocates dividing a single application into a group of small services , Each service runs in its own independent process , Coordination between services , Cooperate with each other , Provide the ultimate value for users .
4、SpringCloud Core and common components
springCloud There are five core components
Eureka: When each service starts ,Eureka Client Will register the service to Eureka Server, also Eureka Client also
Can be reversed from Eureka Server Pull the registry , To know where other services are
Ribbon: When a request is made between services , be based on Ribbon Load balancing , Choose one of the machines in a service
Feign: be based on Feign Dynamic agent mechanism , According to the annotation and the selected machine , Splicing request URL Address , Initiate request
Hystrix: The request is made by Hystrix The thread pool comes and goes , Different services go through different thread pools , Different services are implemented
Isolation of calls , Avoid service avalanches
Zuul: If the front end 、 The mobile side should call the back-end system , Unified from Zuul Gateway access , from Zuul The gateway forwards the request to the corresponding server
service
Next, share the first component
1、 Core components :
1、SpringCloud Core components :Eureka
Eureka It's the registry in the microservice architecture , Dedicated to service Registration and discovery .
The order service wants to call the inventory service 、 Warehousing services , Or point service , How to call ?
The order service doesn't know which machine the inventory service is on at all ! Even if he wants to make a request , I don't know who to send it to , having a heart but no strength
Eureka Client: Responsible for registering information about this service to Eureka Server in
Eureka Server: Registry Center , There's a registry in it , Save the machine and port number of each service
Some other components will be explained to you in the future .
2、 Common components
Spring Cloud Netflix Eureka: Service registry .
Spring Cloud Zookeeper: Service registry .
Spring Cloud Consul: Service registration and Configuration Management Center .
Spring Cloud Netflix Ribbon: Client load balancing .
Spring Cloud Netflix Hystrix: Service fault tolerance protection .
Spring Cloud Netflix Feign: Declarative service invocation .
3、 The process of using components
1. Ask for uniform passage API gateway (Zuul) To access internal services .
2. After the gateway receives the request , From the registry (Eureka) Get available services
3. from Ribbon After load balancing , Distribute to backend specific instances
4. Between microservices Feign Conduct communication processing business
5. Hystrix Be responsible for handling service timeout fusing
5、SpringCloud Relationship
Spring Cloud There are many subprojects : Netflix and Alibaba The two standards are used most
5.1Spring Cloud Netflix first generation
For multiple Netflix Component provided development kit , These include Eureka、Ribbon、Feign、Hystrix、Zuul、Archaius etc. .
Netflix Eureka: One is based on Rest Service governance components of services , Including service registry 、 Service registration and service discovery machine
The realization of the system , It realizes the cloud load balancing and the failover of the middle tier server .
Netflix Ribbon: Client load balancing service calling component .
Netflix Hystrix: Fault tolerant management tools , Achieve breaker mode , By controlling the nodes of the service , Thus, delay and fault are provided
More powerful fault tolerance .
Netflix Feign: be based on Ribbon and Hystrix Declarative service invocation component of .
Netflix Zuul: Microservice gateway , Provide dynamic routing , Access filtering and other services .
Netflix Archaius: Configuration Management API, Includes a series of configuration management API, Provide dynamic typed properties 、 Thread safety configuration operation 、 Polling framework 、 Callback mechanism and other functions .
5.2Spring Cloud Alibaba Second generation
Same as Spring Cloud equally ,Spring Cloud Alibaba It's also a set of microservice solutions .
Spring Cloud Alibaba One stop solution dedicated to microservice development . This project includes the development of distributed application microservices
Required components , It's convenient for developers to pass through Spring Cloud The programming model easily uses these components to develop distributed application services .
Depending on the Spring Cloud Alibaba, Just add some annotations and a little configuration , It can be Spring Cloud Application access to aliwei
Service solutions , Build distributed application system quickly through Alibaba middleware .
5.3 Alibaba open source components
Nacos: Alibaba open source products , A dynamic service discovery that is easier to build cloud native applications 、 Configuration management and service management
platform .
Sentinel: Lightweight traffic control products for distributed service architecture , Take traffic as a starting point , Slave flow control 、 Fuse drop
level 、 Multiple dimensions such as system load protection protect the stability of services .
RocketMQ: An open source distributed message system , Based on highly available distributed cluster technology , Provide low latency 、 Highly reliable
Message publishing and subscription services .
Dubbo:Apache Dubbo It's a high performance Java RPC frame , For service communication .
Seata: Alibaba open source products , An easy-to-use high-performance microservice distributed transaction solution .
5.4 Alibaba commercialized components
Alibaba Cloud ACM: An application configuration center product that centrally manages and pushes application configurations in a distributed architecture environment
product .
Alibaba Cloud OSS: Alibaba cloud object storage service (Object Storage Service, abbreviation OSS), Alibaba cloud provides
The vast amount of 、 Security 、 Low cost 、 Highly reliable cloud storage services . You can use it in any application 、 Anytime 、 Store and access... Anywhere
Ask any type of data .
Alibaba Cloud SchedulerX: A distributed task scheduling product developed by Alibaba middleware team , Provide seconds 、 accurate 、
Highly reliable 、 High availability timing ( be based on Cron expression ) Task scheduling service .
Alibaba Cloud SMS: SMS service covering the world , friendly 、 Efficient 、 Intelligent and interconnected communication capabilities , Help enterprises quickly build customer touch channel .
Two 、SpringCloud And nacos build
Explain with an example of producers and consumers :
1、 Create a maven Project as a parent project
Note that this project is just a simple maven The project is just
2、 preparation , Import dependence
About packing Fill in pom、jar、war The difference between :
packing( Packaging type , The default is jar)
pom : Parent project (pom Not in the project java Code , And don't execute any code , Just for aggregation engineering or passing dependencies )
jar : Internal call or service use
war : Projects that need to be deployed
2.1 Import version number first
<spring-boot.version>2.4.1</spring-boot.version>
<spring-cloud.version>2020.0.0</spring-cloud.version>
<spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
2.1 Import the required dependencies
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Be careful dependencyManagement and dependencies The difference between :
dependencyManagement( management jar The version of the package , Let subprojects refer to a dependency without displaying the listed version number , Subprojects can inherit or not )
dependencies: Manage dependencies inherited by subprojects
dependencyManagement And dependencies difference :
dependencies Even if the dependency is not written in the subproject , The child project will still inherit the dependency from the parent project ( Inherit all )
dependencyManagement It's just a statement of dependency , No introduction , Therefore, the declaration that the sub item needs to display needs to be based on
lai .
If you don't declare dependencies in subprojects , Is not inherited from the parent project .
Only the dependency is written in the subproject , And no specific version was specified , Will inherit the item from the parent project , also version and
scope Read from parent pom.
In addition, if the version number is specified in the subproject , Then you will use the... Specified in the subproject jar edition .
3、 Create subproject
Create two springBoot project , named cloudconsumer and cloudprovider
4、 Import the dependencies in the parent project into the child project
<parent>
<artifactId>cloud</artifactId>
<groupId>org.zj</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
5、 More configuration files in subprojects
provider Medium yml file :
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
application:
name: nacos-provider
server:
port: 8081
consumer Medium yml file :
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
application:
name: nacos-consumer
server:
port: 8082
6、 open nacos service ( Register producers and consumers )
Enter the following website to enter nacos Login screen
Now there is no data :
7、 Write the method and inject comments
provider:
controller class :
The consumer wants to buy a chicken leg from the producer .
package com.zj.provider;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProviderController {
@RequestMapping("/run")
public String run(){
return " A chicken leg ";
}
}
stay provider Start the class :
The following note is particularly important :
@EnableDiscoveryClient
package com.zj.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class CloudProviderApplication {
public static void main(String[] args) {
SpringApplication.run(CloudProviderApplication.class, args);
}
}
consumer:
controller class :
package com.zj.consumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ConsumerController {
private RestTemplate restTemplate;
@Autowired
public ConsumerController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@RequestMapping("/run")
public String run() {
return restTemplate.getForObject("http://nacos-provider/run",String.class);
}
}
About RestTemplate Detailed explanation of :RestTemplate Detailed explanation - You know
stay consumer Start the class
package com.zj.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class CloudConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(CloudConsumerApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
In which @LoadBalances annotation , Why use this annotation ?
@LoadBalances The meaning of can be explained as SpringCloud Medium ribbon( Load balancing ) It means , But because of ribbon Update stopped , So use @LoadBalances annotation , Take a look at the code and you will know why this annotation is used ,
return restTemplate.getForObject("http://nacos-provider/run",String.class);In the above code , The address was originally fixed , But it is likely that the service will change the service address , So use load balancing
LoadBalances Import into the dependence of consumers :
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
8、 To test :
Click the startup class to test :
The registration is successful !
Consumers successfully get drumsticks
That's all for today's knowledge , I hope it helps you !
边栏推荐
- Callback event after the antv X6 node is dragged onto the canvas (stepping on a big hole record)
- leetcode-2280:表示一个折线图的最少线段数
- Vulkan-性能及精细化
- [applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions
- How to systematically learn machine learning
- 全志A40i/T3如何通过SPI转CAN
- 【AutoSAR 九 C/S原理架构】
- [AUTOSAR I overview]
- Cordova plugin device obtains the device information plug-in, which causes Huawei to fail the audit
- Overlay of shutter (Pop-Up)
猜你喜欢
【AutoSAR 七 工具链简介】
[AUTOSAR VI description document]
详解RDD基本概念、RDD五大属性
[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions
【AutoSAR 六 描述文件】
Arduino开发之按键检测与正弦信号输出
百度智能云牵头打造智能云综合标准化平台
【AutoSAR 二 AppL概述】
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
antv x6节点拖拽到画布上后的回调事件(踩大坑记录)
随机推荐
Win10 多种方式解决无法安装.Net3.5的问题
Nacos+openfeign error reporting solution
Vulkan并非“灵药“
Leetcode 294. Flip game II (game theory)
[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions
Array common operation methods sorting (including ES6) and detailed use
Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
Some introduction and precautions about XML
How SQLSEVER removes data with duplicate IDS
【案例分享】让新时代教育发展与“数”俱进
Teach you JDBC hand in hand -- structure separation
MySQL multi table joint deletion
leetcode-224:基本计算器
【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议
Lex & yacc & bison & flex configuration problems
文件操作IO-Part2
Vulkan performance and refinement
Logback configuration file
leetcode-934:最短的桥
Rust ownership (very important)