一,分布式系统面临的配置问题
二,
三,创建项目
1.pom.xml
4.0.0 com.atguigu.springcloud microservicecloud 0.0.1-SNAPSHOT microservicecloud-config-3344 org.springframework.cloud spring-cloud-config-server org.eclipse.jgit org.eclipse.jgit 4.10.0.201712302008-r org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-hystrix org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-jetty org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test org.springframework springloaded org.springframework.boot spring-boot-devtools
2.application.yml
server: port: 3344 spring: application: name: microservicecloud-config cloud: config: server: git: uri: git@github.com:zzyybs/microservicecloud-config.git #GitHub上面的git仓库名字
3.主启动类
package com.atguigu.springcloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication@EnableConfigServerpublic class Config_3344_StartSpringCloudApp{ public static void main(String[] args) { SpringApplication.run(Config_3344_StartSpringCloudApp.class, args); }}
4.修改host文件,添加映射
5.测试是否能从GitHub上获取内容
访问不存在的配置:
其他写法:
四,测试其他服务能否调通配置中心
1.新建yml文件 UTF-8格式,提交到GitHub中
2.新建客户端工程
4.0.0 com.atguigu.springcloud microservicecloud 0.0.1-SNAPSHOT microservicecloud-config-client-3355 org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-hystrix org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-jetty org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test org.springframework springloaded org.springframework.boot spring-boot-devtools
3.yml文件 bootstrap.yml
spring: cloud: config: name: microservicecloud-config-client #需要从github上读取的资源名称,注意没有yml后缀名 profile: test #本次访问的配置项 label: master uri: http://config-3344.com:3344 #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址
4.application.yml
spring: application: name: microservicecloud-config-client
5.
6.ConfigClientRest.java
package com.atguigu.springcloud.rest;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class ConfigClientRest{ @Value("${spring.application.name}") private String applicationName; @Value("${eureka.client.service-url.defaultZone}") private String eurekaServers; @Value("${server.port}") private String port; @RequestMapping("/config") public String getConfig() { String str = "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port; System.out.println("******str: " + str); return "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port; }}
7.主启动类
package com.atguigu.springcloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class ConfigClient_3355_StartSpringCloudApp{ public static void main(String[] args) { SpringApplication.run(ConfigClient_3355_StartSpringCloudApp.class, args); }}
8.测试