springcloud2.0config配置中⼼,连接注册中
⼼,@RefreshScope
spring-boot版本:2.x
⼀、配置中⼼
配置中⼼⽀持从git,svn,本地获取配置⽂件,本⽂使⽤gitlab,(之后补充从本地获取)
1、配置⽂件项⽬
微服务注册中心有哪些此项⽬下是配置⽂件,上传到gitlab
upfs-provider和upfs-service对应项⽬名称,config client端配置的name,spring.application.name
dev:开发环境
prod:⽣产环境
test:测试环境
property1: service-dev1
2、配置中⼼Conifg Server
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
UpfsConfigApplication.java
package com.urthink.upfs.upfsconfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.fig.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class UpfsConfigApplication {
public static void main(String[] args) {
SpringApplication.run(UpfsConfigApplication.class, args);
}
}
spring:
application:
name:  upfs-config
profiles:
active: dev
logging:
config: l
server:
port: 8888
spring:
cloud:
config:
server:
git:
uri: username@11.22.33.230:8082/urthink-upfs/upfs-config-resources.git
username: username
password: password
search-paths: '{application}'
uri:gitlab地址,注意:有端⼝号的加上端⼝号
search-paths:'{application}' 注意格式,带双单引号
搜索路径,对应config client端配置的name,和upfs-config-resources下的⼦⽬录名称⾃动匹配,从浏览器访问也可以。
3、Config Server测试
Config Server启动以后,我们可以通过它暴露的端点获取配置⽂件内容,http请求地址与配置⽂件映射关系如下:
# 映射{application}-{profile}.properties⽂件
/{application}/{profile}/[{label}]
/{label}/{application}-{profile}.properties
/{application}-{profile}.properties
/{label}/{application}-{profile}.yml
/{application}-{profile}.yml
{application}通常使⽤微服务名称,对应Git仓库中⽂件名的前缀;{profile}对应{application}-后⾯的dev、pro、test等;
{label}对应Git仓库的分⽀名,默认为master。
4、配置中⼼Conifg Client
UpfsServiceApplication.java
package com.urthink.upfs.upfsservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class UpfsServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UpfsServiceApplication.class, args);
}
}
ClientController.java
package com.urthink.ller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.
bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;
/**
* ClientController
*
* @author happyqing
* @version 1.0.0
* @CreateDate 2019/6/10
*/
@RestController
public class ClientController {
// 配置中⼼⽂件⾥的key
@Value("${property1}")
private String property1;
@RequestMapping(value = "/test", method = RequestMethod.GET)    public String test(){
return property1;
}
}
spring:
application:
name:  upfs-service
profiles:
active: dev
logging:
config: l
server:
port: 8080
spring:
cloud:
config:
uri: localhost:8888
profile: test
label: master
5、Conifg Client测试
⼆、配置中⼼使⽤本地配置⽂件
1、config server端
property1: native-service-native
server:
port: 8888
spring:
cloud:
config:
server:
native:
searchLocations: classpath:nativeconfig  # 配置⽂件所在⽬录,classpath(类路径)和file(系统⽂件路径)两种          #search-paths: '{application}'
native下并没有search-paths属性。
nativeconfig⽬录下可以⽤应⽤名-l⽅式命名
2、config client端

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。