SpringCloud配置中心集成Gitlab(十五)

配置中心概述
对于传统的单体应用而言,常使用配置文件来管理所有配置,比如SpringBootapplication.yml文件,
但是在微服务架构中全部手动修改的话很麻烦而且不易维护。微服务的配置管理一般有以下需求:
集中配置管理,一个微服务架构中可能有成百上千个微服务,所以集中配置管理是很重要的。
不同环境不同配置,比如数据源配置在不同环境(开发,生产,测试)中是不同的。
运行期间可动态调整。例如,可根据各个微服务的负载情况,动态调整数据源连接池大小等
配置修改后可自动更新。如配置内容发生变化,微服务可以自动更新配置
综上所述对于微服务架构而言,一套统一的,通用的管理配置机制是不可缺少的总要组成部分。常见的
做法就是通过配置服务器进行管理。
常见配置中心
Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。
Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的
配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置
管理场景。
Disconf 专注于各种「分布式系统配置管理」的「通用组件」和「通用平台」, 提供统一的「配置管理
服务」包括 百度、滴滴出行、银联、网易、拉勾网、苏宁易购、顺丰科技 等知名互联网公司正在使用!
disconf」在「2015 年度新增开源软件排名 TOP 100(OSC开源中国提供)」中排名第16强。
Spring Cloud Config简介
Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了ClientServer两个部分,
server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并
依据此数据初始化自己的应用。

Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可
以为所有环境中的应用程序管理其外部属性。它非常适合spring应用,也可以使用在其他语言的应用
上。随着应用程序通过从开发到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用
程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置
环境,以及可以访问用于管理内容的各种工具。
Spring Cloud Config服务端特性:
HTTP,为外部配置提供基于资源的API(键值对,或者等价的YAML内容)
属性值的加密和解密(对称加密和非对称加密)
通过使用@EnableConfigServerSpring boot应用中非常简单的嵌入。
Config客户端的特性(特指Spring应用)
绑定Config服务端,并使用远程的属性源初始化Spring环境。
属性值的加密和解密(对称加密和非对称加密)

一 开始配置config服务

  • config-server

    • pom.xml

      1  <dependency>
      2             <groupId>org.springframework.cloud</groupId>
      3             <artifactId>spring-cloud-config-server</artifactId>
      4         </dependency>
      5 6         <dependency>
      7             <groupId>org.springframework.cloud</groupId>
      8             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      9         </dependency>
    • application.yml

       1 #服务名称
       2 spring:
       3   application:
       4     name: config-server
       5   cloud:
       6     config:
       7       server:
       8         git:
       9           uri: http://192.168.180.112/root/test.git
      10           username: root
      11           password: 19920220ljyp
      12           default-label: master
      13 14 15 #服务的端口号
      16 server:
      17   port: 9100
      18 19 20 #指定注册中心地址
      21 eureka:
      22   client:
      23     serviceUrl:
      24       defaultZone: http://localhost:8761/eureka/
    • 启动类

      1 @SpringBootApplication
      2 @EnableConfigServer
      3 public class ConfigServerApplication {
      4 5     public static void main(String[] args) {
      6         SpringApplication.run(ConfigServerApplication.class, args);
      7     }
      8 9 }
  • 在gitlab上配置服务的yml文件

 注:其实可以指定分支,也可以指定环境,但是建议用分支去区分,因为环境的话,会可能存在冲突问题。

product-service.yml

 1 # eureka:
 2 #   client:
 3 #     serviceUrl:
 4 #       defaultZone: http://localhost:8761/eureka/
 5 #  instance:
 6 #    instance-id: product-service8080
 7 #    prefer-ip-address: true
 8  9 server:
10   port: 8771
11 spring:
12   application:
13     name: product-service
14   zipkin:
15     base-url: http://192.168.180.113:9411/
16   sleuth:
17     sampler:
18       probability: 1
19 20 info:
21   app.name: product-servic
22   company.name: www.topcheer.com

order-service.yml

 1 server:
 2   port: 8781
 3  4  5 #指定注册中心地址
 6 eureka:
 7   client:
 8     serviceUrl:
 9       defaultZone: http://localhost:8761/eureka/
10 11 #服务的名称
12 spring:
13   application:
14     name: order-service
15   redis:
16     port: 6379
17     host: 192.168.180.113
18     timeout: 2000
19   zipkin:
20     base-url: http://192.168.180.113:9411/
21   sleuth:
22     sampler:
23       probability: 1
24 25 ###配置请求超时时间
26 hystrix:
27   command:
28     default:
29       execution:
30         isolation:
31           thread:
32              timeoutInMilliseconds: 5000
33 ribbon:
34 ##指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间。
35   ReadTimeout: 2000
36 ##指的是建立连接后从服务器读取到可用资源所用的时间。
37   ConnectTimeout: 3000
38 feign:
39   hystrix:
40     enabled: true
41 management:
42   endpoints:
43     web:
44       exposure:
45         include: "*"
46 47 48 #自定义负载均衡策略
49 #product-service:
50 #  ribbon:
51 #    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

api-gateway.yml

 1 server:
 2   port: 9001
 3  4 # spring:
 5 #   application:
 6 #     name: api-gateway
 7  8  9 10 11 #指定注册中心地址
12 # eureka:
13 #   client:
14 #     serviceUrl:
15 #       defaultZone: http://localhost:8761/eureka/
16 17 zuul:
18   routes:
19     order-service: /apigateway/**
20     product-service: /apigateway1/**
21   sensitive-headers:
22   #统一入口为上面的配置,其他入口忽略
23   #ignored-patterns: /*-service/**
24 25 hystrix:
26   command:
27     default:
28       execution:
29         isolation:
30           thread:
31              timeoutInMilliseconds: 5000
32 ribbon:
33 ##指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间。
34   ReadTimeout: 2000
35 ##指的是建立连接后从服务器读取到可用资源所用的时间。
36   ConnectTimeout: 5000
37 feign:
38   hystrix:
39     enabled: true
40 

测试:可以直接打开yml(改名:bootstrap.yml)

  • 其他服务

     1 spring:
     2   application:
     3     name: product-service
     4   cloud:
     5     config:
     6       discovery:
     7         service-id: CONFIG-SERVER
     8         enabled: true
     9       label: master
    10 添加pom.xml
    11 
    12             <dependency>
    13                 <groupId>org.springframework.cloud</groupId>
    14                 <artifactId>spring-cloud-config-client</artifactId>
    15             </dependency> 

可以发现通过config-server可以读取配置,正常可以调用。

 

原文地址:https://www.cnblogs.com/dalianpai/p/11728771.html