Spring Cloud Config实战练习

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhou920786312/article/details/85019917

测试中使用到的代码到在这里https://download.csdn.net/download/zhou920786312/10853300

 

1将下面2个配置文件放到远程gethub仓库(如果不会操作,看上一編)

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              # mysql驱动包
    url: jdbc:mysql://localhost:3306/cloudDB01              # 数据库名称
    username: root
    password: root
    dbcp2:
      min-idle: 5                                           # 数据库连接池的最小维持连接数
      initial-size: 5                                       # 初始化连接数
      max-total: 5                                          # 最大连接数
      max-wait-millis: 200      
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置文件所在路径
  type-aliases-package: com.atguigu.springcloud.entities    # 所有Entity别名类所在包
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml                       # mapper映射文件
    
eureka:
  client: #客户端注册进eureka服务列表内
    service-url: 
      #defaultZone: http://localhost:7001/eureka
       defaultZone: http://eureka7001.com:7001/eureka/     
  instance:
    instance-id: microservicecloud-dept8001
    prefer-ip-address: true     #访问路径可以显示IP地址     
 
info: 
  app.name: atguigu-microservicecloud
  company.name: www.atguigu.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              # mysql驱动包
    url: jdbc:mysql://localhost:3306/cloudDB02             # 数据库名称
    username: root
    password: root
    dbcp2:
      min-idle: 5                                           # 数据库连接池的最小维持连接数
      initial-size: 5                                       # 初始化连接数
      max-total: 5                                          # 最大连接数
      max-wait-millis: 200      
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置文件所在路径
  type-aliases-package: com.atguigu.springcloud.entities    # 所有Entity别名类所在包
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml                       # mapper映射文件
    
eureka:
  client: #客户端注册进eureka服务列表内
    service-url: 
      #defaultZone: http://localhost:7001/eureka
       defaultZone: http://eureka7001.com:7001/eureka/     
  instance:
    instance-id: microservicecloud-dept8001
    prefer-ip-address: true     #访问路径可以显示IP地址     
 
info: 
  app.name: atguigu-microservicecloud
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

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
  client: #客户端注册进eureka服务列表内
    register-with-eureka: false#不注册进eureka服务列表内
    fetch-registry: false #不从eureka服务列表获取信息
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka/       

---
server: 
 port: 7001
spring: 
  profiles: test   
  application: 
    name: microservicecloud-config-eureka-client 
    
eureka:
  instance: 
    hostname: eureka7001.com
  client: #客户端注册进eureka服务列表内
    register-with-eureka: false#不注册进eureka服务列表内
    fetch-registry: false #不从eureka服务列表获取信息
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka/      

2创建microservicecloud-config-eureka-client-7001客户端,并测试

/**
 * EurekaServer服务器端启动类,接受其它微服务注册进来
 * 
 * @author zhouyang
 */
@SpringBootApplication
@EnableEurekaServer
public class Config_Git_EurekaServerApplication
{
	public static void main(String[] args)
	{
		SpringApplication.run(Config_Git_EurekaServerApplication.class, args);
	}
}

application.yml

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

bootstrap.yml

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

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.atguigu.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>

测试1

启动microservicecloud-config-3344

启动microservicecloud-config-eureka-client-7001

说明:如果启动成功,microservicecloud-config-eureka-client-7001会从配置中心读取配置文件,就会出现下面的情况

http://eureka7001.com:7001/

3创建microservicecloud-config-dept-client-8001客户端,并测试

代码是对原有的microservicecloud-provider-dept-8001的拷贝,就是配置改了下


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

测试1

启动microservicecloud-config-3344

启动microservicecloud-config-eureka-client-7001

启动microservicecloud-config-dept-client-8001

因为microservicecloud-config-dept-client的配置dev读取的01数据库,test读取的02数据库,所以如下测试

上图对应配置文件的

数据测试

http://localhost:8001/dept/list

修改bootstrap.yml的配置为dev后测试

猜你喜欢

转载自blog.csdn.net/zhou920786312/article/details/85019917