博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringCloud-Config分布式配置中心
阅读量:4353 次
发布时间:2019-06-07

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

一,分布式系统面临的配置问题

二,

 

 

 三,创建项目

1.pom.xml

4.0.0
com.atguigu.springcloud
microservicecloud
0.0.1-SNAPSHOT
microservicecloud-config-3344
org.springframework.cloud
spring-cloud-config-server
org.eclipse.jgit
org.eclipse.jgit
4.10.0.201712302008-r
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-hystrix
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-starter-config
org.springframework.boot
spring-boot-starter-jetty
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.springframework
springloaded
org.springframework.boot
spring-boot-devtools

2.application.yml

server:   port: 3344   spring:  application:    name:  microservicecloud-config  cloud:    config:      server:        git:          uri: git@github.com:zzyybs/microservicecloud-config.git #GitHub上面的git仓库名字

 

 3.主启动类

package com.atguigu.springcloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication@EnableConfigServerpublic class Config_3344_StartSpringCloudApp{    public static void main(String[] args)    {        SpringApplication.run(Config_3344_StartSpringCloudApp.class, args);    }}

4.修改host文件,添加映射

5.测试是否能从GitHub上获取内容

 

 

访问不存在的配置:

 其他写法:

 

四,测试其他服务能否调通配置中心

1.新建yml文件   UTF-8格式,提交到GitHub中

 

2.新建客户端工程

4.0.0
com.atguigu.springcloud
microservicecloud
0.0.1-SNAPSHOT
microservicecloud-config-client-3355
org.springframework.cloud
spring-cloud-starter-config
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-hystrix
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-starter-config
org.springframework.boot
spring-boot-starter-jetty
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.springframework
springloaded
org.springframework.boot
spring-boot-devtools

3.yml文件  bootstrap.yml

spring:  cloud:    config:      name: microservicecloud-config-client #需要从github上读取的资源名称,注意没有yml后缀名      profile: test   #本次访问的配置项      label: master         uri: http://config-3344.com:3344  #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址

 

4.application.yml

spring:  application:    name: microservicecloud-config-client

5.

 

 6.ConfigClientRest.java

package com.atguigu.springcloud.rest;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class ConfigClientRest{    @Value("${spring.application.name}")    private String applicationName;    @Value("${eureka.client.service-url.defaultZone}")    private String eurekaServers;    @Value("${server.port}")    private String port;    @RequestMapping("/config")    public String getConfig()    {        String str = "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port;        System.out.println("******str: " + str);        return "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port;    }}

 

7.主启动类

package com.atguigu.springcloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class ConfigClient_3355_StartSpringCloudApp{    public static void main(String[] args)    {        SpringApplication.run(ConfigClient_3355_StartSpringCloudApp.class, args);    }}

8.测试

 

转载于:https://www.cnblogs.com/zqLoveSym/p/10853957.html

你可能感兴趣的文章
Git clone 报错 128
查看>>
在Python中执行普通除法
查看>>
编译原理(第三版) 语法分析器
查看>>
c# 动态绘制直线和曲线
查看>>
MRP 參數設置
查看>>
Spring理解?
查看>>
删除无限循环的文件夹-删除递归文件夹
查看>>
最近使用SSH开发所遇问题总结
查看>>
Flash报表控件(FusionCharts) 使用
查看>>
本周总结
查看>>
【学习笔记】Lucas定理
查看>>
CS游戏2--三次杀人机会,警察不能杀人
查看>>
oracle行转列及分组排序
查看>>
Java技术面试
查看>>
P2330 [SCOI2005] 繁忙的都市 洛谷
查看>>
phpcms v9 配置sphinx全文索引教程
查看>>
使用C#和Java发送邮件(转载)
查看>>
转载:32位Win7使用4G内存
查看>>
Hadoop中eclipse 插件的编译 笔记四
查看>>
MariaDB备份之XtraBackup
查看>>