关于spring:九整合spring-cloud云服务架构-commonserviceconfig配置服务搭建

2次阅读

共计 4148 个字符,预计需要花费 11 分钟才能阅读完成。

  1. 介绍

Spring Cloud Config 为分布式系统中的内部配置提供服务器和客户端反对。应用 Config Server,您能够在所有环境中管理应用程序的内部属性。客户端和服务器上的概念映射与 Spring EnvironmentPropertySource 形象雷同,因而它们与 Spring 应用程序十分符合,但能够与任何以任何语言运行的应用程序一起应用。随着应用程序通过从开发人员到测试和生产的部署流程,您能够治理这些环境之间的配置,并确定应用程序具备迁徙时须要运行的所有。服务器存储后端的默认实现应用 git,因而它轻松反对标签版本的配置环境,以及能够拜访用于治理内容的各种工具。很容易增加代替实现,并应用 Spring 配置将其插入。(企业架构源码能够加求球:叁五三陆二肆柒二伍玖)

  1. 引入 pom 相干 jar 包,其中 pom.xml 配置如下:
1.  <?xml version="1.0" encoding="UTF-8"?> 
2.  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
3.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
4.   <modelVersion>4.0.0</modelVersion> 

6.   <parent> 
7.   <groupId>com.ml.honghu</groupId> 
8.   <artifactId>commonservice</artifactId> 
9.   <version>0.0.1-SNAPSHOT</version> 
10.   </parent> 

12.   <artifactId>commonservice-config</artifactId> 
13.   <packaging>jar</packaging> 

15.   <name>commonservice-config</name> 
16.   <description>Config Server</description> 

18.   <dependencies> 
19.   <dependency> 
20.   <groupId>org.springframework.cloud</groupId> 
21.   <artifactId>spring-cloud-config-server</artifactId> 
22.   </dependency> 
23.   <dependency> 
24.   <groupId>org.springframework.cloud</groupId> 
25.   <artifactId>spring-cloud-starter-eureka</artifactId> 
26.   </dependency> 
27.   <dependency> 
28.   <groupId>org.springframework.boot</groupId> 
29.   <artifactId>spring-boot-starter-security</artifactId> 
30.   </dependency> 
31.   <dependency> 
32.   <groupId>org.springframework.boot</groupId> 
33.   <artifactId>spring-boot-starter-test</artifactId> 
34.   <scope>test</scope> 
35.   </dependency> 
36.   </dependencies> 

38.   <build> 
39.   <plugins> 
40.   <plugin> 
41.   <groupId>org.springframework.boot</groupId> 
42.   <artifactId>spring-boot-maven-plugin</artifactId> 
43.   <executions> 
44.   <execution> 
45.   <id>1</id> 
46.   <goals> 
47.   <goal>repackage</goal> 
48.   </goals> 
49.   </execution> 
50.   <execution> 
51.   <id>2</id> 
52.   <goals> 
53.   <goal>build-info</goal> 
54.   </goals> 
55.   </execution> 
56.   </executions> 
57.   </plugin> 
58.   </plugins> 
59.   </build> 
60.  </project>
  1. 在 src/main/java 进行 ConfigApplication.java 启动文件配置:
1.  package com.ml.honghu; 

3.  import org.springframework.boot.SpringApplication; 
4.  import org.springframework.boot.autoconfigure.SpringBootApplication; 
5.  import org.springframework.cloud.config.server.EnableConfigServer; 
6.  import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 

8.  @EnableConfigServer 
9.  @EnableEurekaClient 
10.  @SpringBootApplication 
11.  public class ConfigApplication {13.   public static void main(String[] args) {14.   SpringApplication.run(ConfigApplication.class, args); 
15.   } 
16.  }
  1. 在 src/main/resource 下进行 bootstrap.yml 配置
1.  server: 
2.  port: 8888 
3.  spring: 
4.   application: 
5.   name: commonservice-config-server 
6.   profiles: 
7.   active: discovery,native 
8.   cloud: 
9.   config: 
10.   server: 
11.   git: 
12.   uri: http://192.168.0.254/honghu.../honghu-config.git 
13.   username: honghu 
14.   password: 123456 
15.   searchPaths: config-dev 
16.  security: 
17.   basic: 
18.   enabled: true 
19.   user: 
20.   name: honghu 
21.   password: 123456 
22.  eureka: 
23.   client: 
24.   serviceUrl: 
25.   defaultZone: http://honghu:123456@localhost:8761/eureka/ 
26.   honghuZone: http://honghu:123456@localhost:8761/eureka/ 
27.   registry-fetch-interval-seconds: 300 
28.   availability-zones: 
29.   honghu: honghuZone 
30.   instance: 
31.   prefer-ip-address: true 
32.   metadataMap: 
33.   version: 1.0 
34.   variant: A 
35.   user: ${security.user.name} 
36.   password: ${security.user.password} 
37.  management: 
38.   security: 
39.   enabled: false

留神:如果不从近程 git 或者 svn 库加载配置文件信息,能够配置加载本地地址,比方 window 下配置应用:

1.  server: 
2.  port: 8888 
3.  spring: 
4.   application: 
5.   name: commonservice-config-server 
6.   profiles: 
7.   active: discovery,native 
8.   cloud: 
9.   config: 
10.   server: 
11.   <span style="color: #ff0000;">native.searchLocations: d:/honghu-config</span> 
12.  security: 
13.   basic: 
14.   enabled: true 
15.   user: 
16.   name: honghu 
17.   password: 123456 
18.  eureka: 
19.   client: 
20.   serviceUrl: 
21.   defaultZone: http://honghu:123456@localhost:8761/eureka/ 
22.   honghuZone: http://honghu:123456@localhost:8761/eureka/ 
23.   registry-fetch-interval-seconds: 300 
24.   availability-zones: 
25.   honghu: honghuZone 
26.   instance: 
27.   prefer-ip-address: true 
28.   metadataMap: 
29.   version: 1.0 
30.   variant: A 
31.   user: ${security.user.name} 
32.   password: ${security.user.password} 
33.  management: 
34.   security: 
35.   enabled: false

到此,整个 config 服务项目配置结束!!

从当初开始,我这边会将近期研发的 spring cloud 微服务云架构的搭建过程和精华记录下来,帮忙更多有趣味研发 spring cloud 框架的敌人,大家来一起探讨 spring cloud 架构的搭建过程及如何使用于企业我的项目。

正文完
 0