创建配置中心:
选择Spring Initializr模板
选择Config Server
pom文件
<?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.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.test</groupId> <artifactId>cloud-config</artifactId> <version>0.0.1-SNAPSHOT</version> <name>cloud-config</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR3</spring-cloud.version> </properties> <dependencies> <!-- 配置中心的服务端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</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> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </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>
添加服务注解:
@EnableConfigServer
@EnableEurekaClient
创建配置文件(切换配置文件的示例,在这里一并展示)
config文件夹:
dev:测试环境
prod:正式环境
config-dev.yml
registry: url: http://${host:localhost}:9010/eureka/ # 测试属性 isDebug: true
config-prod.yml
registry: url: http://${host:localhost}:9010/eureka/ # 测试属性 isDebug: false
bootstrap.yml
spring: application: name: cloud.config profiles: #native表示本地方式 active: native cloud: config: server: native: # 配置文件存放路径 search-locations: classpath:/config/
application.yml
server: port: 9020 eureka: client: service-url: defaultZone: http://localhost:9010/eureka/
我们启动 注册中心 和 配置中心
原文地址:https://www.cnblogs.com/hanjun0612/p/12575832.html
时间: 2024-12-13 12:26:10