博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring基础 快速入门spring cloud 6 统一配置管理之spring cloud config
阅读量:4036 次
发布时间:2019-05-24

本文共 8952 字,大约阅读时间需要 29 分钟。

分享一下我老师大神的人工智能教程!零基础,通俗易懂!

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                       

这里写图片描述

 

Spring Cloud是Spring总多的Project中的一个,它提供了一整套的工具帮助系统架构师们在进行分布式设计的时候可以拿来即用, 在创建和发布微服务时极为便捷和有效。本系列文章将会使用最简单的例子和最为容易的方式来学习Spring Cloud。本文将会介绍如何引入spring cloud config在微服务的架构中实现统一配置管理。

构成

                           
项目 详细
Config Service Spring Cloud Config:统一配置管理服务
Dashboard Service Hystrix Dashboard
Api Route Service Zuul:Api Gateway
Discovery Service Eureka:服务发现
User Service RESTFUL的用户相关的服务
Org Service RESTFUL的组织相关的服务

这里写图片描述

统一配置管理之服务器侧

Pom详细

   
4.0.0
   
com.liumiaocn.demo.springcloud
   
configservice
   
0.0.1-SNAPSHOT
   
jar
   
configservice
   
Demo project for Spring Boot
   
       
org.springframework.boot
       
spring-boot-starter-parent
       
1.4.3.RELEASE
       
   
   
       
UTF-8
       
UTF-8
       
1.8
   
   
       
           
org.springframework.cloud
           
spring-cloud-starter-eureka
       
       
           
org.springframework.cloud
           
spring-cloud-config-server
       
       
           
org.springframework.boot
           
spring-boot-starter-test
           
test
       
   
   
       
           
               
org.springframework.cloud
               
spring-cloud-dependencies
               
Camden.SR3
               
pom
               
import
           
       
   
   
       
           
               
org.springframework.boot
               
spring-boot-maven-plugin
           
       
   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

configservice

 

需要加入EnableConfigServer注解。

package com.liumiaocn.demo.springcloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableConfigServer@EnableEurekaClientpublic class ConfigserviceApplication {    public static void main(String[] args) {        SpringApplication.run(ConfigserviceApplication.class, args);    }}  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

设定文件

server.port=8888spring.application.name=configserviceeureka.client.serviceUrl.defaultZone=http://localhost:8801/eureka/spring.profiles.active=native  
1
2
3
4
5
                   
项目 详细
server.port configService提供服务所用Port
spring.application.name 向Eureka Server进行注册时使用的服务名
eureka.client.serviceUrl.defaultZone 注意此处的8801端口号需要跟Server端一致。
spring.profiles.active spring cloud config支持本地/svn/git,设定为native表示为本地方式

生成Package

                   
项目 详细
命令 mvn clean package
执行场所 pom所在目录
目标文件所在目录 工程根目录下Target
目标文件名称 configservice-0.0.1-SNAPSHOT.jar

orgservice的设定文件

 

为orgservice准备如下两个设定文件分别对应于dev环境和test环境

orgservice-dev.properties

demo.word=hellodev  
1

orgservice-test.properties

demo.word=hellotest  
1

启动

>启动各个服务后,也启动configservice

Eureka确认

这里写图片描述

orgservice设定文件确认

           
项目 访问方法(URL)
orgservice dev
orgservice test

这里写图片描述

这里写图片描述

统一配置管理之Client侧

Pom

   
4.0.0
   
com.liumiaocn.demo.springcloud
   
orgservice
   
0.0.1-SNAPSHOT
   
jar
   
orgservice
   
Demo project for Spring Boot
   
       
org.springframework.boot
       
spring-boot-starter-parent
       
1.4.3.RELEASE
       
   
   
       
UTF-8
       
UTF-8
       
1.8
   
   
       
           
org.springframework.cloud
           
spring-cloud-starter-config
       
       
           
org.springframework.cloud
           
spring-cloud-starter-eureka
       
       
           
org.springframework.boot
           
spring-boot-starter-web
       
       
           
org.springframework.boot
           
spring-boot-starter-test
           
test
       
   
   
       
           
               
org.springframework.cloud
               
spring-cloud-dependencies
               
Camden.BUILD-SNAPSHOT
               
pom
               
import
           
       
   
   
       
           
               
org.springframework.boot
               
spring-boot-maven-plugin
           
       
   
   
       
           
spring-snapshots
           
Spring Snapshots
           
https://repo.spring.io/snapshot
           
               
true
           
       
       
           
spring-milestones
           
Spring Milestones
           
https://repo.spring.io/milestone
           
               
false
           
       
   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

设定文件

server.port=9002spring.application.name=orgserviceeureka.client.serviceUrl.defaultZone=http://localhost:8801/eureka/spring.cloud.config.uri=http://localhost:8888/spring.cloud.config.profile=test  
1
2
3
4
5
6
                       
项目 详细
server.port orgService提供服务所用Port
spring.application.name 向Eureka Server进行注册时使用的服务名
eureka.client.serviceUrl.defaultZone 注意此处的8801端口号需要跟Server端一致。
spring.cloud.config.profile 选择了服务器侧的orgservice-test.properties作为配置设定文件
spring.cloud.config.uri 服务器侧的URI,此处为

Demo代码

 

为了验证真正读入了服务器侧的设定文件,我们使用Spring的Value注解读入设定文件中的demo.word

package com.liumiaocn.demo.springcloud;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@RestController@RequestMapping("/org")@EnableEurekaClientpublic class OrgserviceApplication {    @Value("${demo.word:default-value}")    String strDemoWord;    @RequestMapping("/detail")    public String Detail(){        return "## The detail information about: Organization : with demo word: "+strDemoWord;    }    public static void main(String[] args) {        SpringApplication.run(OrgserviceApplication.class, args);    }}  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

结果确认

这里写图片描述

总结

>本文介绍了Spring Cloud Config的Server端和Client端如何使用,至次,通读这六篇文章,大体一个小时时间,对有spring基础的初学者应该能够执行搭建结合了Eureka/Zuul/Hystrix/Spring Cloud Config等常用组件进行Sample工程的搭建和基础入门。

但是为了简单起见,比如设定文件只是使用了application.properties,能够不设定大体能运行的选项基本都没有设定,如何更好的设计和搭建分布式的微服务架构,在其他的文章中我们会继续讨论。

           

给我老师的人工智能教程打call!

这里写图片描述
你可能感兴趣的文章
昨夜今晨最大八卦终于坐实——人类首次直接探测到了引力波
查看>>
如何优雅、机智地和新公司谈薪水?
查看>>
为什么读了很多书,却学不到什么东西?
查看>>
长文干货:如何轻松应对工作中最棘手的13种场景?
查看>>
如何用好碎片化时间,让思维更有效率?
查看>>
No.147 - LeetCode1108
查看>>
No.174 - LeetCode1305 - 合并两个搜索树
查看>>
No.175 - LeetCode1306
查看>>
No.176 - LeetCode1309
查看>>
No.182 - LeetCode1325 - C指针的魅力
查看>>
mysql:sql create database新建utf8mb4 数据库
查看>>
mysql:sql alter database修改数据库字符集
查看>>
mysql:sql drop table (删除表)
查看>>
mysql:sql truncate (清除表数据)
查看>>
scrapy:xpath string(.)非常注意问题
查看>>
yuv to rgb 转换失败呀。天呀。谁来帮帮我呀。
查看>>
yuv420 format
查看>>
YUV420只绘制Y通道
查看>>
yuv420 还原为RGB图像
查看>>
LED恒流驱动芯片
查看>>