微服务和VUE入门教程(7):配置中心-config

微服务和VUE入门教程(7):配置中心-config

微服务和VUE入门教程(0): 着手搭建项目
微服务和VUE入门教程(1): 搭建前端登录界面
微服务和VUE入门教程(2): 注册中心
微服务和VUE入门教程(3): user微服务的搭建
微服务和VUE入门教程(4):网关zuul的搭建
微服务和VUE入门教程(5): 前后端交互
微服务和VUE入门教程(6):连接数据库-mybatis
微服务和VUE入门教程(7):配置中心-config
微服务和VUE入门教程(8):前端主页的编写
微服务和VUE入门教程(9): token验证-token后端生成以及前端获取
微服务和VUE入门教程(10): token验证-前端登录拦截以及token过期提醒
微服务和VUE入门教程(11): mybatis 动态查询
微服务和VUE入门教程(12):前端提示搜索框的实现
微服务和VUE入门教程(13): token验证-zuul拦截与验证
微服务和VUE入门教程(14): 热部署
微服务和VUE入门教程(15): 课堂小知识
微服务和VUE入门教程(16): zuul 熔断
微服务和VUE入门教程(17): VUE 响应拦截器
微服务和VUE入门教程(18): 前端接口模块化
微服务和VUE入门教程(19): VUE组件化–子组件向父组件通信
微服务和VUE入门教程(20): VUE组件化–父组件向子组件通信
微服务和VUE入门教程(21): springboot中定时器-Schedule
微服务和VUE入门教程(22): 页面长时间未操作自动退出登录
微服务和VUE入门教程(23): 微服务之间的调用
微服务和VUE入门教程(24): 微服务之断路器
微服务和VUE入门教程(25): 微服务之Hystrix-dashboard
微服务和VUE入门教程(26): 微服务之turbine
微服务和VUE入门教程(27):VUE前端工程打包

1. 配置中心介绍

简单来说,配置中心就是我们来存放各个微服务的配置文件的一个地方。如果没有配置中心,配置文件放在各个微服务中,对于配置的修改,会是一个不小的工程。举例来说,等数据库变化之后,如果没有配置中心,那么需要去各个微服务中修改数据库的配置。但是如果有了配置中心,只需要修改配置中心中的数据库配置文件即可,其他微服务来引用配置中心的配置文件,从而不必再去每个微服务中修改配置文件。

配置中心分为服务端和客户端。服务端用来存放配置文件,客户端是用来读取配置文件的其他微服务。因为注册中心需要最先启动,因此注册中心的配置信息不能保存到配置中心中。

2. 配置中心的编写

2.1 编写之前的准备

了解application.yml和bootstrap.yml文件

首先是加载顺序的不同,bootstrap.yml先加载,application.yml后加载

其次,存放配置信息的性质略有不同,一般而言,在bootstrap.yml中,我们存放系统级的配置信息,一般不用变化的,比如微服务的名字,而application.yml一般存放应用级的信息,有可能会根据需求而变化,比如连接数据库信息。

因此,有了配置中心,我们在其他微服务中使用bootstrap.yml文件。在使用过程中,其实也没必要分这么清楚。

2.2 修改父项目的pom文件

引入配置中心依赖


    org.springframework.cloud
    spring-cloud-config-server
    2.1.4.RELEASE



    org.springframework.cloud
    spring-cloud-starter-config
    2.1.4.RELEASE

2.3 新建my-config的Module

如图:

微服务和VUE入门教程(7):配置中心-config_第1张图片

2.4 修改config的pom文件


    
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-client
    

    
        org.springframework.boot
        spring-boot-starter-web
    

    
    
        org.springframework.cloud
        spring-cloud-config-server
    


2.5 修改application.xml文件

server:
  port: 8804

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8801/eureka/

# 配置微服务名字
spring:
  application:
    name: my-config
#  配置文件保存到本地
  profiles:
    active: native
#    保存位置
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/shared

配置中心中的其他微服务的配置文件可以保存到git,svn或本地。为了方便,这次以保存至本地来展示。保存路径为resouces/shared文件夹。

2.6 修改ConfigApplication.java文件

package com.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

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

这里只是声明了@EnableConfigServer,并没有加入@EnableDiscoveryClient。因为前者包含了后者,故而引用前者即可。

2.7 新建my-user-dev.xml

在resouces/shared文件夹中新建my-user-dev.xml

#配置端口号
server:
  port: 8802

#注册到注册中心
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8801/eureka/

这些信息直接复制my-user的配置信息就可以了。

2.8 新建my-batis-dev.xml

这个文件保存连接数据库的信息,也是直接复制my-user配置文件中关于数据库的信息就好。

在以后的开发过程中,其他微服务需要链接数据库时,直接引用这个文件就好。

# 配置微服务名字
spring:
  #  连接数据库
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/blog?characterEncoding=UTF-8
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
# mapper保存路径
mybatis:
  mapper-locations: classpath:mappers/*

2.9 修改my-user的pom文件

声明这是一个配置中心的客户端


    org.springframework.cloud
    spring-cloud-starter-config

2.10 修改my-user的配置文件

删除原来的application.yml文件,新建bootstrap.yml文件(注意名字可不要取错啊!!!!)

# 配置微服务名字
spring:
  application:
    name: my-user

  cloud:
    config:
      uri: http://localhost:8804
      fail-fast: true
      name: my-user, my-mybatis
  profiles:
      active: dev

信息是不是少了很多,只剩微服务的名字,还有配置中心的一些信息

cloud-config-uri:配置中心的ip和端口 cloud-config-name:要引用配置文件的前缀

profiles: active 本地文件 dev 后缀是dev

3. 测试

3.1 启动my-eureka,my-zuul

3.2 启动my-config

3.3 重启my-user

微服务和VUE入门教程(7):配置中心-config_第2张图片

可以当我们启动my-user微服务时,它会自动去配置中心寻找配置文件,从而正常启动。

可以使用登录来验证是否配置文件真正生效,当然也可以使用其他方法来验证配置中心来生效,可以去百度一下。

你可能感兴趣的:(微服务和VUE,java,spring,boot)