SpringCloud的config使用

官网下载安装Git-2.17.1.2-64-bit.exe

注册GitHub账号

GitHub设置公钥,参考https://www.cnblogs.com/qcwblog/p/5709720.html

https://www.cnblogs.com/joyho/articles/4062574.html

https://jingyan.baidu.com/album/3aed632ed01dd17010809127.html?picindex=7

 

在D盘建立一个文件夹D:\44\mySpringCloud

在文件夹中执行如下命令:git clone git@.......

在D:\44\mySpringCloud\microservicecloud-config下新建一个application.yml文件,必须为utf-8格式

spring:  
  profiles: 
    active: dev
---
spring:  
  profiles: dev  #开发环境
  application:
    name: microservicecloud-config-totto-dev
---
spring: 
  profiles: test  #测试环境
  application:
    name: microservicecloud-config-totto-test        
# 请保存为utf-8格式

执行如下命令到GitHub

git config --global user.email "[email protected]"

git config --global user.name "Your Name"

git add .

git commit -m "init file"

git push origin master

 

创建module     microservicecloud-config-3344

pom.xml


		
		
			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
		
	

若报错,请将下面删掉

 


		
			org.eclipse.jgit
			org.eclipse.jgit
			4.10.0.201712302008-r
		

application.yml

server: 
  port: 3344 
  
spring:
  application:
    name:  microservicecloud-config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/xxxx.git #GitHub上面的git仓库名字
          username: xxx
          password: xxx
          force-pull: true

启动类

@SpringBootApplication
@EnableConfigServer
public class Config_3344_StartSpringCloudApp
{
	public static void main(String[] args)
	{
		SpringApplication.run(Config_3344_StartSpringCloudApp.class, args);
	}
}

修改系统C:\Windows\System32\drivers\etc  下的hosts文件

127.0.0.1  config-3344.com

浏览器访问http://config-3344.com:3344/application-test.yml

http://config-3344.com:3344/application-dev.yml

 

二、

在D:\44\mySpringCloud\microservicecloud-config目录下新建microservicecloud-config-client.yml

spring: 
  profiles:
    active:
    - dev
---
server: 
  port: 8201
spring: 
  profiles: dev
  application:
    name: microservicecloud-config-client
---
server: 
  port: 8202
spring: 
  profiles: test
  application:
    name: microservicecloud-config-client

执行如下命令上传到GitHub

git add .

git commit -m "test config"

git push origin master

新建module  microservicecloud-config-client-3355

pom.xml


		
		
			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
		
	

修改系统的hosts文件

127.0.0.1  client-config.com

bootstrap.yml(系统级别)

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

application.yml(用户级别)

spring:
  application:
    name: microservicecloud-config-client

controller

@RestController
public class ConfigClientRest
{

	@Value("${spring.application.name}")
	private String applicationName;


	@Value("${server.port}")
	private String port;

	@RequestMapping("/config")
	public String getConfig()
	{
		String str = "applicationName: " + applicationName + "\t port: " + port;
		System.out.println("******str: " + str);
		return "applicationName: " + applicationName + "\t port: " + port;
	}
}

启动类

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

浏览器访问http://client-config.com:8202/config

 

修改bootstrap.yml

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

浏览器访问http://client-config.com:8201/config

 

总结:microservicecloud-config-client-3355通过microservicecloud-config-3344访问到GitHub上的microservicecloud-config-client.yml,从而把yml中的配置加载给microservicecloud-config-client-3355

三:

在D:\44\mySpringCloud\microservicecloud-config下新建一个microservicecloud-config-eureka-client.yml文件

spring: 
  profiles: 
    active: dev
---
server: 
  port: 7001
  
spring:
   profiles: dev
   application:
    name: microservicecloud-config-eureka-client 
  
eureka: 
  instance:  
    hostname: eureka7001.com #eureka7001.com #eureka服务端的实例名称,单机版填localhost,集群版填eureka7001.com
  client:  
    register-with-eureka: false     #false表示不向注册中心注册自己。
    fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url: 
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #单机版设置,设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
---
server: 
  port: 7002
  
spring:
   profiles: test
   application: 
    name: microservicecloud-config-eureka-client 
  
eureka:  
  instance:   
    hostname: eureka7002.com #eureka7001.com #eureka服务端的实例名称,单机版填localhost,集群版填eureka7001.com
  client:   
    register-with-eureka: false     #false表示不向注册中心注册自己。
    fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url: 
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #单机版设置,设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。

新建microservicecloud-config-dept-client.yml

spring: 
  profiles: 
    active: dev
---
server:
  port: 8001
  
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置文件所在路径
  type-aliases-package: top.totto.springcloud.entities    # 所有Entity别名类所在包
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml                       # mapper映射文件
    
spring:
   profiles: dev
   application:
    name: microservicecloud-dept 
   datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: com.mysql.cj.jdbc.Driver              # mysql驱动包
    url: jdbc:mysql://localhost:3306/cloudDB01?serverTimezone=GMT%2B8             # 数据库名称
    username: root
    password: root
    dbcp2:
      min-idle: 5                                           # 数据库连接池的最小维持连接数
      initial-size: 5                                       # 初始化连接数
      max-total: 5                                          # 最大连接数
      max-wait-millis: 200                                  # 等待连接获取的最大超时时间

eureka:
  client: #客户端注册进eureka服务列表内
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka #单机版设置
#      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/   #集群版设置   
  instance:
    instance-id: microservicecloud-dept8001
    prefer-ip-address: true     #访问路径可以显示IP地址     
 
info: 
  app.name: totto-microservicecloud-config1
  company.name: www.totto.top
  build.artifactId: $project.artifactId$
  build.version: $project.version$   
---
server:
  port: 8001
  
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置文件所在路径
  type-aliases-package: top.totto.springcloud.entities    # 所有Entity别名类所在包
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml                       # mapper映射文件
    
spring:
   profiles: test
   application:
    name: microservicecloud-dept 
   datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: com.mysql.cj.jdbc.Driver              # mysql驱动包
    url: jdbc:mysql://localhost:3306/cloudDB02?serverTimezone=GMT%2B8             # 数据库名称
    username: root
    password: root
    dbcp2:
      min-idle: 5                                           # 数据库连接池的最小维持连接数
      initial-size: 5                                       # 初始化连接数
      max-total: 5                                          # 最大连接数
      max-wait-millis: 200                                  # 等待连接获取的最大超时时间

eureka:
  client: #客户端注册进eureka服务列表内
    service-url: 
      defaultZone: http://eureka7002.com:7002/eureka #单机版设置
#      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/   #集群版设置   
  instance:
    instance-id: microservicecloud-dept8001
    prefer-ip-address: true     #访问路径可以显示IP地址     
 
info: 
  app.name: totto-microservicecloud-config2
  company.name: www.totto.top
  build.artifactId: $project.artifactId$
  build.version: $project.version$ 

执行如下命令,提交到GitHub

git status

git add .

git commit -m "two new file"

git push origin master

新建module   microservicecloud-config-eureka-client-7001

pom.xml


		
		
			org.springframework.cloud
			spring-cloud-starter-config
		
		
			org.springframework.cloud
			spring-cloud-starter-eureka-server
		
		
		
			org.springframework
			springloaded
		
		
			org.springframework.boot
			spring-boot-devtools
		
	

bootstrap.yml

spring: 
  cloud: 
    config: 
      name: microservicecloud-config-eureka-client     #需要从github上读取的资源名称,注意没有yml后缀名
      profile: dev 
      label: master 
      uri: http://config-3344.com:3344      #SpringCloudConfig获取的服务地址

application.yml

spring:
  application:
    name: microservicecloud-config-eureka-client

启动类

@SpringBootApplication
@EnableEurekaServer
public class Config_Git_EurekaServerApplication
{
	public static void main(String[] args)
	{
		SpringApplication.run(Config_Git_EurekaServerApplication.class, args);
	}
}

新建module   microservicecloud-config-dept-client-8001

pom.xml


		
		
			org.springframework.cloud
			spring-cloud-starter-config
		
		
			org.springframework.boot
			spring-boot-starter-actuator
		
		
			org.springframework.cloud
			spring-cloud-starter-eureka
		
		
			com.atguigu.springcloud
			microservicecloud-api
			${project.version}
		
		
			junit
			junit
		
		
			mysql
			mysql-connector-java
		
		
			com.alibaba
			druid
		
		
			ch.qos.logback
			logback-core
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
		
		
			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
		
	

bootstrap.yml

spring:
  cloud:
    config:
      name: microservicecloud-config-dept-client #需要从github上读取的资源名称,注意没有yml后缀名
      #profile配置是什么就取什么配置dev or test
      profile: dev
      #profile: test
      label: master
      uri: http://config-3344.com:3344  #SpringCloudConfig获取的服务地址

application.yml

spring:
  application:
    name: microservicecloud-config-dept-client

启动类

@SpringBootApplication
@EnableEurekaClient //本服务启动后会自动注册进eureka服务中
@EnableDiscoveryClient //服务发现
public class DeptProvider8001_Config_App
{
	public static void main(String[] args)
	{
		SpringApplication.run(DeptProvider8001_Config_App.class, args);
	}
}

开启microservicecloud-config-3344     microservicecloud-config-eureka-client-7001

microservicecloud-config-dept-client-8001

浏览器访问http://localhost:8001/dept/list

修改microservicecloud-config-dept-client-8001中的bootstrap.yml

spring:
  cloud:
    config:
      name: microservicecloud-config-dept-client #需要从github上读取的资源名称,注意没有yml后缀名
      #profile配置是什么就取什么配置dev or test
#      profile: dev
      profile: test
      label: master
      uri: http://config-3344.com:3344  #SpringCloudConfig获取的服务地址

浏览器再次访问http://localhost:8001/dept/list

可发现数据库切换了

你可能感兴趣的:(SpringCloud)