SpringCloud 配置实战:

<mark>开发流程图</mark>:


在本地的GitHub仓储版本库中创建如下配置
microservicecloud-config-eureka-client.yml配置:

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/
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/cloudDB01
      username: mysql
      password: 15827318595
    

在GitHub仓储版本库总创建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: com.mysql.jdbc.Driver
     #数据库名称
    url: jdbc:mysql://localhost:3306/cloudDB02
    username: mysql
    password: 15827318595
    dbcp2: 
       min-idle: 5 #数据库连接池的最小维持连接数
       initial-size: 5 #初始化连接数5个
       max-total: 5 #设置最大连接数
       max-wait-millis: 200 #s等待连接获取的最大超时时间

mybatis:
#mybatis配置文件所在路径
  config-location: classpath:mybatis/mybatis.cfg.xml
   #所有Entity别名类所在包
  type-aliases-package: com.lyj.springcloud.entities
   #mapper映射文件所在路径
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml
  
#配置Eureka
eureka:
#客户端注册进Eureka服务列表内
  client:   
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka #(单机版服务器)
# defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
# 集群配置(多台服务器共同使用)
 #自定义实例的statusIP地址名字(主机名字:服务名称修改,不采用默认的名字)
  instance:
    instance-id: dept-8001.com
    prefer-ip-address: true #访问路径可以显示IP地址
    
    
#配置Eurekaweb界面访问各个服务的的详细信息的info界面
info:
  app.name: lyj-microservicecloud-springcloudconfig02
  company.name: www.lyj.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

上传文件到GitHub的仓储里:


新建工程,配置盘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.lyj.springcloud</groupId>
		<artifactId>microservicecloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>microservicecloud-config-eureka-client-7001</artifactId>
	<dependencies>
		<!-- SpringCloud Config依赖配置==start -->
		<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>
		<!-- SpringCloud Config依赖配置==end -->
		<!-- 引入热部署 插件依赖==start -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
		<!-- 引入热部署 插件依赖==start -->

	</dependencies>

</project>

配置application.yml和bootstrap.yml文件;name 值要一致与GitHub上的文件名一致。
application.yml:

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

bootstrap.yml:

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

测试:

出现eureka主页,表示成功启动。

新建工程后,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.lyj.springcloud</groupId>
		<artifactId>microservicecloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>microservicecloud-config-dept-client-8001</artifactId>
	<dependencies>
		<!-- 引入SpringCloudConfig配置依赖==start -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>

		<!-- 引入自定义的api通用包,可以使用Dept部门的Entity -->
		<dependency>
			<groupId>com.lyj.springcloud</groupId>
			<artifactId>microservicecloud-api</artifactId>
			<!-- 下面指定的版本是活的,相当于,api通用包随意怎么变,这里都会自动更新,不影响 -->
			<version>${project.version}</version>
		</dependency>
		<!-- 将微服务注册provider到Eureka注册中心中 ==start -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<!-- 将微服务注册provider到Eureka注册中心中 ==end -->
		<!-- 引入监控信息完善的依赖==start -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<!-- 引入监控信息完善的依赖==end -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</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>
		<!-- 引入热部署,修改后立即生效===start -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
		<!-- 引入热部署,修改后立即自动发布和构建生效===end -->
	</dependencies>


</project>

配置bootstrap.yml和application.yml文件:
application.yml:

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

bootstrap.yml:

spring:
  cloud:
    config:
    #需要从GitHub上读取的资源名称
      name: microservicecloud-config-dept-client
      #profile配置是什么就取什么配置 dev ortest
      #profile: dev
      profile: test
      label: master
      #SpringCloudConfig获取的服务IP地址
      uri: http://config-3344.com:3344

测试:



每次在本地修改GitHub仓储里的文件,都要再次上传更新GitHub上的文件,上传步骤截图:<mark>箭头指向的命令很关键,一定要有,它的更新的意思,要不然直接上传会失败</mark>。