- 5.2 Building our Spring Cloud configuration server
- 5.3 Integrating Spring Cloud Config with a Spring Boot Client
- 5.3.1 Setting up the licensing service spring cloud config service dependencies
- 5.3.2 Configuration the licensing service to use Spring Cloud Config
- 5.3.3 Wiring in a data source using Spring Cloud configuration server
- 5.3.4 Directly reading properties using the @Value annotation
- 5.3.5 Refreshing your properties using Spring Cloud configuration server
- 5.3.6 Using Spring Cloud configuration server with Git
- 5.4 Protecting sensitive configuration information
5.2 Building our Spring Cloud configuration server
5.2.2 Using Spring Cloud configuration server with the filesystem
依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
基本配置文件
spring:
application:
name: config-server
server:
port: 8071
基于文件系统存储的配置
native:表示配置文件将从classpath或filesystem中获取
spring:
application:
name: config-server
profiles:
active: native
cloud:
config:
server:
native:
search-locations:
- file:///Users/illary.huaylupo
server:
port: 8071
server:
native:
search-locations:
- classpath:/config
5.2.3 Setting up the configuration files for a service
在resources下创建config目录,并添加两个properties文件,如下图
example.property= I AM THE DEFAULT
spring.jpa.hibernate.ddl-auto=none
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.jpa.show-sql = true
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.database.driverClassName= org.postgresql.Driver
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
management.endpoints.web.exposure.include=*
management.endpoints.enabled-by-default=true
example.property= I AM DEV
# DataSource settings: set here your own configurations for the database
spring.datasource.url = jdbc:postgresql://101.132.251.198:5432/test
spring.datasource.username = postgres
spring.datasource.password = root
启动项目并访问:http://localhost:8071/licensing-service/default
如果访问:http://localhost:8071/licensing-service/dev
查找dev的时候,会把default的属性一起查找出来,这是因为spring有个等级机制,当查找配置文件的时候,总是会优先找default的配置文件,然后再找相应的特定环境的文件,如果有属性冲突,就会覆盖默认的。
5.3 Integrating Spring Cloud Config with a Spring Boot Client
5.3.1 Setting up the licensing service spring cloud config service dependencies
添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.3.1</version>
</dependency>
</dependencies>
5.3.2 Configuration the licensing service to use Spring Cloud Config
配置文件
spring:
application:
name:
licensing-service
profiles:
active: dev
cloud:
config:
uri: http://localhost:8071
启动项目并访问:http://localhost:8080/actuator/env
5.3.3 Wiring in a data source using Spring Cloud configuration server
引入依赖:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
实体类:
@Data
@Entity
@Table(name = "licenses")
public class License extends RepresentationModel<License> {
@Id
@Column(name = "license_id", nullable = false)
private String licenseId;
private String description;
@Column(name = "organization_id", nullable = false)
private String organizationId;
@Column(name = "product_name", nullable = false)
private String productName;
@Column(name = "license_type", nullable = false)
private String licenseType;
@Column(name = "comment")
private String comment;
public License withComment(String comment) {
this.setComment(comment);
return this;
}
}
如果java类中的属性和数据库中表的字段属性相同,就可以不加@Column 注解
LicenseRepository interface定义:
@Repository
public interface LicenseRepository extends CrudRepository<License, String> {
List<License> findByOrganizationId(String organizationId);
License findByOrganizationIdAndLicenseId(String organizationId, String licenseId);
}
LicenseService定义:
@Service
public class LicenseService {
@Autowired
MessageSource messageSource;
@Autowired
private LicenseRepository licenseRepository;
@Autowired
ServiceConfig config;
public License getLicense(String licenseId, String organizationId) {
License license = licenseRepository.findByOrganizationIdAndLicenseId(licenseId, organizationId);
if (null == license) {
throw new IllegalArgumentException(String.format(messageSource.getMessage("license.search.error.message", null, null), licenseId, organizationId));
}
return license.withComment(config.getExampleProperty());
}
public License updateLicense(License license) {
licenseRepository.save(license);
return license.withComment(config.getExampleProperty());
}
public String deleteLicense(String licenseId) {
String responseMessage = null;
License license = new License();
license.setLicenseId(licenseId);
licenseRepository.delete(license);
responseMessage = String.format(messageSource.getMessage("license.delete.message", null, null), licenseId);
return responseMessage;
}
}
5.3.4 Directly reading properties using the @Value annotation
上述代码中引用了config.getExampleProperty()方法
public License updateLicense(License license) {
licenseRepository.save(license);
return license.withComment(config.getExampleProperty());
}
该方法的细节:
@Component
public class ServiceConfig {
@Value("${example.property}")
private String exampleProperty;
public String getExampleProperty() {
return exampleProperty;
}
}
5.3.5 Refreshing your properties using Spring Cloud configuration server
为了改变配置的时候,动态的做出反应,需要在启动类上添加一个@RefreshScope
@SpringBootApplication
@RefreshScope
public class LicensingApplication {
public static void main(String[] args) {
SpringApplication.run(LicensingApplication.class, args);
}
}
@RefreshScope该注解只会动态加载在配置文件中自定义的属性,像database configuration这样的配置不会动态的加载。为了执行refresh其他的属性,可以访问
http://
5.3.6 Using Spring Cloud configuration server with Git
5.4 Protecting sensitive configuration information
5.4.1 Setting up a symmetric encryption key
5.4.2 Encrypting and decrypting a property
根据上述的步骤,可以将配置文件中需要加密的属性值加密
{cipher}告诉spring cloud配置服务,处理的是一个加密的值