1.分布式配置中心
1.1 构建一个本地仓库
下面是git本地仓库中的内容,config-respo为仓库,config-dev.properties为配置文件,config-dev.properties中的内容为 test ql
1.2 父项目
只有一个pom.xml
1.3 子项目config-server
pom.xml
主函数中开启config server
配置文件,当github是公开仓库,可以不用输账号密码
1.4 测试config-server
启动程序
输入http://localhost:8888/config/dev/master ,可以看到它找到了对应的github仓库地址和文件中的配置文件内容
1.6 子项目config-client
pom
controller
application.properties
bootstrap.properties
1.7 测试config-client
1.8 总结
整个项目的配置文件:github --> config-server --> config-client.最开始是在github这种公共管理的地方保存,后来被config-server这个项目加载到本地,config-client就可以直接访问这个项目中的配置文件。
2. eureka整合config
2.1改造config-client
主函数增加 @EnableEurekaClient
pom增加:
<!--引入eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.2 config-server改造
application,properties
# erueka
eureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/
主函数增加:@EnableEurekaClient
pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.3 新建一个eureka-server
主函数
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
application.yml
server:
port: 8889
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
pom
<dependencies>
<!--引入eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
2.4 测试
访问 http://localhost:8881/hi
源码:https://github.com/LUK-qianliu/springcloud