RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
SpringCloudConfig入门-创新互联

1.    简介

创新互联专业IDC数据服务器托管提供商,专业提供成都服务器托管,服务器租用,雅安移动机房雅安移动机房,成都多线服务器托管等服务器托管服务。

Spring Cloud Config 是用来为分布式系统中为微服务应用提供集中化的外部配置支持,主要分为Spring Cloud Config Server(服务器端)和Spring Cloud Config Client(客户端)。

2.    Spring Cloud Config Server

Spring Cloud Config Server为服务器端,它是一个单独的微服务应用,用来连接配置仓库(本文使用的是git仓库)并为客户端获取配置信息。

1.     首先,创建config server工程

打开http://start.spring.io/

Spring Cloud Config 入门

填写好Group、Artifact。选择依赖的包有Config Server。

对应的pom.xml为

     

         org.springframework.cloud

         spring-cloud-config-server

     

     

         org.springframework.boot

         spring-boot-starter-web

     

     

         org.springframework.boot

         spring-boot-starter-test

         test

     

2.     将下载下来的项目导入Eclipse。

目录结构如下,我这里面新增了bootstrap.yml

Spring Cloud Config 入门

3.     修改配置

在application.yml中添加

      server:

     port: 8080

在bootstrap.yml中添加

spring:

 cloud:

   config:

     server:

       git:

         uri: https://github.com/DevinXin/config-repo

注意:ConfigServerApplication,Spring Boot 启动类上需要添加@EnableConfigServer注解

Spring Cloud Config 入门

4.     启动configServer

通过访问http://localhost:8080/master/foobar-dev.properties可以读到git上的配置文件。

Spring Cloud Config 入门

3.    Spring Cloud Config Client

Spring Cloud Config Client为客户端,客户端通过配置连接服务器端,从服务器端加载配置信息。

1.     创建config client工程

同config server工程创建一样,依赖需要web,Config Client

对应的pom.xml为:

     

         org.springframework.cloud

         spring-cloud-starter-config

     

     

         org.springframework.boot

         spring-boot-starter-web

     

     

         org.springframework.boot

         spring-boot-starter-test

         test

     

  

2.     修改配置

application.yml配置为

   server:

   port: 8081

bootstrap.yml配置为

    spring:

     cloud:

       config:

         uri: http://localhost:8080/

         profile: dev

         label: master

     application:

       name: foobar

3.     写一个Controller

Spring Cloud Config 入门

4.     启动config Client。

访问http://localhost:8081/configServer

可以从config Server中获取到配置文件中的值。

Spring Cloud Config 入门

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章题目:SpringCloudConfig入门-创新互联
转载源于:http://scyingshan.cn/article/dpeijo.html