1. Microservice multi-instance (IDEA)
1.1 Right-click instance
1.2 Configure ports to prevent conflicts (-Deserver.port=xxxx)
2.Nacos
2.1 Stand-alone mode startup (windows) command
startup.cmd -m standalone
2.2 Effect diagram of successful startup
2.3 Nacos dependency
2.3.1 Parent project dependency
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
2.3.2 Client dependencies
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
2.3.3 Configuration in the yml configuration file
cloud:
nacos:
server-addr: localhost:8848 #nacos的服务地址
2.4 nacos service registration
2.4.1 Import nacos, discover depends on
2.4.2 Configure nacos address spring.cloud.nacos.server-addr
2.4.3 Microservice registered successfully
2.5 Cluster properties of nacos
2.5.1 yml configuration in configuration set
cloud:
nacos:
server-addr: localhost:8848
discovery:
cluster-name: HZ
2.5.2 Configure as HZ cluster
2.5.3 Configure the priority to call the local cluster and modify the rules of load balancing
1. Prioritize the list of service instances in the same cluster
2. The local cluster cannot find the corresponding microservice, so it goes to other clusters to find it, and a warning message is reported
3. After confirming that there is a list of available instances in the local cluster, use the random load balancing strategy to select instances
userservice:#要做配置的微服务名称
ribbon:
NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule
2.6 Load balancing based on weight
After finding the service list in the nacos console, click the edited microservice and set the weight to 0.1, which will greatly reduce the frequency of the microservice being called
Microservices with higher weights will be called more frequently
2.7 Environmental isolation
Create a new namespace, and the id is randomly generated by default
Fill in the namespace under the yml file of the microservice, pay attention to fill in the id of the namespace
2.8 nacos configuration management
2.8.1 Add configuration files in Ncaos
Date_ID is the microservice name-production environment.yaml, and the file format is yaml
2.8.2 The sequence of springboot startup configuration acquisition is as follows
As shown in the figure, bootstrap.yml will be read before application.yml is read, so information such as the address of nacos needs to be written in the bootstrap.yml file
2.8.3 The configuration management of nacos depends on:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
2.8.4 bootstrap.yml:
spring:
application:
name: userservice #微服务名称
profiles:
active: dev #开发环境
cloud:
nacos:
server-addr: localhost:8848 #Nacos 地址
config:
file-extension: yaml #文件后缀名
discovery:
cluster-name: HZ