spring-cloud-config-server 服务器搭建这篇文章介绍了如何搭建配置服务器及如何使用配置服务器加载配置。但是上面的都是针对单机的配置服务器,虽然集群的配置服务器基本没用到过,但是这里贴一下如何搭建及使用集群的配置服务器
相比较单机的config服务器,集群的服务器需要将服务注册到eureka上去(eureka服务器搭建参考spring-cloud-eureka服务发现注册中心及spring-cloud微服务),所以需要导入spring-cloud-starter-netflix-eureka-client依赖,并配置eureka client
pom.xml 与单机的配置服务器多了spring-cloud-starter-netflix-eureka-client依赖(注意其与spring-cloud-server的版本)
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wl.config</groupId>
<artifactId>config-service</artifactId>
<version>1.0-SNAPSHOT</version>
<name>config-service</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<daylife-version>1.0-SNAPSHOT</daylife-version>
<MainClass>com.wl.config.Application</MainClass>
<spring-cloud-eureka-client-version>1.4.5.RELEASE</spring-cloud-eureka-client-version>
<spring-boot-version>1.5.7.RELEASE</spring-boot-version>
<spring-cloud-starter-config-version>1.2.2.RELEASE</spring-cloud-starter-config-version>
<slf4j-api-version>1.7.5</slf4j-api-version>
<!-- groovy -->
<groovy-all-version>2.4.5</groovy-all-version>
<!-- 测试 -->
<junit.version>4.12</junit.version>
<groovy-all-version>2.4.5</groovy-all-version>
<spock-core-version>1.1-groovy-2.4</spock-core-version>
</properties>
<dependencies>
<!-- spring boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot-version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>${spring-cloud-eureka-client-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
<!-- 依赖 spring-web -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>${spring-cloud-starter-config-version}</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-config-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>${spring-cloud-starter-config-version}</version>
</dependency>
<!---日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-api-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- groovy -->
<!-- https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-all -->
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy-all-version}</version>
</dependency>
<!-- 测试依赖 scope test 不会打进jar包 -->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.spockframework/spock-core -->
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>${spock-core-version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${spring-boot-version}</version>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-version}</version>
<configuration>
<mainClass>${MainClass}</mainClass>
<layout>JAR</layout>
</configuration>
<!-- repackage 生成两个 jar.original -->
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 指定maven 打包java 版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<!-- maven 编译打包resource 和 java 目录下所有文件 maven默认资源路径是resources -->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
<include>*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.*</include>
<include>*.*</include>
</includes>
</resource>
</resources>
</build>
</project>
这里有些多余的引用包没有去除掉
application.properties
server.port=8888
#spring.cloud.config.server.git.uri=http://192.168.245.128:8000/config.git
#spring.cloud.config.server.git.password=123456
#spring.cloud.config.server.git.username=git
#spring.cloud.config.server.native.searchLocations=classpath:/config
server.context-path=/
spring.cloud.config.server.native.searchLocations=file:///D:\\workspace\\wl\\study\\config
spring.profiles.active=native
#=====================================eureka配置
eureka.client.serviceUrl.defaultZone=http\://localhost\:8761/eureka/
spring.application.name=config-server-client
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
启动类(多了EnableDiscoveryClient注解)
package com.wl.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
* Created by wl on 2019/3/7.
*/
@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
HibernateJpaAutoConfiguration.class //不使用数据库
},scanBasePackages = "com.wl")
@EnableConfigServer
@EnableDiscoveryClient
public class Application {
private static final Logger logger = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.setWebEnvironment(true);
app.run(args);
logger.info("application init success");
}
}
下面使用不同的端口来模拟集群
打包
D:\workspace\wl\study\service\config-service>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.wl.config:config-service >--------------------
[INFO] Building config-service 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ config-service ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ config-service ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\workspace\wl\study\service\config-service\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ config-service ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ config-service ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ config-service ---
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ config-service ---
[INFO] Building jar: D:\workspace\wl\study\service\config-service\target\config-service-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.7.RELEASE:repackage (default) @ config-service ---
[INFO] Layout: JAR
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.305 s
[INFO] Finished at: 2019-03-11T21:51:26+08:00
[INFO] ------------------------------------------------------------------------
D:\workspace\wl\study\service\config-service>
指定不同端口执行两次(8888和8999)
D:\workspace\wl\study\service\config-service>cd target
D:\workspace\wl\study\service\config-service\target>java -jar config-service-1.0-SNAPSHOT.jar --server.port=8888
2019-03-11 21:52:58.834 INFO 3624 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@4f8e5cde: startup date [Mon Mar 11 21:52:58 CST 2019]; root of context hierarchy
2019-03-11 21:52:59.083 INFO 3624 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2019-03-11 21:52:59.118 INFO 3624 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$b8e7ea4c] is not eligible
for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.7.RELEASE)
2019-03-11 21:53:00.471 INFO 3624 --- [ main] com.wl.config.Application : The following profiles are active: native
2019-03-11 21:53:00.493 INFO 3624 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@24b1d79b: startup date [Mon Mar 11 21:53:00 CST 2019]; parent: org.springframework.context.annotation.Ann
otationConfigApplicationContext@4f8e5cde
2019-03-11 21:53:01.299 INFO 3624 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'environmentRepository' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; auto
wireCandidate=true; primary=false; factoryBeanName=org.springframework.cloud.config.server.config.EnvironmentRepositoryConfiguration$GitRepositoryConfiguration; factoryMethodName=environmentRepository; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework
/cloud/config/server/config/EnvironmentRepositoryConfiguration$GitRepositoryConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.cloud.config.server.confi
g.EnvironmentRepositoryConfiguration$NativeRepositoryConfiguration; factoryMethodName=environmentRepository; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration$NativeRepositoryConfiguration.
class]]
2019-03-11 21:53:01.464 INFO 3624 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=db22aff0-ac22-3c70-9cea-b90eab66a6cd
2019-03-11 21:53:01.482 INFO 3624 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2019-03-11 21:53:01.546 INFO 3624 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerB
ySpringCGLIB$$b8e7ea4c] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-03-11 21:53:01.870 INFO 3624 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8888 (http)
2019-03-11 21:53:01.881 INFO 3624 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-03-11 21:53:01.882 INFO 3624 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.20
2019-03-11 21:53:01.965 INFO 3624 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-03-11 21:53:01.966 INFO 3624 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1473 ms
2019-03-11 21:53:02.178 INFO 3624 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2019-03-11 21:53:02.182 INFO 3624 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'metricsFilter' to: [/*]
2019-03-11 21:53:02.183 INFO 3624 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-03-11 21:53:02.183 INFO 3624 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-03-11 21:53:02.183 INFO 3624 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-03-11 21:53:02.183 INFO 3624 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2019-03-11 21:53:02.183 INFO 3624 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'webRequestLoggingFilter' to: [/*]
2019-03-11 21:53:02.183 INFO 3624 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'applicationContextIdFilter' to: [/*]
2019-03-11 21:53:02.352 WARN 3624 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2019-03-11 21:53:02.353 INFO 3624 --- [ main] c.n.c.sources.URLConfigurationSource : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2019-03-11 21:53:02.359 WARN 3624 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2019-03-11 21:53:02.359 INFO 3624 --- [ main] c.n.c.sources.URLConfigurationSource : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2019-03-11 21:53:02.704 INFO 3624 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@24b1d79b: startup date [Mon Mar 11 21:53:00 CST 2019]; parent: org.springframework.co
ntext.annotation.AnnotationConfigApplicationContext@4f8e5cde
2019-03-11 21:53:02.776 INFO 3624 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servl
et.http.HttpServletRequest)
2019-03-11 21:53:02.778 INFO 3624 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRe
quest,javax.servlet.http.HttpServletResponse)
2019-03-11 21:53:02.784 INFO 3624 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/encrypt],methods=[POST]}" onto public java.lang.String org.springframework.cloud.config.server.encryption.EncryptionController.encrypt(java.lang.String,org.springframework.http.MediaType)
2019-03-11 21:53:02.785 INFO 3624 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/encrypt/{name}/{profiles}],methods=[POST]}" onto public java.lang.String org.springframework.cloud.config.server.encryption.EncryptionController.encrypt(java.lang.String,java.lang.String,java.l
ang.String,org.springframework.http.MediaType)
2019-03-11 21:53:02.785 INFO 3624 --- [ main] s.w.s.m.m.a.RequestMap