table of Contents
table of Contents
1.2 Use nacos server in Linux environment
1.3 Build nacos server under windows
2. Construction of nacos client
2.2 Add comment: @EnableDiscoveryClient
2.3 Configuration file: application.yml
3. Nacos domain model division and detailed concept
4. Steps to access the configuration center for microservices
4.1 Add dependency package spring-cloud-alibaba-nacos-config
4.2 To write a configuration file, you need to write a bootstrap.yml configuration file
5.1 Download the compressed package after compilation
5.2 Configure the cluster configuration file
Serial number | name | link address |
1 | Spring cloud Alibaba series (1) Detailed use of nacos configuration center | https://blog.csdn.net/qq_38130094/article/details/104722653 |
Introduction
Spring cloud Alibaba is a sub-project of spring cloud. I have heard of a complete set of enterprise-level solutions for microservice development; including the necessary components for microservice development is Ali's microservice solution
About nacos: Nacos is committed to helping you discover, configure, and manage microservices. Nacos provides a set of simple and easy-to-use feature sets to help you quickly realize dynamic service discovery, service configuration, service metadata and traffic management.
1. Nacos server construction
Nacos download address: https://github.com/alibaba/Nacos/releases
1.2 Use nacos server in Linux environment
1: After entering the installation directory, upload the Nacos package and decompress it: tar -zxvf nacos-server-1.2.0.tar.gz
2: Enter the decompressed directory: cd nacos
3: Go to the bin directory to start nacos on a single machine: sh startup.sh -m standalone
4: View process or port status: ps -ef|grep java netstat -tunlp|grep 8848
5: Stop nocas Execute sh shutdown.sh in the nocas/bin directory
5: Visit the server of nocas http://IP:8848/nacos/index.html The default username and password is nocas/nocas; my one is now directly entered without a password
1.3 Build nacos server under windows
1: After decompressing the nacos directory, enter the bin directory:
2. Construction of nacos client
2.1 Add dependency
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring‐cloud‐alibaba‐nacos‐discovery</artifactId>
</dependency>
2.2 Add comment: @EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
@MapperScan("com.ssy")
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
2.3 Configuration file: application.yml
server:
port: 8081
spring:
datasource:
druid:
username: root
password: 123456
jdbcUrl: jdbc:mysql://localhost:3306/alibaba?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
driverClassName: com.mysql.jdbc.Driver
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
filters: stat,wall #\u914D\u7F6E\u8FC7\u6EE4\u5668
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
application:
name: product-center
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos配置地址
#metadata:
#version: v1
#namespace:
#group: pay
#cluster-name:
#namespace:
effect:
3. Nacos domain model division and detailed concept
To be studied
4. Steps to access the configuration center for microservices
4.1 Add dependency package spring-cloud-alibaba-nacos-config
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring‐cloud‐alibaba‐nacos‐config</artifactId>
</dependency>
4.2 To write a configuration file, you need to write a bootstrap.yml configuration file
spring:
cloud:
nacos:
config:
server-addr: localhost:8848
file-extension: yml
shared-dataids: common.yml,common2.yml
refreshable-dataids: common.yml,common2.yml
ext-config:
- data-id: common3.yml
group: DEFAULT_GROUP
refresh: true
- data-id: common4.yml
group: DEFAULT_GROUP
refresh: true
application:
name: product-center
profiles:
active: dev
- server-addr: localhost:8848 indicates how my microservice can find my configuration center
- spring.application.name=order-center means that the current microservice needs to ask the configuration center for order-center configuration
- spring.profiles.active=prod means that I need to ask the configuration center for the configuration of the order-center production environment
- The format of the requested file is ${application.name}- ${spring.profiles.active}.${file-extension}
- With the above files, the configuration file obtained from the nacos configuration center is product-center-prod.yml
5 Nacos cluster construction
5.1 Download the compressed package after compilation
wget https://github.com/alibaba/nacos/releases/download/1.3.0/nacos-server-1.3.0.tar.gz
5.2 Configure the cluster configuration file
In the conf directory of the decompression directory nacos/ of nacos, there is a configuration file cluster.conf, please configure each line as ip:port. (Please configure 3 or more nodes)
# ip:port
172.17.24.185:8848
172.17.24.186:8848
172.17.24.187:8848
5.3 Determine the data source
mysql data source
try to use highly available mysql data source
nacos-mysql.sql in the installation package to initialize the database
### The built-in data source does
not require any configuration **but the startup command needs to be modified**
sh startup.sh -p embedded
5.4 Start nacos
#单机:
sh startup.sh -m standalone
使用内置数据源:
sh startup.sh -p embedded
#使用外置数据源
sh startup.sh
5. 5 Visit
url: ip:port/nacos
username password: nacos/nacos