SpringCloud Config

服务端配置

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  请求体

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值