基于GitHub搭建SrpingCloudConfig详解(3)

#配置应用名称
spring.cloud.config.name=configTestService
#配置文件的版本
spring.cloud.config.profile=test
#配置服务的地址
spring.cloud.config.uri=http://localhost:8091/
#配置文件所在的分支
spring.cloud.config.label=master

创建一个 WebApplication ,代码如下

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @Author: jipeng
 * @Description:
 * @Date: Created in 2018/6/8 7:03
 */
@SpringBootApplication
public class WebApplication {
    public static void main(String[] args){
        SpringApplication.run(WebApplication.class,args);
    }
}

我们创建一个 TestController

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: jipeng
 * @Description:
 * @Date: Created in 2018/6/8 7:52
 */
@RestController
public class TestController {
    @Value("${configtest.version}")
    private String version;

@RequestMapping("/test")
    public String from() {
        return this.version;
    }
}

都加好了我们启动项目,留意下启动日记,就是启动日记刚开始打印的时候

2018-06-10 21:45:07.420  INFO 8376 --- [          main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: :8091/
2018-06-10 21:45:11.347  INFO 8376 --- [          main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=configTestService, profiles=[dev], label=master, version=9d66adb8d2b7ace9e4933177051b167a7ef49c1a, state=null
2018-06-10 21:45:11.348  INFO 8376 --- [          main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://github.com/jploveslife/springcloudtest.git/configTestService-dev.properties'}]]
2018-06-10 21:45:11.352  INFO 8376 --- [          main] com.ji.WebApplication                    : No active profile set, falling back to default profiles: default
2018-06-10 21:45:11.369  INFO 8376 --- [          main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3a5ecce3: startup date [Sun Jun 10 21:45:11 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@10db82ae
2018-06-10 21:45:11.855  INFO 8376 --- [          main] o.s.cloud.context.scope.GenericScope    : BeanFactory id=0537ae00-a03a-35d0-830a-1277fd4d74d0
2018-06-10 21:45:11.913  INFO 8376 --- [          main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$4fec7b02] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

我会发现刚启动的时候是使用我们的配置服务去获取的配置文件,这里面的原理下次讲

我们来通过postman验证下,看配置有没有过来,我们请求test接口

基于GitHub搭建SrpingCloudConfig详解

返回1.0.0 和我们配置文件中一直,说明配置生效了。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/12748.html