zoukankan      html  css  js  c++  java
  • Cngigure和BUS实现远端配置

    1 server依赖

    <?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.sselab</groupId>
    <artifactId>configserver</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <dependencyManagement>
    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Brixton.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>
    </dependencies>
    </dependencyManagement>

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

    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>


    </project>

    2 server实现

    package org.sselab;

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

    /**
    * Created by pinker on 2016/10/31.
    */
    @SpringBootApplication
    @EnableConfigServer
    public class application {
    public static void main(String[] args) {
    new SpringApplicationBuilder(application.class).web(true).run(args);
    }
    }
    server:
    port: 8888

    spring:
    application:
    name: config-server
    cloud:
    config:
    server:
    git:
    uri: https://git.oschina.net/xd03122049/springboot-config
    search-paths: config-repo
    username: xd03122049
    password: 85523970

    3 server结果

    4 client配置

    <?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.sselab</groupId>
    <artifactId>configclient</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>

    <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>

    </dependencies>

    <dependencyManagement>
    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Brixton.RELEASE</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>

    5 client实现

    spring.application.name=pinker
    spring.cloud.config.profile=dev
    spring.cloud.config.label=master
    spring.cloud.config.uri=http://localhost:8888/
    server.port=7002

    package org.sselab;

    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;

    /**
    * Created by pinker on 2016/10/31.
    */
    @SpringBootApplication
    public class application {
    public static void main(String[] args) {
    new SpringApplicationBuilder(application.class).web(true).run(args);
    }
    }

    package org.sselab.controller;

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;

    /**
    * Created by pinker on 2016/10/31.
    */
    @RefreshScope
    @RestController
    public class TestController {
    @Value("${from}")
    private String from;

    @GetMapping("/from")
    public String from(){
    return this.from;
    }

    public void setFrom(String from) {
    this.from = from;
    }

    public String getFrom() {
    return from;
    }
    }

    6 client 结果

     

    然后将server和client都注册到注册中心,略

  • 相关阅读:
    使用UpdatePanel后如果在Render中篡改输出的html代码问题解决方案
    .net项目的二次开发解决方案
    (转)C#开源资源大汇总
    实例讲解PostSharp(一)
    实例讲解PostSharp(二)
    10个故事看员工管理和激励(转)
    用日志记录LINQ中的所有增删改的SQL语句的方法
    IT管理人才必备的十大能力(转)
    揭示常见的重构误区(转)
    利用传入的Type类型来调用范型方法的解决方案
  • 原文地址:https://www.cnblogs.com/xd03122049/p/6018371.html
Copyright © 2011-2022 走看看