分享

spring-cloud(二)分布式配置中心[Git]

 关平藏书 2018-01-12

这篇文件简单解释下如何使用spring-cloud来搭建分布式配置中心

其实学了spring-boot后再来看spring-cloud是非常容易的。

在git仓库中创建相关文件

具体参见
https:///majinding/TM-springcloud-demo-config

创建配置中心服务端

1。 创建config-server项目
2。 在pom中引入config-server的依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-config-server</artifactId>
  5. </dependency>
  6. </dependencies>

3。 编写application.yml

  1. server:
  2. port: 8888
  3. spring:
  4. application:
  5. name: config-server
  6. cloud:
  7. config:
  8. server:
  9. git:
  10. uri: https:///majinding/TM-springcloud-demo-config
  11. # username: majinding888@foxmail.com
  12. # password:
  13. default-label: master
  14. search-paths: tm-1

4。 编写主启动类

  1. @SpringBootApplication
  2. @EnableConfigServer
  3. public class ConfigServerApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(ConfigServerApplication.class, args);
  6. }
  7. }

在启动类上增加 @EnableConfigServer

5。 启动项目

创建配置中心客户端

1。 创建config-client项目
2。 在pom文件中增加config客户端的依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-config</artifactId>
  4. </dependency>

3。 编写application.yml

  1. server:
  2. port: 8887
  3. spring:
  4. application:
  5. name: config-client
  6. cloud:
  7. config:
  8. label: master
  9. profile: dev
  10. uri: http://localhost:8888/

4。 编写beanconfig类(用于存储获取git仓库中的参数)

  1. @Configuration
  2. @RefreshScope
  3. public class BeanConfig {
  4. @Value("${tm.name}")
  5. private String name;
  6. public String getName() {
  7. return name;
  8. }
  9. public void setName(String name) {
  10. this.name = name;
  11. }
  12. }

5。 编写调用的Controller类

  1. @RestController
  2. public class SayController {
  3. @Autowired
  4. private BeanConfig beanconfig;
  5. @RequestMapping(value = "/say")
  6. public String say() {
  7. return beanconfig.getName();
  8. }
  9. }

6。 编写主启动类

  1. @SpringBootApplication
  2. public class ConfigClientApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(ConfigClientApplication.class, args);
  5. }
  6. }

7。 启动项目,浏览器访问 http://localhost:8887/say

1.png

现在来看下项目已经成功读取到了git仓库中的变量值。到这里只是介绍了配置中心的简单基本用法。


配置中心服务注册到注册中心eureka中,集群化的高可用。

在config-server项目中增加eureka依赖支持
  1. pom中加入eureka依赖
  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-eurek</artifactId>
  4. </dependency>
  1. 在application.yml配置文件中增加eureka
    1. eureka:
    2. client:
    3. registerWithEureka: true
    4. fetchRegistry: true
    5. service-url:
    6. defaultZone: http://localhost:8761/eureka/
在config-client项目中增加eureka依赖支持
  1. pom中加入eureka依赖
  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-eurek</artifactId>
  4. </dependency>
  1. 在application.yml配置文件中增加eureka,并对config进行改造
    ```
    server:
    port: 8887
    spring:
    application:
    name: config-client
    cloud:
    config:
    label: master
    profile: dev
    discovery:
    1. enabled: true
    2. service-id: config-server

eureka:
client:
registerWithEureka: true
fetchRegistry: true
service-url:
defaultZone: http://localhost:8761/eureka/

```

  1. 依次启动

    • tm-eureka-server
    • config-server
    • config-client
  2. 在浏览器访问 http://localhost:8887/say

2.png

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多