SpringCloud Config
一般我们的项目配置文件都直接放在src下,或者resource文件夹下。单独项目这样很ok,但是如果我们搭建分布式系统,一个请求可能要通过几个模块共同协作来完成,为了方便服务配置文件统一管理,方便维护,就有必要构建一个配置中心了,让各个模块通过路由规则等方式在项目启动的时候按需加载各自需要的对应的配置。
引入SpringCloud Config,我们可以把我们的配置文件集中放到git仓库里,然后建立一个ConfigServer来统一管理维护我们的配置文件,然后我们本地修改后推送到远程仓库。所有的服务都可以通过ConfigServer来读取配置中心的配置,这里服务的位置就相当于ConfigClient了,在配置ConfigClient时,要配置ConfigServer的位置,以及本服务的名称,已经用到的profiles,从而通过一定的规则读取对应的配置信息。
我们也可以对ConfigServer配置集群,这样就算某一个配置中心实例不可用了,服务还可以通过其他健康的配置中心继续使用。
以下是网上找到的一张图
废话不多说直接上代码:
1.Pom.xml文件
//服务发现客户端
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
//SpringColud Config依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
//继承一些配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2.配置文件
server:
port: 8002
//服务名称
spring:
application:
name: ConfiServer
profiles:
active: native
cloud:
config:
server:
native:
//这里我配置的本地的资源路径
searchLocations: C:/config
//服务注册
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
3.引导类
@SpringBootApplication
//服务发现,让服务注册中心发现并注册
@EnableDiscoveryClient
//启动ConfigServer配置
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
System.out.println("配置服务器启动了!!!");
}
}
启动我们的服务
查看一下我本地的资源目录:
访问一下我们的中心配置服务:
http://localhost:8002/client/dev
一下是对应的client-dev.yml文件
这样我们就访问到配置中心服务了·····
下来我们通过ConfigClient来访问配置中心并读取配置
这里我把服务注册中心和配置中心放到Ali云服务器上 服务器地址 47.107.82.76
对应的服务注册和配置中心地址都变为47.107.82.76
1.看一下我本地configclient pom.xml
//服务发现客户端
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
//web模块 必须要
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
//config客户端依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2.配置文件
spring:
application:
name: client #指定配置文件的应用名
cloud:
config:
uri: http://47.107.82.76:8002 #指定Config服务端地址
profile: dev #指定环境
label: master #分支
//服务注册中心配置
eureka:
client:
service-url:
defaultZone: http://47.107.82.76:8001/eureka/
3.引导类配置服务发现
@SpringBootApplication
//启动服务发现
@EnableDiscoveryClient
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
System.out.println("配置客户端启动了!");
}
}
4.编写一个Contirller来测试
@Controller
@RequestMapping("/")
@ResponseBody
public class hello {
@Value("${name}")
private String nickName;
@RequestMapping("/hello")
public String hello()
{ return "hello " + nickName; }
}
5.服务器路径下的配置文件
/opt/configresource
我们启动一下,测试一下。
看到我们成功获取到Aliyun服务器上配置中心的配置文件。