zoukankan      html  css  js  c++  java
  • springcloud整合config组件

    config组件

    config组件支持两种配置文件获取方式
    springcould搭建的微服务的配置文件的获取方式有两种。它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中或者本地数据源。在spring cloud config 组件中,分两个角色,一是config server,二是config client。

    server端搭建步骤:

    POM:

    <dependencies>
    <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.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    </dependencies>

    需要注册到注册中心
    application.yml
    server:
    port: 8877

    spring:
    application:
    name: cloud-config
    cloud:
    config:
    server:
    git:
    uri: git@gitee.com:in__cong/test-config.git #git上的地址
    search-paths:
    - testConfig #仓库的包名
    default-label: master #分支名
    eureka:
    instance:
    #以IP地址注册到服务中心,相互注册使用IP地址
    preferIpAddress: true
    instance-id: cloud-config
    client:
    serviceUrl:
    defaultZone: http://localhost:8761/eureka/
    然后是启动类:
    @EnableEurekaClient
    @SpringBootApplication
    @EnableConfigServer
    public class config8877Application {

    public static void main(String[] args) {
    SpringApplication.run(config8877Application.class);
    }
    }
    注意: 使用开启注解
    环境就搭建好了 访问地址:

    IP地址端口号+分支名称+文件名
    注意文件名的格式: application-dev.yml

    客户端:
    POM:
    <dependencies>
    <!--config客户端起步依赖-->
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <!--eureka客户端依赖-->
    <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-web</artifactId>
    </dependency>
    <!--微服务的监控依赖-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!--热部署-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
    </dependency>

    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
    </dependencies>
    配置文件:bootstrap.yml

    server:
    port: 9966

    spring:
    application:
    name: config-client1
    cloud:
    config:
    label: master #分支名
    name: application #要拉取的文件名
    profile: dev #那个环境的文件
    uri: http://localhost:8877 #服务端的IP地址和端口

    eureka:
    instance:
    instance-id: config-client1 #z在eureka注册中心上显示的名字
    prefer-ip-address: true #在eureka上显示IP地址
    client:
    service-url:
    defaultZone: http://eureka-server1:8761/eureka/
    启动类:

    @SpringBootApplication
    @EnableEurekaClient
    public class ConfigClient9966Application {

    public static void main(String[] args) {

    SpringApplication.run(ConfigClient9966Application.class,args);
    }
    }

    测试: controller
    @RestController
    @RequestMapping("/config")
    public class ConfigDemoController {

    @Value("${wei.xin}")
    private String info;
    @GetMapping("/demo")
    public String getGitConfiguration(){

    return info;
    }
    }
    注意:wei.xin 这个值是我们Git上的文件配置的值:访问接口我们就拉取到git上的value值了

    简单的配置句到这里吧
  • 相关阅读:
    Oracle 11g R2 常用配置与日志的文件位置
    DBA常用SQL之会话与等待事件
    SSH框架之Spring第三篇
    SSH框架之Spring第二篇
    SSH框架之Spring第一篇
    SSH框架之Struts2第三篇
    SSH框架之Struts2第一篇
    SSH框架之Struts2第二篇
    SSH框架之Hibernate第四篇
    SSH框架之Hibernate第三篇
  • 原文地址:https://www.cnblogs.com/niCong/p/15418142.html
Copyright © 2011-2022 走看看