一. 创建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
推荐文章
系统上线后雪崩!让我们来学习 Spring Cloud Hystrix 及监控来解决雪崩问题
10 分钟教会你 Spring Boot 集成 Thymeleaf、MyBatis 完成产品的增删改查