当前位置:网站首页>Nacos installation and use

Nacos installation and use

2022-06-25 11:30:00 sermonlizhi

1.1 Nacos install

Refer to official documentation Reference:https://nacos.io/zh-cn/docs/quick-start.html
After the installation is completed, you can use http://xxx.com:8848/nacos visit Nacos The console of , The user name and password are nacos
Then create a custom namespace , The main function of namespaces is resource isolation

Nacos Is a service registry , It is also a configuration center , The following demonstrates service registration and configuration management

1.2 Service release

With shop-user For example , stay pom Add... To the file spring-cloud-starter-alibaba-nacos-discovery rely on , Then add... To the startup class @EnableDiscoveryClient annotation

<!--nacos client -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
@SpringBootApplication
@EnableDiscoveryClient
public class UserApplication {
    

    public static void main(String[] args) {
    
        SpringApplication.run(UserApplication.class,args);
    }

}

The configuration file is as follows :

server:
  port: 9527
spring:
  application:
    name: shop-user
  datasource:
    name: userDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://xxx.com:3306/shop?useUnicode=true&characterEncoding=UTF8&serverTimezone=Asia/Shanghai
    username: root
    password: lz123
    druid:
      max-active: 20
      min-idle: 10
      initial-size: 5
      test-while-idle: false
  cloud:
    nacos:
      discovery:
        server-addr: 120.79.221.55:8848
        namespace: 24712b7c-05ad-4b79-af97-1d202431f521
        group: LZ_GROUP_MASTER	

notes : Sub module dependencies must have spring-boot-starter rely on , Otherwise, the service cannot be registered to nacos
After starting the service , You can go to Nacos You can see the registered services in the service list of the console

1.3 Service configuration

1.3.1 In the sub module pom Add... To the file config rely on

<!--config rely on -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

1.3.2 add to nacos config To configure

notes : You can't use the original application.yml As a configuration file , It's a new one bootstrap.yml As a configuration file

Profile priority ( From high to low ):

bootstrap.properties -> bootstrap.yml -> application.properties -> application.yml

Create a bootstrap.yml The configuration file

spring:
  profiles:
    active: dev
  application:
    name: shop-user
  cloud:
    nacos:
      config:
        server-addr: xxx.com:8848
        namespace: 24712b7c-05ad-4b79-af97-1d202431f521
        group: LZ_GROUP_MASTER
        file-extension: yml

1.3.3 stay nacos Add configuration in

explain : Why configuration is needed spring.application.name , It's because it's made up of Nacos Configuration Management dataId Part of the field .

stay Nacos Spring Cloud in ,dataId The complete format is as follows :

${prefix}-${spring.profiles.active}.${file-extension}
  • prefix The default is spring.application.name Value , You can also configure items through spring.cloud.nacos.config.prefix To configure the

  • spring.profiles.active That is, the current environment corresponds to profile.

    Be careful : When spring.profiles.active It's empty time , Corresponding connector - There will be no ,dataId The splicing format of becomes ${prefix}.${file-extension}

  • file-exetension To configure the data format of the content , You can use the configuration item spring.cloud.nacos.config.file-extension To configure the . Currently only supported properties and yaml type .

We put the previous application.yml The connection information of the database configured in is put into the configuration item , Start again shop-user Program , The database can still be initialized successfully

notes :Nacos Support DNS agreement , That is to say, you can server-address Configure domain name at , The domain name is a simple IP Corresponding domain name , And Nacos It doesn't matter in itself

You can also add some customized configurations in the configuration file of the configuration center , Then, where the program needs to be used , Add a... To the class @RefreshScope annotation , That is, the configuration can be modified , The program dynamically updates the configuration without restarting the service

notes : The configuration file of the configuration center is the same as the local configuration file , Only the configuration center can realize dynamic update

原网站

版权声明
本文为[sermonlizhi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200537567580.html