springcloud之config

官方文档:https://www.springcloud.cc/spring-cloud-dalston.html#_spring_cloud_config
在这里插入图片描述

一、git环境搭建

1.Gitee添加SSH公钥: 右键—》git bash here—》输入指令:ssh-keygen -t rsa -C "2364201071@qq.com",连按回车—》复制C:\Users\z2364.ssh\id_rsa.pub中的内容并粘贴到码云上的添加公钥中
2.在码云上创建一个springcloud-config仓库。
3.将代码通过ssh下载到本地。
4.创建application.yml文件,加上如下配置

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

5.依次执行如下指令,将代码上传到仓库。cd springcloud-config/

git add .
git status
git commit -m "first commit"
 git push origin master

解决出错:

! [rejected] master -> master (fetch first) error: failed to push some refs to ' 。。。'

出现这个问题是因为github中的README.md文件不在本地代码目录中,可以通过如下命令进行代码合并

git pull --rebase origin master

二、服务端连接git配置

1.创建一个config-server-3344工程。
2.引入依赖

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>2.1.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

3.创建启动类

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

4.编写配置文件

server:
  port: 3344
spring:
  application:
    name: config-server

    #连接远程仓库
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/zheng_silence/springcloud-config.git

输入网址http://localhost:3344/application/test/master或http://localhost:3344/application-test.yml可访问GitHub上的相关资源,格式如下:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

三、客户端连接服务端访问远程

1.创建一个config-client.yml配置文件,里面进行config-client服务的配置,然后通过git上传到github仓库中。

spring:
  profiles:
    active: dev

---
server:
  port: 3345
spring:
  profiles: dev #SpringBoot能使用application- {你的自定义profile名称myProfileName} .properties模式添加任何你指定配置文件到其属性文件。要加载特定的配置文件属性文件,我们可以使用命令行选项-Dspring.profiles.active = myProfileName。
  application:
    name: provider-dept

eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka,http://localhost:10087/eureka

---
server:
  port: 3346
spring:
  profiles: test #SpringBoot能使用application- {你的自定义profile名称myProfileName} .properties模式添加任何你指定配置文件到其属性文件。要加载特定的配置文件属性文件,我们可以使用命令行选项-Dspring.profiles.active = myProfileName。
  application:
    name: provider-dept

eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka,http://localhost:10087/eureka

2.创建一个config-client工程。
3.引入依赖

	<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
        <version>2.2.1.RELEASE</version>
    </dependency>
    <!--实时监控-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

4.创建bootstrap.yml配置文件

#系统级别的配置spring:
spring:
  cloud:
    config:
      name: config-client #需要从git上读取的资源名称,不需要后缀
      uri: http://localhost:3344
      profile: test
      label: master

创建application.yml配置文件

#用户级别的配置
spring:
  application:
    name: config-client

5.创建启动类
6.编写一个controller类进行测试

@RestController
public class ConfigClientController {
    @Value("${spring.application.name}")
    private String applicationName;
    @Value("${eureka.client.service-url.defaultZone}")
    private String eurekaServer;
    @Value("${server.port}")
    private String port;

    @RequestMapping
    public String GetConfig(){
        return "applicationName:"+applicationName+"eurekaServer:"+eurekaServer+"port:"+port;
    }
}

输入网址路径http://localhost:3346/master/config-client-test.yml,页面返回结果:在这里插入图片描述

四、远程配置测试

将eureka-server工程由之前的本地配置转为远程配置
步骤一:
新建一个config-eureka.yml配置文件,并将eureka-service工程的application.yml的配置信息复制到该文件,然后删除application.yml。

spring:
  profiles:
    active: dev
    
---
server:
  port: 10087

spring:
  profiles: dev
  application:
    name: eureka-service #服务名称

eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka,http://localhost:10087/eureka
  server:
    eviction-interval-timer-in-ms: 30000 #设置宕机服务剔除时间
    
---
server:
  port: 10087

spring:
  profiles: test
  application:
    name: eureka-service #服务名称

eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka,http://localhost:10087/eureka
  server:
    eviction-interval-timer-in-ms: 30000 #设置宕机服务剔除时间

步骤二:
引入依赖

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

步骤三:
新建bootstrap.yml配置文件:

spring:
  cloud:
    config:
      label: master
      name: config-eureka
      profile: dev
      uri: http://localhost:3344

创建application.yml配置文件

spring:
  application:
    name: eureka-service #服务名称

最后,输入网址http://localhost:10087访问成功,则远程配置成功。

五、config动态刷新

避免每次更新配置都要重启客户端微服务。
1.引入依赖

	<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>sproing-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>

2.修改bootstrap.yml配置文件

#暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

3.在ConfigClientController中添加@RefreshScope注解
4.需要运维人员发送post请求刷新config-client。(在命令行中输入-X post "http://xxxx"即可变为post请求)
5.修改远程配置文件,用config-client去访问资源,看是否能被刷新。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值