Spring Cloud --08--Config 配置中心

Config 配置中心

在这里插入图片描述

统一管理和维护配置文件

  • yml 配置文件保存到 git 服务器,例如 github.com 或 gitee.com
  • 微服务启动时,从服务器获取配置文件

gitee 上存放配置文件

1.新建 “Project”空模块,命名为 config

2.将sp02,sp03,sp04,sp11四个项目的yml配置文件,复制到config项目,并改名

  • item-service-dev.yml
  • user-service-dev.yml
  • order-service-dev.yml
  • zuul-dev.yml

在这里插入图片描述

3.最后2,3,4,11 的 application.yml 文件全部删除(或全部注释)

禁止配置中心的配置信息覆盖客户端配置

默认配置中心配置优先级高,配置中心配置会覆盖客户端的所有配置,包括命令行参数配置,这样我们在item-service和order-service中配置的端口号启动参数会无效

在这里插入图片描述
我们可以设置禁止配置中心的配置将客户端配置覆盖掉

spring:
  ......
  cloud:
    config:
      override-none: true

4.将 config 项目上传到 gitee

在这里插入图片描述

搭建配置中心

流程;

  1. 添加 config server依赖
  2. application.yml配置gitee地址
  3. 主程序添加 @EnableConfigServer 和 @EnableDiscoveryClient

1.添加 config server依赖

新建 sp12-config 项目
在这里插入图片描述
在这里插入图片描述
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>cn.tedu</groupId>
	<artifactId>sp12-config</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>sp12-config</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

2.application.yml配置gitee地址

spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/gaogao2020/springcloud1
          search-paths: config
  application:
    name: config-server

eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
server:
  port: 6001

在这里插入图片描述

3.主程序添加 @EnableConfigServer 和 @EnableDiscoveryClient

package cn.tedu.sp12;
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
@EnableDiscoveryClient
@SpringBootApplication
public class Sp12ConfigApplication {

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

}

启动,访问测试

在这里插入图片描述

配置中心的客户端

流程;

  1. 添加 Config Client 依赖

  2. application.yml直接删除(或注释全部代码)

  3. 新建配置文件 bootstrap.yml

    • eureka 连接信息
    • 从eureka获取配置中心的地址
    • 连接配置中心下载指定的配置文件

1. 添加 Config Client 依赖

  • 右键点击项目,编辑起步依赖,添加 config client 依赖

在这里插入图片描述

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

2. application.yml直接删除(或注释全部代码)

3. 新建配置文件 bootstrap.yml

bootstrap.yml,引导配置文件,先于 application.yml 加载

  • item-service
spring: 
  cloud:
    config:
      discovery:
        enabled: true
        service-id: config-server
      name: item-service
      profile: dev
      
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
  • user-service
spring: 
  cloud:
    config:
      discovery:
        enabled: true
        service-id: config-server
      name: user-service
      profile: dev
      
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
  • order-service
spring: 
  cloud:
    config:
      discovery:
        enabled: true
        service-id: config-server
      name: order-service
      profile: dev
      
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
  • zuul
spring: 
  cloud:
    config:
      discovery:
        enabled: true
        service-id: config-server
      name: zuul
      profile: dev
      
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka

启动服务,观察从配置中心获取配置信息的日志

在这里插入图片描述
在这里插入图片描述

配置刷新

spring cloud 允许运行时动态刷新配置,可以重新从配置中心获取新的配置信息

以 user-service 为例演示配置刷新

pom.xml
user-service 的 pom.xml 中添加 actuator 依赖
右键点击sp03-user-service项目,编辑起步依赖,添加 actuator 依赖
在这里插入图片描述

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

yml 配置文件中暴露 refresh 端点

  • 修改 config 项目中的 user-service-dev.yml,并提交推送到远程仓库
sp:
  user-service:
    users: "[{\"id\":7, \"username\":\"abc\",\"password\":\"123\"},{\"id\":8, \"username\":\"def\",\"password\":\"456\"},{\"id\":9, \"username\":\"ghi\",\"password\":\"789\"}]"

spring:
  application:
    name: user-service
  cloud:
    config:
      override-none: true
    
server:
  port: 8101
  
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka  

management:
  endpoints:
    web:
      exposure:
        include: refresh

UserServiceImpl 添加 @RefreshScope 注解

  • 只允许对添加了 @RefreshScope 或 @ConfigurationProperties 注解的 Bean
    刷新配置,可以将更新的配置数据注入到 Bean 中
ackage cn.tedu.sp03.user.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.service.UserService;
import cn.tedu.web.util.JsonUtil;

import lombok.extern.slf4j.Slf4j;

@RefreshScope
@Slf4j
@Service
public class UserServiceImpl implements UserService {
	@Value("${sp.user-service.users}")
	private String userJson;
	
	@Override
	public User getUser(Integer id) {
		log.info("users json string : "+userJson);
		List<User> list = JsonUtil.from(userJson, new TypeReference<List<User>>() {});
		for (User u : list) {
			if (u.getId().equals(id)) {
				return u;
			}
		}
		
		return new User(id, "name-"+id, "pwd-"+id);
	}

	@Override
	public void addScore(Integer id, Integer score) {
		// 这里增加积分
		log.info("user "+id+" - 增加积分 "+score);
	}

}

重启配置中心, 再重启sp03, 查看暴露的刷新端点

  • 查看暴露的刷新端点
    http://localhost:8101/actuator

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值