7.配置服务器(config)

来源:互联网 发布:java多线程上传文件 编辑:程序博客网 时间:2024/06/04 17:46

1.config介绍

由于springcloud服务众多,在众多服务启动的情况下单独使用一个config服务器(配置服务器),其他所有的服务都在这个配置服务器中读取配置并应用。下面写一个配置服务器和一个服务,使用服务去读取配置服务器上的配置。这个配置有本地和远程两种获取方式,config服务器通过两种方式获取到配置之后传给其他各个服务


2.config服务器

(1)pom.xml
<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.tyf</groupId>  <artifactId>config_test</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>jar</packaging>  <name>config_test</name>  <url>http://maven.apache.org</url>  <!-- 1 -->  <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.2.RELEASE</version>        <relativePath/>   </parent>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>  <!-- 2 -->  <dependencies>  <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</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>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-eureka</artifactId>        </dependency>  </dependencies>    <!-- 3 -->  <dependencyManagement>        <dependencies>            <dependency>                <groupId>org.springframework.cloud</groupId>                <artifactId>spring-cloud-dependencies</artifactId>                <version>Camden.SR6</version>                <type>pom</type>                <scope>import</scope>            </dependency>        </dependencies>  </dependencyManagement>      <!-- 4 -->  <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>  </build>    <!-- 5 -->  <repositories>        <repository>            <id>spring-milestones</id>            <name>Spring Milestones</name>            <url>https://repo.spring.io/milestone</url>            <snapshots>                <enabled>false</enabled>            </snapshots>        </repository>  </repositories>  </project>
(2)resources/application.properties
server.port=8888#active指明配置地址在native(本地)spring.profiles.active = native#searchLocations指明配置文件的位置spring.cloud.config.server.native.searchLocations=classpath:/properties/
(3)创建一个配置文件:classpath/properties/com-dataSource.properties

url=192.168.100.100

这个文件名称:configClient-dataSource.properties

我们可以直接访问config服务器读取这个文件:http://localhost:8888/configClient/dataSource(第(5)步测试)

前面configClient是一个服务名称,这个文件是这个服务的配置文件,等会儿我们创建这个服务来读取这个文件

(4)启动类

package com.tyf.config_test;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication@EnableConfigServerpublic class App {    public static void main( String[] args )    {     SpringApplication.run(App.class, args);    }}

(5)启动config-server

访问:http://localhost:8888/configClinet/dataSource

返回json结果如下

 * "name":"com", * "profiles":["dataSource"], * "label":null, * "version":null, * "state":null, * "propertySources":[ * {"name":"classpath:/properties/configClient-dataSource.properties", *  "source":{"url":"192.168.100.100"}}]}
可以看到读取一个配置返回的完整信息包括六个数据,最后propertySources就是读取到的数据

3.写一个服务去访问配置服务器,读取上面的配置文件

(1)pom.xml
<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.tyf</groupId>  <artifactId>config-client</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>jar</packaging>  <name>config-client</name>  <url>http://maven.apache.org</url>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>  <!-- 1 -->  <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.2.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->   </parent><!-- 2 --><dependencies>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-config</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>            <scope>test</scope>        </dependency>    </dependencies><!-- 3 -->    <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><!-- 4 -->    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build><!-- 5 -->    <repositories>        <repository>            <id>spring-milestones</id>            <name>Spring Milestones</name>            <url>https://repo.spring.io/milestone</url>            <snapshots>                <enabled>false</enabled>            </snapshots>        </repository>    </repositories></project>
(2)resources/bootstrap.properties
spring.cloud.config.uri:http://localhost:8888spring.application.name=configClientspring.cloud.config.profile=dataSource spring.cloud.config.label=master

拼接起来就是访问config-server下面configClinet-dataSource文件的地址:http://local:8888/configClient/dataSources。服务启动时,服务就会根据这个文件去获取配置文件并使用它

(3)启动类

我们在启动类中打印获取到的配置文件,并return到浏览器

@SpringBootApplication@RestControllerpublic class App {@Value("${url}")    String url;@RequestMapping("/get")    String hello() {System.out.println("config-server读取到的文件 :"+url);//response给浏览器        return "Hello " + url + "!";    }public static void main(String[] args) {    SpringApplication.run(App.class, args);}}
启动服务之后,访问这个控制器可以看到浏览器输出结果和控制台结果




原创粉丝点击