8 Spring Cloud微服务 configServer配置中心

new->project->
Group    :com.imooc
Artifact :config
->
Spring Cloud Discovery ->Eureka Discovery
Spring Cloud Config    ->Config Server
->完成

application.yml文件内容
spring:
  application:
    name: config
  datasource:
    #driver-class-name: com.mysql.jdbc.Driver
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: hadoop
    url: jdbc:mysql://127.0.0.1:3306/SpringCloud_Sell?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
    #url: jdbc:mysql://127.0.0.1:3306/SpringCloud_Sell?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true

  jpa:
    show-sql: true
    hibernate:
      ddl-auto: create
      database-platform: org.hibernate.dialect.PostgreSQLDialect
    database: mysql
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

如果使config 成为Server需要添加一个注解下面文件为ConfigApplication.java
package com.imooc.config;

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

@SpringBootApplication
@EnableDiscoveryClient         <----服务发现Eureka作为客户端
@EnableConfigServer            <----configure server
public class ConfigApplication {

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

}


https://gitee.com/projects/new 登录
新建仓库
仓库名称 config-repo
仓库介绍:用来存放配置文件
项目:选择私有
    使用Readme文件初始化这个仓库
进入到仓库->文件->新建文件
config-repo/order.yml(order项目的application.yml配置文件,内容如下)

spring:
  application:
    name: order
  datasource:
    #driver-class-name: com.mysql.jdbc.Driver
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: hadoop
    url: jdbc:mysql://127.0.0.1:3306/SpringCloud_Sell?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
    #url: jdbc:mysql://127.0.0.1:3306/SpringCloud_Sell?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true

  jpa:
    show-sql: true
    hibernate:
      ddl-auto: create
      database-platform: org.hibernate.dialect.PostgreSQLDialect
    database: mysql
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

提交到gitee中
仓库的地址为: https://gitee.com/zhangjinyu/config-repo
之后在config项目的application.yml文件中加入spring.cloud.config.server.git.uri
spring:
  application:
    name: config
  datasource:
    #driver-class-name: com.mysql.jdbc.Driver
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: hadoop
    url: jdbc:mysql://127.0.0.1:3306/SpringCloud_Sell?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
    #url: jdbc:mysql://127.0.0.1:3306/SpringCloud_Sell?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true

  jpa:
    show-sql: true
    hibernate:
      ddl-auto: create
      database-platform: org.hibernate.dialect.PostgreSQLDialect
    database: mysql
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/zhangjinyu/config-repo
          username: xsjzdrxsjzdr@163.com
          password: Zhang1335501
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

重启之后访问,两个都可以,但是不能不写
http://localhost:8080/order-a.yml
http://localhost:8080/order-b.yml

http://localhost:8080/order-a.properties 网址,如下内容,进行了转换
eureka.client.service-url.defaultZone: http://localhost:8761/eureka/
spring.application.name: order
spring.datasource.driver-class-name: com.mysql.cj.jdbc.Driver
spring.datasource.password: hadoop
spring.datasource.url: jdbc:mysql://127.0.0.1:3306/SpringCloud_Sell?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
spring.datasource.username: root
spring.jpa.database: mysql
spring.jpa.hibernate.database-platform: org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto: create
spring.jpa.show-sql: true

http://localhost:8080/order-a.json

可以用这种方式判断自己的YML文件是否写对
(1)现在https://gitee.com/  项目中更改文件
(2)不用重启,然后在浏览器中更新网址

/{name}-{profiles}.yml
/{label}/{name}-{profiles}.yml
其中
name 服务名
profiles 环境env: dev
label 对应着git分支(branch)
/release/order-dev.yml  release分支label

从远端git拉去到本地git,其中本地git目录可以配置,配置文件如下
spring:
  application:
    name: config
  datasource:
    #driver-class-name: com.mysql.jdbc.Driver
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: hadoop
    url: jdbc:mysql://127.0.0.1:3306/SpringCloud_Sell?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
    #url: jdbc:mysql://127.0.0.1:3306/SpringCloud_Sell?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true

  jpa:
    show-sql: true
    hibernate:
      ddl-auto: create
      database-platform: org.hibernate.dialect.PostgreSQLDialect
    database: mysql
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/zhangjinyu/config-repo
          username: xsjzdrxsjzdr@163.com
          password: Zhang1335501
          basedir:  D:\workspace\test     <----------Git代码本地存放位置
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

运行完成后本地文件内容为
D:\workspace\test 的目录

2020/03/06  15:02    <DIR>          .
2020/03/06  15:02    <DIR>          ..
2020/03/06  15:02               727 order.yml
2020/03/06  15:02               833 README.en.md
2020/03/06  15:02               931 README.md
D:\workspace\test>

D:\workspace\test>git log
commit c14056c8f25191c229e42ae4a52c480fc6c2757f
Author: 蓝色飞扬 <xsjzdrxsjzdr@163.com>
Date:   Fri Mar 6 12:51:09 2020 +0800

    add order.yml.

commit 889f3451f769774a971de7f3c8734dafcd969905
Author: 蓝色飞扬 <xsjzdrxsjzdr@163.com>
Date:   Fri Mar 6 12:48:21 2020 +0800

    Initial commit

D:\workspace\test>

 

 

客户端内容

config client如何使用
(1)修改pom.xml文件
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
</dependency>

(2)config->application.yml文件内容
spring:
  application:
    name: config     <------------------------
  datasource:
    #driver-class-name: com.mysql.jdbc.Driver
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: hadoop
    url: jdbc:mysql://127.0.0.1:3306/SpringCloud_Sell?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
    #url: jdbc:mysql://127.0.0.1:3306/SpringCloud_Sell?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true

  jpa:
    show-sql: true
    hibernate:
      ddl-auto: create
      database-platform: org.hibernate.dialect.PostgreSQLDialect
    database: mysql
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/zhangjinyu/config-repo
          username: ******@163.com
          password: *******
          basedir:  D:\workspace\test
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/



(3)application.yml修改名字为bootstrap.yml希望先启动(bootstrap启动引导的意思)
order->src->resources->bootstrap.yml
spring:
  application:
    name: order  <--------------------
  cloud:
    config:
      discovery:
        enabled: true
        service-id: CONFIG  <-------------Eureka中的config服务名字
      profile: test
    stream:
      bindings:
        myMessage:
          group: order
          content-type: application/json
  zipkin:
    base-url: http://zipkin:9411/
    sender:
      type: web
  sleuth:
    sampler:
      probability: 1
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:8761/eureka/,http://eureka2:8762/eureka/
  instance:
    prefer-ip-address: true

logging:
  level:
    org.springframework.cloud.openfeign: debug
    org.springframework.cloud.bus: debug




(3)server->controller->EnvController.java
package com.imooc.order.controller;

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

@RestController
@RequestMapping("/env")
public class EnvController {

    @Value("${env}")
    private String env;

    @GetMapping("/print")
    private String print() {
        return env;
    }
}


访问->localhost:8081/env/print


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值