Spring Cloud_34_SpringCloud使用配置中心

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zhaozao5757/article/details/79937181

SpringCloud使用配置中心

  • 之前章节讲过:配置服务器决定去哪里读取配置文件,配置客户端决定读取什么配置文件

1、准备工作

  • eureka-server:Eureka服务器,端口为8761
  • eureka-config-server:Eureka客户端,同时也是配置服务器,端口为8899
  • eureka-config-client:Eureka客户端,同时也是配置客户端,端口为8081,在本例中,充当普通的服务实例角色
  • eureka-zuul:Eureka客户端,集群网关,也是配置客户端,会到配置服务器抓取路由规则,端口为9000
  • eureka-bus:在本例中,它主要向消息中间件(RabbitMQ等)发送消息,通知所有的节点更新配置,端口为10000

2、配置服务器、客户端整合Zuul

2.1、eureka-server

2.1.1、依赖

<!-- Spring Cloud -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<!-- Eureka Server -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

<!-- Config -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

2.1.2、配置

server:
  port: 8761
eureka:
  client: 
    register-with-eureka: false
    fetch-registry: false

2.1.3、启动

package com.atm.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApp {

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

2.2、eureka-config-server

2.2.1、依赖

<!-- SpringCloud -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<!-- Config Server -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

<!-- SpringBoot Config -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

<!-- SpringBoot 整合 Eureka -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

<!-- SVN Kit -->
<dependency>
    <groupId>org.tmatesoft.svnkit</groupId>
    <artifactId>svnkit</artifactId>
    <version>1.9.0</version>
</dependency>

2.2.2、配置

server:
  port: 8899
spring:
  application:
    name: eureka-config-server
  profiles:
    active: subversion
  cloud:
    config:
      server:
       svn:
         uri: https://USER-20170523PW/svn/my-aitemi/
         username: aitemi
         password: aitemi
       default-label: eureka
management:
  security:
    enabled: false
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  • 与之前章节的配置基本相同,仅仅是多了一个eureka的注册配置

2.2.3、启动

package com.atm.cloud;

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

@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class EurekaConfigServerApp {

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

2.3、eureka-config-client

2.3.1、依赖

<!-- SpringCloud -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<!-- Config Client Start -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

<!-- Eureka Client Start -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

<!-- 查看集群实例端点,健康监控 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>1.5.4.RELEASE</version>
</dependency>

<!-- RabbitMQ 刷新配置,AMQP -->
<!-- 由于配置客户端会作为一个消息消费者接收RabbitMQ发送的消息,因此需要引入此依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

2.3.2、配置bootsrap.yml

server:
  port: 8081
management:
  security:
    enabled: false
spring:
  application:
    name: eureka-config-client
  cloud:
    config:
      discovery:
        enabled: true
        service-id: eureka-config-server
      profile: dev
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  • 由于配置服务器已经注册到Eureka中,完全有可能部署多个配置服务实例,因此开启客户端的配置发现功能,让客户端去发现、使用配置服务的服务(因为已经注册在Eureka中了,之前已经学习过,可以直接通过service-id进行访问)
  • 相对于之前的直接配置IP地址,使用service-id来配置服务可以使用服务器与客户端之间的关系变得松散,也增加了灵活性

2.3.3、启动

package com.atm.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class EurekaConfigClientApp {

    public static void main(String[] args) {
        SpringApplication.run(EurekaConfigClientApp.class, args);
    }
}
  • 此时程序的结构图如下

2.4、eureka-zuul

2.4.1、依赖

<!-- SpringCloud -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<!-- Config Client -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- Eureka Client -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- Zuul -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>1.5.4.RELEASE</version>
</dependency>

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

2.4.2、配置bootstrap.yml

spring:
  application:
    name: eureka-config-zuul
  cloud:
    config:
      discovery:
        enabled: true
        service-id: eureka-config-server
      profile: dev
server:
  port: 9000
management:
  security:
    enabled: false
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

2.4.3、启动

package com.atm.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class EurekaZuulApp {

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

2.4.4、eureka-config-zuul-dev.yml

zuul:
  routes:
    routeTest:
      path: /routeTest
      url: http://www.souhu.com

2.5、eureka-bus(刷新配置)

  • 每次修改完配置文件,需要逐个端点去刷新/refresh(其他问题:网络延迟,部分刷新失败,刷新节点暴露),这明显是不科学的,所以使用eureka-bus总线项目,当外部程序访问/bus/refresh时,使用消息中间件通知其他项目进行配置的刷新
  • eureka-bus:消息生产者
  • eureka-config-client,eureka-zuul:消息消费者

2.5.1、依赖

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--eureka-config-client、eureka-zuul、eureka-bus均需要依赖该依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

2.5.2、配置

server:
  port: 10000
spring:
  application:
    name: eureka-bus
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
management:
  security:
    enabled: false
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

2.5.3、启动

package com.atm.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class EurekaBusApp {

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

3、刷新单个节点配置

在一些情况下,SVN更新了多个项目的配置,单调用者只希望刷新其中一个项目的配置,要实现这样的需求,可以直接访问该项目的/refresh端点。但是,在实际环境中,集群中的节点有可能根本不对外开放,此时,可以为总线项目的/bus/refresh端点添加destination请求参数,刷新指定项目配置

/bus/refresh?destination=eureka-zuul:9000

参数格式“服务ID:端口”,如果要树心该服务的全部实例,可以使用eureka-zuul:**

展开阅读全文

没有更多推荐了,返回首页