SpringCloud入门(七)之Config(下)

导读:本篇文章讲解 SpringCloud入门(七)之Config(下),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

本篇文章是继上一篇SpringCloud入门(七)之Config(上)之后的,因此目录也是从之下的第三开始。

三,客户端配置

3.1本地仓库创建yml

在本地仓库 E:\Workspace\Git\servicecloud-config 路径下创建文件servicecloud-config -client.yml

server:
   port: 8201
spring:
   profiles: dev
   application: 
      name: servicecloud-config-client
eureka:
   client: 
      service-url:
         defaultZone: http://eureka-dev.com:7001/eureka/

---
server:
   port: 8202
spring:
   profiles: test
   application: 
      name: servicecloud-config-client
eureka:
   client: 
      service-url:
         defaultZone: http://eureka-dev.com:7001/eureka/

# 必须保存为utf-8格式

然后将创建的application.yml文件推送到GitHub上

git status

git add .

git commit -m "init file"

git push origin master
=====================不知为何,不加下面的命令就无法提交成功=======================
git remote add origin "git@github.com:XXX/servicecloud-config.git"

git pull --rebase origin master

git push -u origin master

3.2 pom.xml

新建module模块servicecloud-config-client-3355,修改pom.xml文件:

<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.lmc</groupId>
    <artifactId>servicecloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>servicecloud-config-client-3355</artifactId>
  
  <dependencies>
  	<!-- 自己定义的api -->
   <dependency>
     <groupId>com.lmc</groupId>
     <artifactId>servicecloud-api</artifactId>
     <version>${project.version}</version>
   </dependency>

   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
   </dependency>

   <!-- 修改后立即生效,热部署 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>springloaded</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
   </dependency>
   
   <!-- Ribbon相关 -->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-ribbon</artifactId>
   </dependency>

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

   <!-- feign相关 -->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-feign</artifactId>
   </dependency>
   
   <!-- config相关 -->
   <dependency> 
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-starter-config</artifactId>
	    <version>1.2.0.RELEASE</version> 
	</dependency>
   
   <dependency>
     <groupId>org.eclipse.jgit</groupId>
     <artifactId>org.eclipse.jgit</artifactId>
     <version>4.10.0.201712302008-r</version>
   </dependency>
   
  </dependencies>
  
</project>

3.3 bootstrap.yml

application.yml是用户级的资源配置项

bootstrap.yml是系统级的,优先级更高

创建bootstrap.yml:

spring: 
   cloud: 
      config: 
         name: servicecloud-config-client #需要从git读取的资源名称,没有后缀
         profile: dev #本次访问的配置项
         label: master
         uri: http://localhost:3344 #本微服务启动后先去找3344服务,通过springcloudconfig获取GitHub的服务地址

​ 服务启动时,本微服务先会去配置服务端 http://localhost:3344 获取名为 servicecloud-config-client 的配置文件,然后通过 bootstrap.yml 的profile 属性,去 servicecloud-config-client 配置文件获取相关配置参数。

再创建application.yml,配置微服务名称:

spring:
  application: 
     name: servicecloud-config-client

3.4 测试Controller

创建类ConfigClientRest,由于测试是否能从git获取配置数据:

package com.lmc.springcloud.rest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigClientRest {
	
	@Value("${spring.application.name}")
	private String applicationName;
	
	@Value("${eureka.client.service-url.defaultZone}")
	private String eurekaServers = "lmchh";
	
	@Value("${server.port}")
	private String port;
	
	@RequestMapping("/config")
	public String getConfig(){
		String url = "applicationName: " + applicationName +
				"\t eurekaServers: " + eurekaServers + "\t port: " + port;
		System.out.println(url);
		return url;
	}

}

@Value对应的参数都是在GitHub上面的

3.5 启动类

创建启动类ConfigClient_3355_StartSpringCloudApp:

package com.lmc.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.lmc.springcloud")
public class ConfigClient_3355_StartSpringCloudApp {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SpringApplication.run(ConfigClient_3355_StartSpringCloudApp.class, args);

	}

}

3.6 测试

第一步:先启动配置服务类3344,访问 http://localhost:3344/application-dev.yml 成功

第二步:启动配置客户端3355,访问 http://localhost:8201/config

第三步:修改3355客户端的 bootstrap.yml, 将profile值dev改成test

第四步:访问 http://localhost:8202/config

四,Config配置实践

4.1本地仓库配置

在本地仓库目录 E:\Workspace\Git\servicecloud-config 路径下创建文件servicecloud-config -eureka-client.yml:

spring: 
   profiles: 
      active: 
      - dev

---

server: 
   port: 7001

spring: 
   profiles: dev
   application: 
      name: servicecloud-config-eureka-client
eureka: 
   instance: 
      hostname: eureka7001.com
   client: 
      register-with-eureka: false
      fetch-registry: false
      service-url: 
         defaultZone: http://eureka7001.com:7001/eureka/

---
server: 
   port: 7001
spring: 
   profiles: test
   application: 
      name: servicecloud-config-eureka-client
eureka: 
   instance: 
      hostname: eureka7001.com
   client: 
      register-with-eureka: false
      fetch-registry: false
      service-url: 
         defaultZone: http://eureka7001.com:7001/eureka/

# 必须保存为utf-8格式

继续在本地仓库目录 E:\Workspace\Git\servicecloud-config 路径下创建文件servicecloud-config -dept-client.yml:

spring:
   profiles:
      active:
      - dev
---

server:
   port: 8001
spring:
   profiles: dev
   application: 
      name: servicecloud-config-dept-client
   datasource:
       type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
       driver-class-name: com.mysql.jdbc.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.lmc.springcloud.entities    # 所有Entity别名类所在包
  mapper-locations: classpath*:mybatis/mapper/**/*.xml     # mapper映射文件
  
eureka:
   client: 
      service-url: 
         defaultZone: http://eureka7001.com:7001/eureka
   instance:
      instance-id: dept-8001				#自定义服务名称信息
      prefer-ip-address: true   

info:
   app.name: lmc-servicecloud
   company.name: www.lmc.com
   build.artifactId: $project.artifactId$
   build.version: $project.version$
   
---

server:
   port: 8001
spring:
   profiles: test
   application: 
      name: servicecloud-config-dept-client
   datasource:
       type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
       driver-class-name: com.mysql.jdbc.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.lmc.springcloud.entities    # 所有Entity别名类所在包
  mapper-locations: classpath*:mybatis/mapper/**/*.xml     # mapper映射文件
  
eureka:
   client: 
      service-url: 
         defaultZone: http://eureka7001.com:7001/eureka
   instance:
      instance-id: dept-8001				#自定义服务名称信息
      prefer-ip-address: true   

info:
   app.name: lmc-servicecloud
   company.name: www.lmc.com
   build.artifactId: $project.artifactId$
   build.version: $project.version$

4.2 pom.xml(1)

创建config 版的eureka服务中心servicecloud-config-eureka-client-7001,配置pom.xml:

<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.lmc</groupId>
    <artifactId>servicecloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>servicecloud-config-eureka-client-7001</artifactId>
  
  <dependencies>
  
  <!-- config相关 -->
   <dependency> 
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-starter-config</artifactId>
	    <version>1.2.0.RELEASE</version> 
	</dependency>

   <!--eureka-server服务端 -->
   <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>

4.2 bootstrap.yml(1)

创建 bootstrap.yml :

spring: 
   cloud: 
      config: 
         name: servicecloud-config-eureka-client #需要从git读取的资源名称,没有后缀
         profile: dev #本次访问的配置项
         label: master
         uri: http://localhost:3344 #本微服务启动后先去找3344服务,通过springcloudconfig获取GitHub的服务地址

创建application.yml:

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

4.3 启动类(1)

创建启动类:

package com.lmc.springcloud;

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


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

此时,先启动3344,再启动7001的 http://eureka7001.com:7001/ ,能够正常进行访问了。

4.4 新8001

参照8001再创建servicecloud-config-dept-client的新module,pom.xml与之前8001一样。

bootstrap.yml:

spring: 
   cloud: 
      config: 
         name: servicecloud-config-dept-client #需要从git读取的资源名称,没有后缀
         profile: dev #本次访问的配置项
         label: master
         uri: http://localhost:3344 #本微服务启动后先去找3344服务,通过springcloudconfig获取GitHub的服务地址

application.yml:

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

4.5 测试

第一步:先启动3344配置服务器 ( http://localhost:3344/servicecloud-config-dept-client-dev.yml )

第二步:启动7001服务注册中心( http://eureka7001.com:7001/ )

第三步:启动8001服务提供者( http://localhost:8001/dept/list )

第四步:修改新8001的bootstrap.yml的profiles为test( http://localhost:8001/dept/list )

由于dev中访问的是clouddb01,test中访问的是clouddb02,因此两次访问的结果不同,则部署成功。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/81636.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!