【Spring全家桶系列之核心篇 | Spring Cloud】 - 第十一章 Spring Config入门案例

一. 创建Spring Cloud Server端

在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述修改appliation.properties文件为application.yml,并加入下面内容:

server:
  port: 9002
spring:
  application:
    name: configserver
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/config
        bootstrap: true

在resources下新建config文件夹,文件夹中新建文件eurekaapplication-dev.yml,内容如下:

server:
  port: 8001
spring:
  application:
    name: eureka
  security:
    user:
      name: itzhiya
      password: itzhiya

eureka:
  instance:
    hostname: localhost
  server:
    enable-self-preservation: false #关闭自我保护
    eviction-interval-timer-in-ms: 5000 #默认 60000 毫秒
  client:
    fetch-registry: false # 注册中心职责是维护服务实例,false:不检索服务。
    register-with-eureka: false # 此应用为注册中心,false:不向注册中心注册自己。
    service-url:
      defaultZone: http://localhost:8001/eureka/ #, http://localhost:8008/eureka/

启动类中加入注解@EnableConfigServer

package com.itzhiya.spring.cloud.config.server;

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

@SpringBootApplication
@EnableConfigServer
public class SpringcloudconfigserverApplication {

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

}

启动程序

二. 创建Spring Cloud Config Client端

在这里插入图片描述
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述
在resources中创建bootstrap.yml文件,内容如下:

spring:
  application:
    name: springcloudconfigclient
  cloud:
    config:
      uri: http://localhost:9002/
      name: eurekaapplication
      profile: dev  

创建Controller,进行测试

package com.itzhiya.spring.cloud.config.client;

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

@RestController
public class ApplicationController {

    @GetMapping("hello")
    public String getApplicationName(){
        return "hello, welcome to spring cloud config!";
    }
}

启动服务
浏览器输入路径http://localhost:8001/hello
在这里插入图片描述

推荐视频
玩转Spring Data JPA&Spring Data JDBC

深入浅出SpringCloud

SpringBoot快速入门

Spring MVC快速开发

轻松搞定Spring全家桶(初识篇)

玩转 Spring 全家桶

推荐文章

系统上线后雪崩!让我们来学习 Spring Cloud Hystrix 及监控来解决雪崩问题

10 分钟教会你 Spring Boot 集成 Thymeleaf、MyBatis 完成产品的增删改查

【高阶用法】一个实例学会 Spring Cloud 的注册中心 Eureka的使用

Spring Cloud gateway与注册中心Eureka的完美集成

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT职涯

你的鼓励将是我最最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值