spring cloud config client 使用

[b]spring cloud config client 使用[/b]

client会从config service 中得到配置参数,config service 从git 中得到配置参数


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>


<groupId>com.midea</groupId>
<artifactId>base</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>base</name>

<!-- 设定仓库,按设定顺序进行查找. -->
<!--<repositories> <repository> <id>public</id> <name>Team Nexus Repository</name>
<url>http://10.33.183.113:8081/nexus/content/groups/public</url> <snapshots>
<enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots>
</repository> </repositories> <pluginRepositories> <pluginRepository> <id>public</id>
<name>Team Nexus Repository</name> <url>http://10.33.183.113:8081/nexus/content/groups/public</url>
<snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots>
</pluginRepository> </pluginRepositories> -->

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId>
</dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId>
</dependency> -->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>

</build>
</project>



server.port=8767
##logging.pattern.level=INFO
#
##服务器路径
#server.context-path=/eurekaServer

#服务名
spring.application.name=configClient
spring.cloud.config.label=master
spring.cloud.config.profile=test
#到configServicej里得到配置参数
spring.cloud.config.uri= http://localhost:8888/

#默认是需要身份认证的,一些页面不能访问,加上后所有页面不需要认证,都可以访问。
management.security.enabled=false



package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;


@SpringBootApplication //spring boot 开启应用
//@RefreshScope //指示Config客户端在服务器参数刷新时,也刷新注入的属性值,
public class Application {

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

}



package com.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;

@Api(value = "/Test", description = "测试接口", tags = "TestController")
@RequestMapping("/Test")
@RestController
@RefreshScope //指示Config客户端在服务器参数刷新时,也刷新注入的属性值,
public class TestController {
final static Logger logger = LogManager.getLogger(TestController.class);

//得到配置参数放入变量foo中
@Value("${foo}")
String foo;

@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test() {
logger.info("test");

logger.info("foo == " + foo);
return foo;
}
}



动态刷新配置
对Conf客户端发一个POST请求[url]http://localhost:8767/refresh[/url],返回200 OK。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

jie310600

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

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

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

打赏作者

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

抵扣说明:

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

余额充值