Spring Cloud之分布式配置中心-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

Spring Cloud之分布式配置中心

简介: 用服务的方式来实现 ConfigAppApplication.java package com.packtpub.ConfigApp; import org.springframework.

用服务的方式来实现

ConfigAppApplication.java

package com.packtpub.ConfigApp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class ConfigAppApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(ConfigAppApplication.class).web(true).run(args);
    }
}

application.properties
spring.application.name=config-server
server.port=7001

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

spring.profiles.active=native
spring.cloud.config.server.native.searchLocations=file:D:/temp/

ConfigClientApplication.java

package com.packtpub.ConfigClient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(ConfigClientApplication.class).web(true).run(args);
    }
}

bootstrap.properties

spring.application.name=didispace
server.port=7002

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server

spring.cloud.config.profile=test
#spring.cloud.config.label=master
#spring.cloud.config.uri=http://localhost:7001/

TestController.java

package com.packtpub.ConfigClient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope
@RestController
public class TestController {
    
    @Value("${from}")
    private String from;
    
    @RequestMapping("/from")
    public String from() {
        return this.from;
    }

}

 

 

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享: