spring cloud 配置中心--本地 native

------- server pom ------------

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cloud.framework</groupId>
    <artifactId>server-cloud</artifactId>
    <packaging>war</packaging>
    <version>1.0.1-SNAPSHOT</version>
    <name>server-cloud Maven Webapp</name>
    <url>http://maven.apache.org</url>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.RC1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- web -->
        <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>
    
     <build>
        <finalName>server-cloud</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
 

----------------- application.properties -------------------

server.port=9091

#
management.security.enabled=false

-----------------bootstrap.properties-----------------------------

spring.application.name=cloud-server

#set profiles 
spring.profiles.active=native 
spring.cloud.config.server.native.search-locations=file:\\D:\\Users\\xuzhi268\\zhongchou\\config_profiles

#file:///D:/Users/xuzhi268/zhongchou/config_profiles

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

@SpringBootApplication
@EnableConfigServer
public class ServerCloudStarterRunner {
    
    public static void main(String[] args) {
        System.out.println("======= " + Thread.currentThread().getName() + " 开始启动。");
        new SpringApplicationBuilder(ServerCloudStarterRunner.class).web(true).run(args);
        System.out.println("======= " + Thread.currentThread().getName() + " 启动成功。");
    }
    
}

---------------------client pom ------------------------

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cloud.framework</groupId>
    <artifactId>sms-cloud</artifactId>
    <packaging>war</packaging>
    <version>1.0.1-SNAPSHOT</version>
    <name>sms-cloud Maven Webapp</name>
    <url>http://maven.apache.org</url>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.RC1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</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-config-client</artifactId>  
        </dependency>  
          
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-actuator</artifactId>  
        </dependency> 
    </dependencies>
    <build>
        <finalName>sms-cloud</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>  
                    <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>  
                </configuration> 
            </plugin>
        </plugins>
         
    </build>
</project>
 

--------------bootstrap.properties-----------------

#set profiles 
spring.application.name=sms-cloud

spring.cloud.config.uri=http://localhost:9091/

--------------application.properties-------------------

server.port=9092

management.security.enabled=false

@SpringBootApplication
@EnableDiscoveryClient
@ComponentScan(basePackages = {"com.cloud.framework.sms"})
public class SmsStarterRunner {
    public static void main(String[] args) {
        System.out.println("======" + Thread.currentThread().getName() + "[" + SmsStarterRunner.class.getSimpleName() + "]启动开始。");
        new SpringApplicationBuilder(SmsStarterRunner.class).web(true).run(args);
        System.out.println("======" + Thread.currentThread().getName() + "[" + SmsStarterRunner.class.getSimpleName() + "]启动成功。");
    }
}
 

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;

@RestController
@RefreshScope
public class ParentController {

    @Value("${sms.send.template.content}")
    private String templateContent;
    
    @GetMapping(value = "/sendSms.do")
    public String sendSms (String body) {
        try {
            System.out.println("开始发送短信{" + body + "}");
            System.out.println("短信模板内容{" + templateContent + "}");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return body + " 发送成功";
    }
    
}

--- http://localhost:9092/refresh -- 动态刷新客户端配置

猜你喜欢

转载自my.oschina.net/u/2510361/blog/1819832