服务端配置
springboot 版本 2.0.0.RELEASE
pow.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--Spring Cloud Config 服务端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M9</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
bootstrap.yml
server:
port: 7201
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: http://git.**.cn/platform/****.git
search-paths: config-repo
username: **** #账号密码写真实的快一些我觉得,不使用也能访问有点慢
password: *****
启动类
/**
* 这里没有使用SpringBootApplication或SpringCloudApplication注解,会报错
* 原因也很简单,我们的java源码目录下没有目录,我们手动加一个也就正常了,
* 为了写点体会和这里没必要用到包和类,所以使用这种方式
*/
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApp {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApp.class, args);
}
}
客户端配置
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--Spring Cloud Config 客户端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M9</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
bootstrap.yml
spring:
application:
name: hellxztest #指定了配置文件的应用名
cloud:
config:
uri: http://127.0.0.1:7201/ #Config server的uri
profile: prod #指定的环境
label: master #指定分支
server:
port: 7202
management:
security:
enabled: false #SpringBoot 1.5.X 以上默认开通了安全认证,如果不关闭会要求权限
endpoints:
web:
exposure:
include: refresh # 打开自动refresh功能
eureka :
client :
register-with-eureka : false
fetch-registry : false
serviceUrl.defaultZone : http://192.168.*.*:1111/eureka/
启动类
@SpringBootApplication
public class ConfigClientApp {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApp.class, args);
}
}
配置读取
/**
* 当有请求/fresh节点的时候,会重新请求一次ConfigServer去拉取最新的配置文件
* 请求/fresh需要有几点要求:1.加actuator的依赖 2.SpringCloud1.5以上需要设置 management.security.enabled=false
* 这个Controller的作用是查看from这个key的值
*/
@RestController
@RefreshScope //开启更新功能
@RequestMapping("api")
public class TestController {
@Value("${from}")
private String fromValue;
/**
* 返回配置文件中的值
*/
@GetMapping("/from")
@ResponseBody
public String returnFormValue(){
return fromValue;
}
}
refresh的接口 http://127.0.0.1:7202/actuator/refresh post请求 formdata 请求体
167万+

被折叠的 条评论
为什么被折叠?



