SpringCloud----configServer配置中心及手动刷新

准备工作:一个注册中心项目、一个配置中心服务端及一个配置中心客户端项目。需要将配置中心服务端及客户端注册到注册中心去。使用码云作为仓库管理配置信息。

【1】配置中心服务端依赖:pom.xml

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
	</parent>
	<!-- 管理依赖 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.M7</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<!--spring-cloud 整合 config-server -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<!-- SpringBoot整合eureka客户端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

	</dependencies>
	<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/libs-milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

application.yml:

####端口号      
server:
  port: 8010
###服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8000/eureka
spring:
  application:
    ####注册中心应用名称,也是配置中心的名称,在配置中心客户端需要用到该name
    name: config-server
  cloud:
    config:
      server:
        git:
          ###git环境地址
          uri: https://gitee.com/zhoulunwu/config.git
          #如果是私有仓库,需要配置账号及密码
          ##username: 用户名
          #password: 密码
          ###下面的skip-ssl-validation为了解决: Cannot clone or checkout repository
          skip-ssl-validation: true
          ####搜索目录
          search-paths:
            - config  
      ####读取分支      
      label: master

启动类: 启动之后,可以通过访问:http://localhost:8010/config-client-dev.properties是否能正常访问。

@SpringBootApplication
// 开启配置中心服务
@EnableConfigServer
public class SpringCloudConfigServerApplication {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SpringApplication.run(SpringCloudConfigServerApplication.class, args);
	}

}

【2】配置中心客户端依赖:pom.xml

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
	</parent>
	<!-- 管理依赖 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.M7</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>

		</dependencies>
	</dependencyManagement>
	<dependencies>
		<!-- SpringBoot整合Web组件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-client</artifactId>
		</dependency>
		<!-- SpringBoot整合eureka客户端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		<!-- actuator监控中心 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		
	</dependencies>
	<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/libs-milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

bootstrap.yml:

server:
  port: 8050
#####    eureka服务注册地址    
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8000/eureka    
spring:
  application:
    ####注册中心应用名称
    name:  config-client
  cloud:
    config:
    ####读取后缀
      profile: dev
      ####读取config-server注册地址
      discovery:
        ###这里的service-id就是配置中心服务端的name
        service-id: config-server
        enabled: true
##开启监控断点  
management:
  endpoints:
    web:
      exposure:
        include: "*"

启动类:

@SpringBootApplication
@EnableEurekaClient
public class SpringCloudConfigClientApplication {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SpringApplication.run(SpringCloudConfigClientApplication.class, args);
	}

}

测试类:获取gitee中新建的config-client-dev.properties的myTestName属性。

@RestController
@RefreshScope
public class ConfigClient {

	@Value("${myTestName}")
	private String myTestName;
	
	@RequestMapping("/getMyTestName")
	public String getMyTestName() {
		return myTestName;
	}
	
}

如果能正常访问到表示配置成功

【3】手动刷新配置信息。

此时去更改gitee上面的信息,访问配置中心服务端http://localhost:8010/config-client-dev.properties可以看到内容已经更改了。但是通配置中心客户端的路径去获取,依旧是前一次的内容,并没有更改。

需要手动刷新同步:使用postman软件去同步,使用post请求;

然后再访问配置中心客户端的路径,就可以看到改变了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值