spring cloud系列一 搭建配置服务器(分布式配置管理)configserver

来源:互联网 发布:用ps做淘宝主图 编辑:程序博客网 时间:2024/06/06 07:46

分布式配置管理应该是分布式系统和微服务应用的第一步。想象一下如果你有几十个服务或应用需要配置,而且每个服务还分为开发、测试、生产等不同维度的配置,那工作量是相当大的,而且还容易出错。如果能把各个应用的配置信息集中管理起来,使用一套机制或系统来管理,那么将极大的提高系统开发的生产效率,同时也会提高系统开发环境和生产环境运行的一致性。

1、pom.xml

<parent>
   <groupId>org.springframework.boot</groupId>  引入boot
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>1.5.4.RELEASE</version>
</parent>
<dependencyManagement>
   <dependencies>
       <dependency>
           <groupId>org.springframework.cloud</groupId>引入spring cloud
           <artifactId>spring-cloud-dependencies</artifactId>
           <version>Dalston.SR2</version>
           <type>pom</type>
           <scope>import</scope>
       </dependency>
   </dependencies>
</dependencyManagement>


<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>引入config-server jar
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>

2、启动类

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {


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

3、配置文件

=====application.properties

server.port=8888
#spring.cloud.config.server.git.uri=http://git.oschina.net/zhou666/spring-cloud-7simple/tree/master/cloud-config-repo
#spring.cloud.config.server.git.searchPaths=cloud-config-repo
#eureka.client.serviceUrl.defaultZone=http\://localhost\:8761/eureka/,http\://zlhost\:8762/eureka/
spring.application.name=config-server

#使用本地属性文件
spring.profiles.active = native
#属性文件地址,只要指定文件夹的路径
spring.cloud.config.server.native.searchLocations=classpath:/properties/


====configServer-dev.properties

spring.datasource.url: jdbc:mysql://localhost/test
#spring.datasource.url = jdbc:h2:file:~/.h2/testdb
spring.datasource.username: root
spring.datasource.password: root
driver-class-name: com.mysql.jdbc.Driver
spring.jpa.database=MySQL
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy


4、浏览器访问

http://127.0.0.1:8888/configServer/dev



5、客户端使用configserver

pom.xml

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

配置bootstrap.properties

#指定配置中心
spring.cloud.config.uri:http://127.0.0.1:8888

#指定属性文件名 configServer-dev.properties,是有命名规则的
spring.cloud.config.name=configServer
spring.cloud.config.profile=${profile:dev}


阅读全文
1 0
原创粉丝点击