SpringCloud二十四、Config配置演示与策略切换。dependencyManagement

①目前情况:

1  Config服务端配置配置OK且测试通过,我们可以和config+GitHub进行配置修改并获得内容
 
2 此时我们做一个eureka服务+一个Dept访问的微服务,将两个微服务的配置统一由于github获得实现统一配置分布式管理,完成多环境的变更

 

步骤如下。

②Git配置文件本地配置。在本地D:\yunweigongchengshi\microservicecloud-config路径下新建文件microservicecloud-config-eureka-client.yml。

 

 

 

 

③microservicecloud-config-eureka-client.yml的内容是:另存为,必须保存为UTF-8形式。

spring: 
  profiles: 
    active: 
    - dev
---
server: 
  port: 7001 #注册中心占用7001端口,冒号后面必须要有空格
   
spring: 
  profiles: dev
  application:
    name: microservicecloud-config-eureka-client
    
eureka: 
  instance: 
    hostname: eureka7001.com #冒号后面必须要有空格
  client: 
    register-with-eureka: false #当前的eureka-server自己不注册进服务列表中
    fetch-registry: false #不通过eureka获取注册信息
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka/
---
server: 
  port: 7001 #注册中心占用7001端口,冒号后面必须要有空格
   
spring: 
  profiles: test
  application:
    name: microservicecloud-config-eureka-client
    
eureka: 
  instance: 
    hostname: eureka7001.com #冒号后面必须要有空格
  client: 
    register-with-eureka: false #当前的eureka-server自己不注册进服务列表中
    fetch-registry: false #不通过eureka获取注册信息
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka/

 

④在本地D:\yunweigongchengshi\microservicecloud-config路径下新建文件microservicecloud-config-dept-client.yml。

 

⑤microservicecloud-config-dept-client.yml内容是:

spring: 
  profiles:
    active:
    - dev
--- 
server:
  port: 8001
spring: 
   profiles: dev
   application: 
    name: microservicecloud-config-dept-client
   datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/cloudDB01
    username: root
    password: 你的密码
    dbcp2:
      min-idle: 5
      initial-size: 5
      max-total: 5
      max-wait-millis: 200 
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml
  type-aliases-package: com.lss.springcloud.entities
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml

eureka: 
  client: #客户端注册进eureka服务列表内
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka
  instance:
    instance-id: dept-8001.com
    prefer-ip-address: true

info:
  app.name: lss-microservicecloud-springcloudconfig01
  company.name: www.lss.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$
---
server:
  port: 8001
spring: 
   profiles: test
   application: 
    name: microservicecloud-config-dept-client
   datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/cloudDB02
    username: root
    password: 你的密码
    dbcp2:
      min-idle: 5
      initial-size: 5
      max-total: 5
      max-wait-millis: 200  
  
  
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml
  type-aliases-package: com.lss.springcloud.entities
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml

eureka: 
  client: #客户端注册进eureka服务列表内
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka
  instance:
    instance-id: dept-8001.com
    prefer-ip-address: true

info:
  app.name: lss-microservicecloud-springcloudconfig02
  company.name: www.lss.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

 

 

⑥把文件上传到GitHub。

 

 

 

⑦Config版的eureka服务端,新建工程microservicecloud-config-eureka-client-7001。pom文件的内容是:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.lss.springcloud</groupId>
		<artifactId>microservicecloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>microservicecloud-config-eureka-client-7001</artifactId>
	<dependencies>
		<!-- SpringCloudConfig配置 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
		</dependency>
		<!-- 热部署插件 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
	</dependencies>
</project>

 

 

然后maven update一下。

 

 

 

⑧bootstrap.yml和application.yml。创建主启动类Config_Git_EurekaServerApplication

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

 

 

主启动类Config_Git_EurekaServerApplication的内容是:

package com.lss.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication 
@EnableEurekaServer 
public class Config_Git_EurekaServerApplication {

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

	
}

测试:

先启动microservicecloud-config-3344微服务,保证Config总配置是OK的

再启动microservicecloud-config-eureka-client-7001微服务

http://eureka7001.com:7001/   出现eureka主页表示成功启动

 

⑨参考之前的8001拷贝后新建工程microservicecloud-config-dept-client-8001。pom文件,bootstrap.yml文件,application.yml文件。

pom文件的内容是:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.lss.springcloud</groupId>
		<artifactId>microservicecloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>microservicecloud-config-dept-client-8001</artifactId>
	<dependencies>


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

		<!-- SpringCloudConfig配置 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>com.lss.springcloud</groupId>
			<artifactId>microservicecloud-api</artifactId>
			<version>${project.version}</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-core</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jetty</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
	</dependencies>
</project>

然后记得maven update一下。

 

 

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

 

 

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获取的服务地址

 

 

主要看bootstrap.yml文件里面的
profile: 属性具体值是什么,从而确定它能从github上取得什么样的配置
假如配置dev左图,如果配置test那就找右图,具体各自数据库不同,从而依据配置得到分布式配置的目的
 

 

test配置默认访问

http://localhost:8001/dept/list

可以看到数据库配置是02

 

 

本地换配置成dev

http://localhost:8001/dept/list

可以看到数据库配置是01

 

 

 

 

 

dependencyManagement使用简介
Maven中的dependencyManagement元素提供了一种管理依赖版本号的方式。在dependencyManagement元素中声明所依赖的jar包的版本号等信息,那么所有子项目再次引入此依赖jar包时则无需显式的列出版本号。Maven会沿着父子层级向上寻找拥有dependencyManagement 元素的项目,然后使用它指定的版本号。

举例
在父项目的POM.xml中配置:

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>1.2.3.RELEASE</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
此配置即声明了spring-boot的版本信息。

子项目则无需指定版本信息:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
使用优点
如果有多个子项目都引用同一样依赖,则可以避免在每个使用的子项目里都声明一个版本号。当想升级或切换到另一个版本时,只需要在顶层父容器里更新,而不需要逐个修改子项目;另外如果某个子项目需要另外的一个版本,只需要声明version即可。

注意事项
dependencyManagement中定义的只是依赖的声明,并不实现引入,因此子项目需要显式的声明需要用的依赖。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值