2017-03-23 27 views
0

我寫了自己的配置服務器來集中配置管理並使用API​​公開應用程序配置。現在我想從彈簧和彈簧引導應用程序中使用配置屬性。但我無法找出正確的方法。我嘗試將我的配置服務器客戶端代碼放置在監聽應用程序上下文啓動事件並從配置服務器讀取配置。但我無法將此配置注入其他bean。如何使用配置服務器外部化spring應用程序配置?

如何從配置服務器(使用休息客戶端)讀取應用程序配置並將此讀取配置注入應用程序環境以進一步處理?

回答

0

創建一個應用程序,用於配置和您在配置服務器

spring: 
    cloud: 
    config: 
     server: 
     native: 
      search-locations: classpath:/shared 
    profiles: 
    active: native 


security: 
    user: 
    password: pass 

server: 
    port: 8888 

應用

@SpringBootApplication 
@EnableConfigServer 
public class ConfigApplication { 

    public static void main(String[] args) throws Exception { 
     SpringApplication.run(ConfigApplication.class, args); 
    } 
} 

application.yml並創建SRC共享文件夾/主/資源/共享和您的服務。像這樣的yml

spring: 
    security: 
     basic: 
      enabled: false 

server: 
    port: 8083 

配置服務器中的maven pom.xml

<parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.5.1.RELEASE</version> 
    </parent> 
    <dependencyManagement> 
     <dependencies> 
      <dependency> 
       <groupId>org.springframework.cloud</groupId> 
       <artifactId>spring-cloud-dependencies</artifactId> 
       <version>Camden.SR5</version> 
       <type>pom</type> 
       <scope>import</scope> 
      </dependency> 
     </dependencies> 
    </dependencyManagement> 
    <dependencies> 
     <dependency> 
      <groupId>org.springframework.cloud</groupId> 
      <artifactId>spring-cloud-config-server</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-security</artifactId> 
     </dependency> 
    </dependencies> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
       <configuration> 
        <finalName>config</finalName> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
在您的服務春季啓動應用程序

添加/src/main/resources/bootstrap.yml

這樣

spring: 
    application: 
    name: your-service 
    cloud: 
    config: 
     uri: http://localhost:8888 
     fail-fast: true 
     username: user 
     password: pass 

,並添加這種依賴於您的服務

<dependency> 
    <groupId>org.springframework.cloud</groupId> 
    <artifactId>spring-cloud-starter-config</artifactId> 
</dependency> 

第一次運行配置服務器,然後您的彈簧啓動服務

+0

嗨阿里,我沒有使用spring-cloud-config服務器,但我有我自己的配置服務器和配置客戶端。 – user34567

+0

爲什麼你不使用雲配置?你想寫一個項目,如雲配置:D –

+0

有幾個要求我需要編寫我自己的配置服務,當然我不喜歡重新發明輪子,但是這使我更多地控制數據模型,代碼和功能。 – user34567

相關問題