SpringCloud实践引入注册中心+配置中心

  随着服务数量的增多,尤其是多数项目涉及jni本地方法的调用,所需参数配置较多,同时内存溢出等维护问题时常发生.鉴于此,原tomcat集群的使用已难满足需求,而微服务的思想契合当前项目实践,特在服务端构建起高可用eureka_server注册中心集群/config_server配置中心集群,完成对应用和git配置文件的管理.同时考虑到服务器集群并发清洗数据的必要性,构建起了ribbon+zuul负载均衡集群(后续完成)并在实践中效果显著.整体而言,微服务的引用改善了项目开发和日常维护/迭代过程纷乱的现状.特整理注册中心和配置中心构建过程博客如下:

  一 . 环境一览:

  •   开发系统:Ubuntu 16.04 生产/测试系统:Cent OS 6
  •        版本: SpringBoot 1.5.19 + SpringCloud Edgware.SR5 (idea构建自动匹配) + JDK 1.8

  二 . 注册中心:

  1.构建maven父工程 (便于开发环境项目管理),默认即可.

  2.新建注册中心模块

  new Module -> Spring Initializr --- Module SDK: 1.8 + Initializr Service URL - Default

            -> Project Metadata:完成Group + Artifact + Version + Package的配置

         -> Dependencies:

          -> SpringBoot - 1.5.19

          -> Cloud Discovery - Eureka Server

           -> Finish

  构建过程基于 IDEA新建Maven模块流程

  3.启动类配置:

  新增 @EnableEurekaServer 注解即可.

@EnableEurekaServer
@SpringBootApplication
public class RosettaEurekaServer1Application {

    public static void main( String[] args ) {

        SpringApplication.run(RosettaEurekaServer1Application.class,args);
    }
   
}

  4.应用配置:

  注意:此处构建为 eureka_server 集群 , 故而开发对自身的注册(单实例设置为false即可),同时默认注册中心为另一实例路径,具体配置如下: 

server:
  port: 8761
eureka:
  client:
    register-with-eureka: true # Eureka Server向自己注册
    fetch-registry: true
    service-url:
      defaultZone:
        http://127.0.0.1:8762/eureka
  server:
    enable-self-preservation: false
    wait-time-in-ms-when-sync-empty: 0
spring:
  application:
    name: resetta_eureka_server
  jackson:
    time-zone: GMT+8

  5.同上,构建另一eureka注册中心实例

  结合4,配置如下 ( 注意: 实例名相同 ):

server:
  port: 8762
eureka:
  client:
    register-with-eureka: true 
    fetch-registry: true
    service-url:
      defaultZone:
        http://127.0.0.1:8761/eureka
  server:
    enable-self-preservation: false
    wait-time-in-ms-when-sync-empty: 0
spring:
  application:
    name: resetta_eureka_server
  jackson:
    time-zone: GMT+8

  6.构建 eureka_client 测试

  1) 新建 eureka_client 模块:

  同 二 - 2 , 只在 pom依赖环节改动如下:

   Dependencies:

      -> Spring Boot - 1.5.19

      -> Cloud Discovery - Eureka Discovery

   Finish

  2) 启动类配置:  

  新增 @EnableDiscoveryClient 注解

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientTestApplication.class, args);
    }

}

  3) 应用配置:

  注意:因由多个注册中心,故在配置时指定多个实例,具体如下:

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka, http://127.0.0.1:8762/eureka
server:
  port: 8777
spring:
  application:
    name: eureka_client
  cloud:
    config:
      allow-override: true
      override-system-properties: false
# 在eureka集群中,需注意关闭安全组件
management:
  security:
    enabled: false

  7.依次启动注册中心/测试实例

  注:idea有一好用的实例启动视图 - RunDashbord . 如果实例启动未加载至该视图 , 可做以下修改

    a. 找到项目根目录下 .idea - workspace.xml 配置文件

    b. 找到文件夹下的 RunDashboard 组件, 设置 配置类型( configurationTypes )为 ( SpringBootApplicationConfigurationType ) 

    c. 最终该组件配置如下:

<component name="RunDashboard">
    <option name="configurationTypes">
      <set>
        <option value="SpringBootApplicationConfigurationType" />
      </set>
    </option>
    <option name="ruleStates">
      <list>
        <RuleState>
          <option name="name" value="ConfigurationTypeDashboardGroupingRule" />
        </RuleState>
        <RuleState>
          <option name="name" value="StatusDashboardGroupingRule" />
        </RuleState>
      </list>
    </option>
</component>

    d.Run Dashboard 视图启动效果如下:

  项目启动完毕后,进入 localhost:8761 / localhost:8762 查看各实例运行状态,截图如下:

  三 . 配置中心

  配置中心的构建,有些分歧,这里暂提供行之有效的终版也是简略版.

  1. 基于 二 中 maven 父工程 , 新建配置中心 模块

  同 二 - 2 , 只在 pom依赖环节改动如下:

    Dependencies:

      -> Spring Boot - 1.5.19

      -> Cloud Config - Config Server

      -> Cloud Discovery - Eureka Discovery

  2. 启动类配置

  新增 @EnableConfigServer / @EnableEurekaClient 注解 : 

@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication
public class RosettaConfigServer1Application {

    public static void main(String[] args) {
        SpringApplication.run(RosettaConfigServer1Application.class, args);
    }

}

  3. 应用配置

eureka:
  client:
    service-url:
      defaultZone: http://172.18.28.100:8761/eureka, http://172.18.28.100:8762/eureka
server:
  port: 8768
spring:
  application:
    name: rosetta_config_server
  cloud:
    config:
      server:
        git:
          uri: git@*.*.*.*:~/git/rosetta_cloud_config.git # 管理配置文件git服务器端路径
          search-paths: test # 对应的配置文件路径
          username: git
          password: 123456
      label: master # 分支

  4. 构建配置中心另一实例

  同 3 , 除 端口不同

  5. 启动测试 , 及 git服务端配置文件命名规约

  Configuration Server 端点与配置文件的映射规则如下:

/{applicaiton}/{profile}[/{label}]
/{appllication}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

  启动配置中心实例,根据上述规约测试配置信息如下:

 

  6. Config Client 的使用

  1) 根据 二 中 maven 父工程 , 新建配置客户端 模块:

  pom依赖方面, 因需要时常刷新配置,故需采用 config client 结合 actuator的 /refresh的使用方式.依赖如下:

    Dependencies:

      Spring Boot - 1.5.19

      Cloud Config - Config Client

      Cloud Discovery - Eureka Discovery

      Ops - Actuator 

  2) 启动类配置:        

     此处只需保留 @EnableEurekaClient 即可 

  3) 启动配置:

  application.yml

  management.security.enabled 置为 false , 免去调用/refresh时的权限验证

eureka:
  client:
    service-url:
      defaultZone: http://172.18.28.100:8761/eureka, http://172.18.28.100:8762/eureka
spring:
  application:
    name: test-config
server:
  port: 8771
management:
  security:
    enabled: false

  bootstrap.yml

spring:
  cloud:
    config:
      fail-fast: true
      label: master
      profile: dev
      discovery:
        enabled: true
        service-id: rosetta_config_server

  4). 配置文件类:

@Component
@ConfigurationProperties(prefix = "test")
@RefreshScope
public class PersonConfigRemote {

    private String husband;
    private String wife;

    public String getHusband() {
        return husband;
    }

    public void setHusband(String husband) {
        this.husband = husband;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }
}

  5) 接口测试:

@RestController
@RequestMapping("/test")
public class TestConfig {

    @Autowired
    private PersonConfigRemote personConfigRemote;

    @GetMapping
    public String test(){
        return personConfigRemote.getHusband() + " --- " + personConfigRemote.getWife();
    }

}

  6) 配置信息刷新

  Spring提供了@ConfigurationProperties注解,可以将配置属性映射到一个JavaBean ,  而且 Actuator 导出 /refresh 服务 , 每当调用这个服务的时候,被@ConfigurationProperties标注的Bean就会刷新属性值 .

  注 : /refresh 为 POST 请求.

  7) 测试 

  7. 追加

  关于 Spring Cloud Bus + rabbit mq 

  新增pom:

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

  应用配置如下:

spring:
  application:
    name: rosetta_config_server
  cloud:
    bus:
      trace:
        enabled: true # 开启cloud bus的跟踪
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest
management:
  security:
    enabled: false

  rabbit mq 安装使用 : 

  docker -> rabbit mq

docker pull rabbitmq:3-management
docker run -d --hostname localhost --name myrabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-management

参数说明:
-d 后台进程运行
hostname RabbitMQ主机名称
name 容器名称
-p port:port 本地端口:容器端口
-p 15672:15672 http访问端口
-p 5672:5672 amqp访问端口

  注:映射2个端口:15672是Web管理界面的端口;5672是MQ访问的端口。

  实际应用中,调用 Config Client实例的 /bus/refresh 端点 , 注意 : POST 请求

原文地址:https://www.cnblogs.com/nyatom/p/10288747.html