Spring Cloud Consul使用——配置中心

一、pom.xmljava

<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>com.lynch</groupId>
    <artifactId>spring-cloud-consul-config</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-config</artifactId> </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <version>1.4.5.RELEASE</version>
        </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>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

 

二、添加 bootstrap.yml 配置文件web

spring: application: name: myconsul cloud: consul: host: localhost port: 8500 config: enabled: true #false禁用Consul配置,默認true format: YAML # 表示consul上面文件的格式 有四種 YAML PROPERTIES KEY-VALUE FILES #data-key: configuration #表示consul上面的KEY值(或者說文件的名字) 默認是data data-key: data #表示consul上面的KEY值(或者說文件的名字) 默認是data #prefix設置配置值的基本文件夾 #defaultContext設置全部應用程序使用的文件夾名稱 #profileSeparator設置用於使用配置文件在屬性源中分隔配置文件名稱的分隔符的值 profiles: active: dev server: port: 8081


三、StudentConfig——配置Java類spring

package com.lynch.config; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "student") public class StudentConfig { private String name; private int age; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "StudentConfig{" + "name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + '}'; } }

 

四、StudentController——配置測試類apache

package com.lynch.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.lynch.config.StudentConfig; @RestController @RequestMapping("/test") public class StudentController { @Value("${myName}") private String myName; @Autowired private StudentConfig studentConfig; @RequestMapping("/myname") public String testHello() { System.out.println("my name is : " + myName); return myName; } @RequestMapping("/config") public String testConfig() { System.out.println(studentConfig.toString()); return studentConfig.toString(); } }

 

五、程序入口類添加註解@EnableConfigurationPropertiesbootstrap

package com.lynch; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import com.lynch.config.StudentConfig; @SpringBootApplication @EnableDiscoveryClient @EnableConfigurationProperties({StudentConfig.class}) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }

注意:屬性配置類的class須要添加到springboot的屬性配置註解裏面,
eg:@EnableConfigurationProperties({StudentConfig.class})springboot

不添加的話,不能經過@Autowired註解,注入屬性配置類,那麼就須要在屬性配置類上使用spring的bean註解,標記時一個bean到這裏,代碼已經完成了,啓動consul服務器,下面在consul裏面進行配置。服務器

六、配置consul key/value
app

輸入key和value key:config/myconsul,dev/data value: myName: jack student: name: jack age: 18 sex: 男


注意:
a、默認狀況下,consul配置默認存儲在/config文件夾中
b、myconsul爲spring.application.name值,dev爲spring.profiles.active值,data爲data-key值
c、value用的是yml格式的配置,冒號後面有一個空格。maven

七、運行程序測試
7.一、測試經過@Value注入ide

@Value("${myName}") private String myName;

http://localhost:8081/test/myname

7.二、測試經過@ConfigurationProperties進行屬性配置
測試url:http://localhost:8081/test/config

控制檯打印信息:
my name is : jack
StudentConfig{name='jack', age=18, sex='男'}

八、總結      
到這裏consul的簡單使用就完成了,consul有兩個功能,一個是consul做爲註冊中心,另外一個是consul做爲配置中心。

在本文中consul做爲配置中心,有一個點須要注意,經過@Value注入的屬性,修改consul的配置後,屬性不能當即生效,須要服務重啓。而經過@ConfigurationProperties注入的屬性,修改consul的配置後,屬性會當即生效,因此建議若是須要動態生效的配置,最好使使用@ConfigurationProperties進行屬性的注入。

相關文章
相關標籤/搜索