springcloud config

2021-07-10 06:04

阅读:513

标签:jdb   def   名称   pid   dap   服务配置   turn   client   admin   

分布式配置中心(Spring Cloud Config)(Finchley版本)

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。

在原有项目的基础上 新增model,cloud-config-server,引入依赖

xml version="1.0" encoding="UTF-8"?>
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.0modelVersion>

    groupId>com.zhaowb.springcloudgroupId>
    artifactId>cloud-config-serverartifactId>
    version>0.0.1-SNAPSHOTversion>
    packaging>jarpackaging>

    name>cloud-config-servername>
    description>Demo project for Spring Bootdescription>
    parent>
        groupId>com.examplegroupId>
        artifactId>cloud-demoartifactId>
        version>0.0.1-SNAPSHOTversion>
    parent>

    dependencies>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-webartifactId>
        dependency>
        dependency>
            groupId>org.springframework.cloudgroupId>
            artifactId>spring-cloud-config-serverartifactId>
        dependency>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-testartifactId>
            scope>testscope>
        dependency>
    dependencies>

    build>
        plugins>
            plugin>
                groupId>org.springframework.bootgroupId>
                artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

配置application.yml

server:
  port: 8769
spring:
  application:
    name: cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/JiShiMoWang_admin/cloud-demo-config.git
          default-label: master

我的不能使用SSH ,暂未解决。

在启动类上添加@EnableConfigServer

package com.zhaowb.springcloud.cloudconfigserver;

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

@SpringBootApplication
@EnableConfigServer
public class CloudConfigServerApplication {

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

gitee上的配置为

spring:
  profiles:
    active:
    - dev
---
spring:
  profiles: dev
  application:
    name: cloud-config-server-dev
---
spring:
  profiles: test
  application:
    name: cloud-config-server-test

在浏览器输入http://localhost:8769/cloud-config-server-test.yml 显示

spring:
  application:
    name: cloud-config-server-test
  profiles:
    active:
    - dev

输入http://localhost:8769/cloud-config-server-dev.yml,显示

spring:
  application:
    name: cloud-config-server-dev
  profiles:
    active:
    - dev

说明配置生效。

http请求地址和资源文件映射如下:

  • /{application}/{profile}[/{label}]

  • /{application}-{profile}.yml

  • /{label}/{application}-{profile}.yml

  • /{application}-{profile}.properties

  • /{label}/{application}-{profile}.properties

都可以获取配置信息,看个人喜好选择。

高可用的分布式配置中心(Spring Cloud Config)

当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,创建model,cloud-config-eureka-server、cloud-config-dept-client,

在server中引入依赖


         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0com.zhaowb.springcloud
    cloud-config-eureka-server
    0.0.1-SNAPSHOTjarcloud-config-eureka-serverDemo project for Spring Bootcom.example
        cloud-demo
        0.0.1-SNAPSHOTorg.springframework.boot
            spring-boot-starter-web
        org.springframework.cloud
            spring-cloud-config-server
        org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        org.springframework.boot
            spring-boot-starter-test
            testorg.springframework.boot
                spring-boot-maven-plugin
            

配置bootstrap.yml

spring:
  cloud:
    config:
      name: cloud-config-eureka-server
      uri: http://localhost:8769/
      label: master

配置application.yml

spring:
  application:
    name: cloud-config-eureka-server

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:8770/eureka/
server:
  port: 8770

原计划是将application.yml 放到git上,但是在实际操作中未能实现,暂未找到原因。可能是eureka-server 无法从git获取信息,或我配置文件错误。

在启动类上加上注解

package com.zhaowb.springcloud.cloudconfigeurekaserver;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaServer
@RestController
public class CloudConfigEurekaServerApplication {

    @Value("${file.name:mojhap}")
    private String fileName;
    public static void main(String[] args) {
        SpringApplication.run(CloudConfigEurekaServerApplication.class, args);
    }
    @RequestMapping(value = "/getFileName",method = RequestMethod.GET)
    public String getFileName(){
        return fileName;
    }
}

cloud-config-dept-client ,引入依赖

xml version="1.0" encoding="UTF-8"?>
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.0modelVersion>

    groupId>com.zhaowb.springcloudgroupId>
    artifactId>cloud-config-dept-clientartifactId>
    version>0.0.1-SNAPSHOTversion>
    packaging>jarpackaging>

    name>cloud-config-dept-clientname>
    description>Demo project for Spring Bootdescription>

    parent>
        groupId>com.examplegroupId>
        artifactId>cloud-demoartifactId>
        version>0.0.1-SNAPSHOTversion>
    parent>
    dependencies>
        dependency>
            groupId>com.zhaowb.springcloudgroupId>
            artifactId>cloud-apiartifactId>
            version>${project.version}version>
        dependency>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-webartifactId>
        dependency>
        dependency>
            groupId>org.springframework.cloudgroupId>
            artifactId>spring-cloud-starter-configartifactId>
        dependency>
        dependency>
            groupId>org.springframework.cloudgroupId>
            artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>

        dependency>
            groupId>mysqlgroupId>
            artifactId>mysql-connector-javaartifactId>
            scope>runtimescope>
        dependency>
        
        dependency>
            groupId>com.alibabagroupId>
            artifactId>druidartifactId>
            version>1.1.10version>
        dependency>

        
        dependency>
            groupId>org.mybatis.spring.bootgroupId>
            artifactId>mybatis-spring-boot-starterartifactId>
            version>1.3.2version>
        dependency>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-testartifactId>
            scope>testscope>
        dependency>
    dependencies>

    build>
        plugins>
            plugin>
                groupId>org.springframework.bootgroupId>
                artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

配置bootstrap.yml

spring:
  cloud:
    config:
      name: cloud-config-dept-client # 需要从github上读取的资源名称,注意没有yml后缀名
      uri: http://localhost:8769/  # SpringCloudConfig获取的服务地址
      label: master

配置application.yml

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

启动类加上注解

package com.zhaowb.springcloud.cloudconfigdeptclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class CloudConfigDeptClientApplication {

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

其余的将eureka-hi的复制过来,修改一下使用。

git上配置

server:
  port: 8771
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置文件所在路径
  type-aliases-package: com.zhaowb.springcloud.cloudapi.entities   # 所有Entity别名类所在包
  mapper-locations:
  - classpath:/mybatis/mapper/**/*.xml                       # mapper映射文件

spring:
  application:
    name: cloud-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: 308539393
    dbcp2:
      min-idle: 5                                           # 数据库连接池的最小维持连接数
      initial-size: 5                                       # 初始化连接数
      max-total: 5                                          # 最大连接数
      max-wait-millis: 200                                  # 等待连接获取的最大超时时间

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8770/eureka/
  instance:
    instance-id: cloud-config-dept-client
    prefer-ip-address: true
info:
  app.name: cloud-config-dept-client
  company.name: www.zhaowb.com
  build.artifactId: $project.artifactId$    # 这样写在idea 中运行出来不显示版本号,直接显示 $project.artifactId$ 字符串,可以自己修改
  build.version: $project.version$

在浏览器输入http://localhost:8771/dept/list,显示[{"deptno":1,"dname":"部门1","db_source":"clouddb02"},{"deptno":2,"dname":"部门2","db_source":"clouddb02"},{"deptno":3,"dname":"部门3","db_source":"clouddb02"},{"deptno":4,"dname":"部门4","db_source":"clouddb02"},{"deptno":5,"dname":"部门5","db_source":"clouddb02"},{"deptno":6,"dname":"部门6","db_source":"clouddb02"}]

修改数据库, 改为url: jdbc:mysql://localhost:3306/cloudDB01 # 数据库名称

重新启动,只要显示的"db_source":"clouddb01",即为成功。

码云地址

GitHub地址

springcloud config

标签:jdb   def   名称   pid   dap   服务配置   turn   client   admin   

原文地址:https://www.cnblogs.com/zwb1234/p/9563439.html


评论


亲,登录后才可以留言!