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

springconfig配置

2020-05-05 12:18 621 查看

一、配置类服务器的搭建

1.登录github.com,创建一个自己的账号和仓库,并获取仓库地址: debugggcloud_config_demo

转存失败重新上传取消

2.从本地上传一个yml文件上去,内容如下

[code]spring:
profiles:
active:
- dev
---
spring:
profiles: dev     #开发环境
application:
name: debugggcloud-config-debuggg-dev
---
spring:
profiles: test   #测试环境
application:
name: debugggcloud-config-debuggg-test
#  请保存为UTF-8格式

3.创建一个配置类module: debugggcloud_config_3344

转存失败重新上传取消

4.pom的复制

这里最重要的包是

[code]        <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

全部内容如下

[code]<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>debugggcloud</artifactId>
<groupId>com.deubggg</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>debugggcloud_config_3344</artifactId>

<dependencies>
<!-- springCloud Config -->
<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.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</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-jetty</artifactId>
</dependency>
<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>
<!-- 热部署插件 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>

5.yml内容如下:

[code]server:
port: 3344

spring:
application:
name:  debugggcloud-config
cloud:
config:
server:
git:
uri: https://github.com/mx342/debugggcloud_demo_config.git
username: username #账号
password: password #密码

6.启动类创建,并添加注解:@EnableConfigServer

[code]package com.debuggg.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

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

7.启动,并测试:

效果如下图:
转存失败重新上传取消

二、远程配置应用到具体的服务的搭建

1.将如下配置文件上传到上面创建的git仓库里面去

[code]spring:
profiles:
active:
- dev
---
server:
port: 7001 #注册中心占用7001端口,冒号后面必须要有空格

spring:
profiles: dev
application:
name: debugggcloud-config-eureka-client

eureka:
instance:
hostname: eureka7001.com #冒号后面必须要有空格
client:
register-with-eureka: false #当前的eureka-server自己不注册进服务列表中
fetch-registry: false #不通过eureka获取注册信息
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
---
server:
port: 7001 #注册中心占用7001端口,冒号后面必须要有空格

spring:
profiles: test
application:
name: debugggcloud-config-eureka-client

eureka:
instance:
hostname: eureka7001.com #冒号后面必须要有空格
client:
register-with-eureka: false #当前的eureka-server自己不注册进服务列表中
fetch-registry: false #不通过eureka获取注册信息
service-url:
defaultZone: http://eureka7001.com:7001/eureka/

2.将之前的eureka项目中添加一个pom坐标:

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

3.创建一个bootstrap.yml(bootstrap的优先级比application还要高)

具体内容如下:

[code] spring:
cloud:
config:
name: debugggcloud_config_client #需要从github上读取的资源名称,注意没有yml后缀名
profile: test   #本次访问的配置项
label: master
uri: http://config-3344.com:3344  #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址

烤火的猫 原创文章 36获赞 0访问量 722 关注 私信
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: