您的位置:首页 > 编程语言 > Java开发

配置中心搭建(spring-cloud-config-server)

2019-01-11 20:14 537 查看

开发十年,就只剩下这套架构体系了! >>>   

1.github创建配置库

2.配置服务端

①创建项目

②导入jar

<!--springboot支持-->
<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>
</dependency>

<!--eureka客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--配置中心支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

③yml配置

server:
port: 8848
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
instance:
prefer-ip-address: true
spring:
application:
name: ruigou-config-server
cloud:
config:
server:
git:
uri: https://github.com/xxxxx/application_config.git
username: xxxxx
password: xxxxxxxx

④入口

@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigServerApplication_8848 {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication_8848.class);
}
}

⑤测试

localhost:8848/application-plat-dev.yml

3.配置客户端

  • Eureka,configserver不需要,其他的都是客户端

①创建项目

②导入jar

<!--配置中心支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

<!--打包插件依赖-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>cn.rui97.ruigou.PlatApplication_8001</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

③做配置

spring:
profiles:
active: dev
cloud:
config:
name: application-plat #github上面名称
profile: ${spring.profiles.active} #环境 java -jar -D xxx jar
label: master #分支
discovery:
enabled: true #从eureka上面找配置服务
service-id: ruigou-config-server #指定服务名
#uri: http://127.0.0.1:1299 #配置服务器 单机配置
eureka: #eureka不能放到远程配置中
client:
service-url:
defaultZone: http://localhost:7001/eureka  #告诉服务提供者要把服务注册到哪儿 #单机环境
instance:
prefer-ip-address: true #显示客户端真实ip

④打包测试

  • 命令:java -jar -Dspring.profiles.active=test plat_services_8001-1.0-SNAPSHOT.jar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: