Proficient in the configuration of SpringCloud/Boot configuration files in Nacos

Table of contents

1 What is nacos? 

Nacos map

 Nacos ecological map

2 Configuration center proficient in Nacos in springcloud

Directory Structure

log configuration file

original configuration file

Use Nocas to split the configuration file

Refer to the configuration file in nacos

Share configuration file operations in nacos

We use the same method to extract the log configuration


1 What is nacos? 

        Nacos is an open source distributed system service registry and configuration management platform. It can help developers quickly implement functions such as registration and discovery of microservices, dynamic configuration management, and service health monitoring. Simply put, Nacos is a tool that allows you to manage and use microservices more conveniently.

Nacos map

 Nacos ecological map

2 Configuration center proficient in Nacos in springcloud

Directory Structure

log configuration file

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="180" packages="">
    <properties>
        <property name="logdir">logs</property>
        <property name="PATTERN">%date{YYYY-MM-dd HH:mm:ss,SSS} %level [%thread][%file:%line] - %msg%n%throwable</property>
    </properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="${PATTERN}"/>
        </Console>

        <RollingFile name="ErrorAppender" fileName="${logdir}/error.log"
            filePattern="${logdir}/$${date:yyyy-MM-dd}/error.%d{yyyy-MM-dd-HH}.log" append="true">
            <PatternLayout pattern="${PATTERN}"/>
            <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
            </Policies>
        </RollingFile>

        <RollingFile name="DebugAppender" fileName="${logdir}/info.log"
            filePattern="${logdir}/$${date:yyyy-MM-dd}/info.%d{yyyy-MM-dd-HH}.log" append="true">
            <PatternLayout pattern="${PATTERN}"/>
            <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
            </Policies>
        </RollingFile>
        
        <!--异步appender-->
         <Async name="AsyncAppender" includeLocation="true">
            <AppenderRef ref="ErrorAppender"/>
            <AppenderRef ref="DebugAppender"/>
        </Async>
    </Appenders>
    
    <Loggers>
         <!--过滤掉spring和mybatis的一些无用的debug信息-->
        <logger name="org.springframework" level="INFO">
        </logger>
        <logger name="org.mybatis" level="INFO">
        </logger>
        <logger name="cn.itcast.wanxinp2p.consumer.mapper" level="DEBUG">
        </logger>

        <logger name="springfox" level="INFO">
        </logger>
		<logger name="org.apache.http" level="INFO">
        </logger>
        <logger name="com.netflix.discovery" level="INFO">
        </logger>
        
        <logger name="RocketmqCommon"  level="INFO" >
		</logger>
		
		<logger name="RocketmqRemoting" level="INFO"  >
		</logger>
		
		<logger name="RocketmqClient" level="WARN">
		</logger>

        <logger name="org.dromara.hmily" level="WARN">
        </logger>

        <logger name="org.dromara.hmily.lottery" level="WARN">
        </logger>

        <logger name="org.dromara.hmily.bonuspoint" level="WARN">
        </logger>
		
        <!--OFF   0-->
        <!--FATAL   100-->
        <!--ERROR   200-->
        <!--WARN   300-->
        <!--INFO   400-->
        <!--DEBUG   500-->
        <!--TRACE   600-->
        <!--ALL   Integer.MAX_VALUE-->
        <Root level="DEBUG" includeLocation="true">
            <AppenderRef ref="AsyncAppender"/>
            <AppenderRef ref="Console"/>
            <AppenderRef ref="DebugAppender"/>
        </Root>
    </Loggers>
</Configuration>

original configuration file

Configuration file for the original Blog-Article-API module

server:
  port: 8081
  servlet:
    context-path: /Api

#  port: 63041
#微服务配置
spring:
  application:
    name: Blog-Article-Api #服务名content-api-dev.yaml
  cloud:
    nacos:
      discovery: #服务注册相关配置
        enabled: true
        namespace: dev202305
        group: Blog-Cloud
      config: #配置文件相关配置
        enabled: true
        namespace: dev202305
        group: Blog-Cloud
        file-extension: yaml
        refresh-enabled: true
      server-addr: 172.18.229.62:8848
  profiles:
    active: dev   #环境名

  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/yyy?useSSL=false&serverTimezone=UTC&characterEncoding=utf8&allowPublicKeyRetrieval=true
    username: root
    password: xx

swagger:
  title: "幻梦博客系统"
  description: "幻梦博客,你的选择"
  base-package: com.scm.controller
  enabled: true
  version: 1.0.0
  #        extension-configs:
  contact:
    email: [email protected]
    name: "Lancer"
logging:
  config: classpath:log4j2-dev.xml  # 日志文件配置路径

Original Blog-Article-Service configuration file

#微服务配置
spring:
  application:
    name: Blog-Article-Service #服务名content-api-dev.yaml
  cloud:
    nacos:
      config: #配置文件相关配置
        enabled: true
        namespace: dev202305
        group: Blog-Cloud
        file-extension: yaml
        refresh-enabled: true
      server-addr: 172.18.229.62:8848
  profiles:
    active: dev   #环境名

  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/yyy?useSSL=false&serverTimezone=UTC&characterEncoding=utf8&allowPublicKeyRetrieval=true
    username: root
    password: xx


logging:
  config: classpath:log4j2-dev.xml  # 日志文件配置路径

Use Nocas to split the configuration file

For details, please see the Nacos Simple Learning Tutorial of SpringCloudAliBaba Learning_Guitingting's Blog-CSDN Blog

Enter Nacos and create a new configuration file

Going back to the source code, we split some configuration files in the Service module into nacos

new configuration

Pay attention to the writing specification of Data ID. The content corresponds to the configuration file in the above figure

The content of the configuration file here is the configuration information of a database.

 

We have seen here that the upload has been successful.

Refer to the configuration file in nacos

In the above figure, we have configured the database-related configuration files in nacos

 The Blog-Article-API module configuration file is changed as follows: Note that

 

server:
  port: 8081
  servlet:
    context-path: /Api

#  port: 63041
#微服务配置
spring:
  application:
    name: Blog-Article-Api #服务名content-api-dev.yaml
  cloud:
    nacos:
      discovery: #服务注册相关配置
        enabled: true
        namespace: dev202305
        group: Blog-Cloud
      config: #配置文件相关配置
        enabled: true
        namespace: dev202305
        group: Blog-Cloud
        file-extension: yaml
        refresh-enabled: true
#        配置文件引用
        extension-configs:
          - data-id: Blog-Article-Service-${spring.profiles.active}.yaml
            group: Blog-Cloud
            refresh: true
      server-addr: 172.18.229.62:8848
  profiles:
    active: dev   #环境名

swagger:
  title: "幻梦博客系统"
  description: "幻梦博客,你的选择"
  base-package: com.scm.controller
  enabled: true
  version: 1.0.0
  contact:
    email: [email protected]
    name: "Lancer"



logging:
  config: classpath:log4j2-dev.xml  # 日志文件配置路径

Start project testing

Share configuration file operations in nacos

Create a new swagger configuration in nacos as the swagger configuration for all projects

Success is as follows.

 The Blog-Article-API module configuration file is changed as follows: Note that

 

server:
  port: 8081
  servlet:
    context-path: /Api

#  port: 63041
#微服务配置
spring:
  application:
    name: Blog-Article-Api #服务名content-api-dev.yaml
  cloud:
    nacos:
      discovery: #服务注册相关配置
        enabled: true
        namespace: dev202305
        group: Blog-Cloud
      config: #配置文件相关配置
        enabled: true
        namespace: dev202305
        group: Blog-Cloud
        file-extension: yaml
        refresh-enabled: true
#        配置文件引用
        extension-configs:
          - data-id: Blog-Article-Service-${spring.profiles.active}.yaml
            group: Blog-Cloud
            refresh: true
        shared-configs:
          - data-id: Swagger-${spring.profiles.active}.yaml
            group: Blog-Cloud-Common
            refresh: true
      server-addr: 172.18.229.62:8848
  profiles:
    active: dev   #环境名




logging:
  config: classpath:log4j2-dev.xml  # 日志文件配置路径

Start project testing

--"access

http://localhost:8081/Api/swagger-ui.html

We use the same method to extract the log configuration

Create a new log configuration file

Success is as follows

 The Blog-Article-API module configuration file is changed as follows: Note that

 

server:
  port: 8081
  servlet:
    context-path: /Api

#  port: 63041
#微服务配置
spring:
  application:
    name: Blog-Article-Api #服务名content-api-dev.yaml
  cloud:
    nacos:
      discovery: #服务注册相关配置
        enabled: true
        namespace: dev202305
        group: Blog-Cloud
      config: #配置文件相关配置
        enabled: true
        namespace: dev202305
        group: Blog-Cloud
        file-extension: yaml
        refresh-enabled: true
#        配置文件引用
        extension-configs:
          - data-id: Blog-Article-Service-${spring.profiles.active}.yaml
            group: Blog-Cloud
            refresh: true
        shared-configs:
          - data-id: Swagger-${spring.profiles.active}.yaml
            group: Blog-Cloud-Common
            refresh: true
          - data-id: Logging-${spring.profiles.active}.yaml
            group: Blog-Cloud-Common
            refresh: true  
      server-addr: 172.18.229.62:8848
  profiles:
    active: dev   #环境名

 Run project tests

ok, the log is output normally

END 

Please leave a message for more questions

Guess you like

Origin blog.csdn.net/qq_53679247/article/details/130536482