SpringCloud-ServerConfig center server configuration

ServerConfig

SpringCloud components, ServerConfig is to configure the service center, components for unified management of project configuration

principle

Springboot by starting a service (server-config), configure git repository address (also svn or other), through configured file address, access to the configuration file. Other micro-service access configuration does not require access to git, you can only access by ServerConfig.

Set up

Premise summary, this building use ide: Spring Tool Suite 4

Version: 4.3.0.RELEASE
Build Id: 201906200901

Installed maven 3.6.3, and configure the network address of the warehouse unobstructed maven, some foreign address of the warehouse may not speed, slow download dependence.

Mirroring the domestic warehouse but faster https://www.cnblogs.com/Narule/p/12595960.html

file -> new -> Spring Starter Project

pom-dependent

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.wunanyu</groupId>
	<artifactId>Config</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Config</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

Profiles

application.yml

server:
  port: 8888  #	服务端口号
spring:
  application:
    name: server-config  #服务名
  cloud:
    config:
      server:
        git:
          uri: /usr/data/git/cloud/config  #linux配置文件地址
          # 这里可以配置文件路径,也可以配置服务器路径,windows需要添加//  
          # 如uri: file://${user.home}/config-repo
          # github  uri:https://github.com/repo

#
#logging:
#config: src/main/resources/logback-spring.xml
log:
  path: log  #日志路径

More details may be found in https://www.cnblogs.com/hellxz/p/9306507.html

logback

Profile has been configured, can be used directly, Not here logback details, this is not the focus

<?xml version="1.0" encoding="UTF-8"?>
<!--  
配置说明

%6 表示字段最小长度为6,格式右对齐 		[	 右对齐 ]
%-64 表示最小长度64,并且左对齐		[左对齐	]
%6.32 表示最小长度6,最大长度32		[左对齐	]
-->
<configuration scan="true" scanPeriod="60 seconds"
	debug="false">
	<!-- 默认读取application.yml 配置 -->
	<springProperty scope="contex" name="app" source="spring.application.name"></springProperty>
	<springProperty scope="contex" name="log.path" source="log.path"></springProperty>
 	<!-- <property name="log.path" value="log" /> -->
	<property name="LOG_PATTERN"
		value="%d -%magenta(%6(${PID:- }))- [%-5(${app})] [%6thread] %-5level %cyan(%-64logger{128}) - %msg%n" />
		<!-- 时间	进程						 线程 					日志级别 				输出对象 			消息内容 -->
		<!-- 彩色 -->
		<!-- <property name="LOG_PATTERN" value="%d -%magenta(${PID:- })- [%16thread] %customcolor(%-5level) %cyan(%-64logger{128}) - %msg%n" /> -->
		<!-- 黑白日志 -->
		<!-- <property name="LOG_PATTERN" value="%d -%6(${PID:- })- [%16thread] %-5level %-64logger{128} - %msg%n" /> -->
	
	<!-- <contextName>${app}</contextName> -->
	<!--输出到控制台 -->
	<appender name="console"
		class="ch.qos.logback.core.ConsoleAppender">
		<!-- 级别过滤 -->
		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
			<!-- 级别低于INFO 的日志不会被打印 -->
			<level>INFO</level>
		</filter>
		<!-- 打印模板 -->
		<encoder>
			<pattern>${LOG_PATTERN}</pattern>
			<!-- <pattern>%d -%magenta(${PID:- })- [%16thread] %customcolor(%-5level) 
				%cyan(%-64logger{128}) - %msg%n</pattern> -->
		</encoder>
	</appender>

	<!--输出到文件 -->
	<!-- all 日志文件,级别高于info 的全部打印 -->
	<appender name="all-file"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${log.path}/${app}-all.log</file>
		<!-- ThresholdFilter级别过滤 级别大于 info 的才会输出 -->
		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
			<level>INFO</level>
		</filter>
		<rollingPolicy
			class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log.path}/all/${app}-all-logging-%d{yyyy-MM-dd}.log
			</fileNamePattern>
			<!-- <fileNamePattern>${log.path}/spring-boot-${log.name}-logging.%d{yyyy-MM-dd}.all-log.zip</fileNamePattern> -->
			<!-- 日志保存周期 -->
			<maxHistory>30</maxHistory>
			<!-- 总大小 -->
			<totalSizeCap>1GB</totalSizeCap>
		</rollingPolicy>
		<encoder>
			<pattern>${LOG_PATTERN}</pattern>
		</encoder>
	</appender>

	<!-- info 日志文件 只打印info级别信息 -->
	<appender name="info-file"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${log.path}/${app}-info.log</file>
		<!-- 只打印唯一种级别的日志 可以用 LevelFilter 配置 -->
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>INFO</level>
			<!--匹配到就允许 -->
			<onMatch>ACCEPT</onMatch>
			<!--没匹配到就禁止 -->
			<onMismatch>DENY</onMismatch>
		</filter>
		<rollingPolicy
			class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log.path}/info/${app}-info-logging-%d{yyyy-MM-dd}.log
			</fileNamePattern>
			<!-- 日志保存周期 -->
			<maxHistory>30</maxHistory>
			<!-- 总大小 -->
			<totalSizeCap>1GB</totalSizeCap>
		</rollingPolicy>
		<encoder>
			<pattern>${LOG_PATTERN}</pattern>
		</encoder>
	</appender>

	<!-- warn 日志文件 只打印warn级别信息 -->
	<appender name="warn-file"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${log.path}/${app}-warn.log</file>
		<!-- 只打印唯一种级别的日志 可以用 LevelFilter 配置 -->
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>WARN</level>
			<!--匹配到就允许 -->
			<onMatch>ACCEPT</onMatch>
			<!--没匹配到就禁止 -->
			<onMismatch>DENY</onMismatch>
		</filter>
		<rollingPolicy
			class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log.path}/warn/${app}-warn-logging-%d{yyyy-MM-dd}.log
			</fileNamePattern>
			<!-- 日志保存周期 -->
			<maxHistory>30</maxHistory>
			<!-- 总大小 -->
			<totalSizeCap>1GB</totalSizeCap>
		</rollingPolicy>
		<!-- <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 
			<fileNamePattern>${log.path}/spring-boot-${log.name}-logging.%d{yyyy-MM-dd}.warn-log.zip</fileNamePattern> 
			日志保存周期 <maxHistory>30</maxHistory> 总大小 <totalSizeCap>1GB</totalSizeCap> </rollingPolicy> -->
		<encoder>
			<pattern>${LOG_PATTERN}</pattern>
		</encoder>
	</appender>

	<!-- error 日志文件 只打印error级别信息 -->
	<appender name="error-file"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${log.path}/${app}-error.log</file>
		<!-- <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> 
			<onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> -->
		<!--如果只是想要 Error 级别的日志,那么需要过滤一下,默认是 info 级别的,ThresholdFilter -->
		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
			<level>Error</level>
		</filter>
		<rollingPolicy
			class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log.path}/error/${app}-error-logging-%d{yyyy-MM-dd}.log
			</fileNamePattern>
			<!-- 日志保存周期 -->
			<maxHistory>30</maxHistory>
			<!-- 总大小 -->
			<totalSizeCap>1GB</totalSizeCap>
		</rollingPolicy>
		<encoder>
			<pattern>${LOG_PATTERN}</pattern>
		</encoder>
	</appender>






	<!-- root 指定打印日志最低级别 -->
	<root level="info">
		<appender-ref ref="console" />
		<appender-ref ref="all-file" />
		<!-- <appender-ref ref="trace-file" /> -->
		<!-- <appender-ref ref="debug-file" /> -->
		<appender-ref ref="info-file" />
		<appender-ref ref="warn-file" />
		<appender-ref ref="error-file" />
	</root>

	<!-- logback为java中的包 -->
	<!-- <logger name="com.baiding"/> -->
	<!-- java中的包 -->
	<!-- <logger name="com.baiding" level="warn" addtivity="false"> <appender-ref 
		ref="console" /> </logger> -->


	<!-- <appender name="trace-file" class="ch.qos.logback.core.rolling.RollingFileAppender"> 
		<file>${log.path}/spring-boot-${log.name}-trace.log</file> <filter class="ch.qos.logback.classic.filter.LevelFilter"> 
		<level>TRACE</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> 
		</filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 
		<fileNamePattern>${log.path}/spring-boot-${log.name}-logging.%d{yyyy-MM-dd}.trace-log.zip</fileNamePattern> 
		日志保存周期 <maxHistory>30</maxHistory> 总大小 <totalSizeCap>1GB</totalSizeCap> </rollingPolicy> 
		<encoder> <pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} 
		- %msg%n</pattern> </encoder> </appender> -->

	<!-- <appender name="debug-file" class="ch.qos.logback.core.rolling.RollingFileAppender"> 
		<file>${log.path}/spring-boot-${log.name}-debug.log</file> <filter class="ch.qos.logback.classic.filter.LevelFilter"> 
		<level>DEBUG</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> 
		</filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 
		<fileNamePattern>${log.path}/spring-boot-${log.name}-logging.%d{yyyy-MM-dd}.debug-log.zip</fileNamePattern> 
		日志保存周期 <maxHistory>30</maxHistory> 总大小 <totalSizeCap>1GB</totalSizeCap> </rollingPolicy> 
		<encoder> <pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} 
		- %msg%n</pattern> </encoder> </appender> -->

</configuration>

Startup class

package com.wunanyu.cloud.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication  //springboot启动类注解
@EnableConfigServer  //SpringCloud-ServerConfig配置中心服务注解,表示这是配置服务中心
/**
 * @author Narule
 */
public class ConfigApplication {

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

Start main method of this class, see the print console console log is started successfully, successfully started, you can pass

localhost: 8888 / eureka-dev.yml view configuration information, as long as there is this file to configure address

configserver Profile Rule

/ { 应用名 } / { 环境名 } [ / { 分支名 } ]
/ { 应用名 } - { 环境名 }.yml
/ { 应用名 } - { 环境名 }.properties
/ { 分支名 } / { 应用名 } - { 环境名 }.yml
/ { 分支名 } / { 应用名 } - { 环境名 }.properties

If a springboot service called eureka, eureka configuration is as follows

bootstrap.yml

spring:
  application:
    name: eureka #应用名
  cloud:
    config:
     uri: localhost:8888 #配置中心的访问地址 这不是配置文件的地址,是配置服务server-config的地址
     profile: dev #环境
     label: master #分支
management:
  endpoints:
    web:
      exposure:
        include:
        - "*"
        
eureka:
  server:
    renewal-percent-threshold: 0.45
    
log:
  path: log

Then he will read

eureka-dev.yml name, git branch for the master files (default)

Guess you like

Origin www.cnblogs.com/Narule/p/12596052.html
Recommended