学习springcloud的SpringCloudConfig(配置中心-码云git)。记录其中遇见的问题(参考纯洁的微笑)...

配置中心服务端处理

1.先建立配置文件,将其上传到git上面

a83213c2d2a5f266418899a4d6a84b5f622.jpg

492c51ebe857676ea4ba3a5c93143c0c014.jpg

2.添加配置中心的pom依赖

<!--Springclud 的配置中心-->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-config-server</artifactId>
</dependency>

3.application.properties里面,增加配置中心配置

###############注册eureka服务##############
spring.application.name=spring-cloud-producer
server.port=9011
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/


##############集成SpringClud的配置中心##########
spring.cloud.config.server.git.uri=https://gitee.com/strongFan/individualProject/
#spring.cloud.config.server.git.uri=https://gitee.com/strongFan/individualProject.git   这里获取的不是git的地址,请参考进行对比
spring.cloud.config.server.git.searchPaths=config-repo  #git上面存放配置的文件夹
spring.cloud.config.server.git.username=xxxxxx
spring.cloud.config.server.git.password=xxxxxx

4.启动类增加开启配置中心注解

package com.example.democloudserver;

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

@EnableConfigServer  //开启SpringClud的配置中心
@SpringBootApplication
@EnableDiscoveryClient//启用服务注册与发现
public class DemocloudserverApplication {

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

}

这里,服务端的配置已经完后了,现在启动服务中心,启动配置中心,进行请求,查看。

b6433151bb3f7b9c182e4f4d678caf6a595.jpg

接下来,准备客户端请求获取服务端的配置

1.pom文件增加相关依赖

<!--配置中心客户端-->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

2.如果有application.properties,则建立bootstrap.properties。没有,则建立bootstrap.properties,原因请看配置文件

##########配置中心服务端获取############
spring.cloud.config.name=neo-config-dev
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:9011/
spring.cloud.config.label=master

#上面这些与spring-cloud相关的属性必须配置在bootstrap.properties中,config部分内容才能被正确加载。
#因为config的相关配置会先于application.properties,而bootstrap.properties的加载也是先于application.properties。

3.写一个方法,测试是否可以获取配置中心服务的配置

package com.example.servicefeign.controller;

import com.example.servicefeign.interfaceServer.HelloRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    HelloRemote hello;//注册接口层

    @Value("${neo.hello:111}")
    private String config;

    @RequestMapping("/hello/{name}")
    public String index(@PathVariable("name") String name) {
        return hello.hello(name);
    }

    /**
     * 获取配置中心参数
     */
    @GetMapping("/config")
    public String getConfig(){
        return this.config;
    }

}

启动项目,进行查看

3701b47ce5b963c693fb03bb05249ec33e3.jpg

 

好了,这里已经获取了。

 

我们这里,在看下git上面的配置文件

e735a8503b71d9bf61a4f5f26a10e54c842.jpg

 

转载于:https://my.oschina.net/u/2971292/blog/3044539

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值