集群配置中心-----整合使用
Spring cloud config与其他框架进行整合使用,包括Eureka、Zuul、Spring Cloud Bus等
一:项目介绍
- eureka-server:Eureka服务器,端口为8761
- eureka-config-server:Eureka客户端,同时也是配置服务器,端口为8899
- eureka-config-client:Eureka客户端,同时也是配置客户端,端口为8081,在本例中,充当普通的服务实例角色
- eureka-zuul:Eureka客户端,集群网关,也是配置客户端,会到配置服务器抓取路由规则,端口为9000
- eureka-bus:在本例中,它主要向消息中间件(RabbitMQ等)发送消息,通知所有的节点更新配置,端口为10000
二: Eureka服务端(eureka_server)
2.1 依赖项(pom.yml)
<!-- 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.2 配置项(application.yml)
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
2.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);
}
}
三:配置服务器(eureka_config_server)
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 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>
3.2 配置项(application.yml)
server:
port: 8899
spring:
application:
name: eureka-config-server
profiles:
active: subversion
cloud:
config:
server:
svn:
uri: https://localhost/svn/test-project/
username: aitemi
password: aitemi
default-label: eureka
management:
security:
enabled: false
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
3.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);
}
}
四: Eureka客户端(eureka-config-client)
4.1 依赖项(pom.xml)
在pom.xml文件中需要添引用依赖,spring-cloud-starter-config、spring-cloud-starter-eureka、spring-boot-starter-actuator、spring-cloud-starter-bus-ampq这四个依赖。
由于配置客户端会作为一个消费者接受RabbitMQ发送的消息。所以引用ampq
<!-- 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>
4.2 配置文件(bootstrap.yml)
其中一部分配置文件需要从配置服务中读取,这里在bootstrap中的配置文件,不需要再次读取配置文件中的。
PS:bootstrap的加载比application的加载要早。
- bootstrap.yml 用于应用程序上下文的引导阶段。
- application.yml 可以用来定义应用级别的,
注:
bootstrap和application的属性配置不相同时,bootstrap先加载。bootstrap里面的内容不会替换,会使得application响应属性的值失效。
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来配置服务可以使用服务器与客户端之间的关系变得松散,也增加了灵活性
- spring.cloud.config.discovery:设置时用的是service-id指的是配置客户端到配置服务器去读取配置文件,也可以写成IP。但在这个微服务集群中,配置服务器和配置客户端都注册到了eureka服务端,所以使用service-id也是可以指定到配置服务器的
4.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);
}
}
五:整合Zuul(eureka-zuul)
- eureka-zuul项目实际上也是一个配置客户端,他需要去配置服务器中获取路由配置。
- 获取到路由配置后,调用配置客户端提供的/refresh端点来刷新配置。
5.1 依赖(pom.xml)
<!-- 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>
5.2 配置项(bootstrap.yml)
spring:
application:
name: eureka-zuul
cloud:
config:
discovery:
enabled: true
service-id: eureka-config-server
profile: rel
server:
port: 9000
management:
security:
enabled: false
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
上面的配置项为:
- eureka服务器地址
- 配置服务器地址
5.3 路由配置(svn)
此外路由配置不需要再eureka-zuul项目中设置,路由配置写在SVN项目中,从项目中读取,
在SVN中,新建eureka-zuul-rel.yml文件。保存的路径为:
https://localhost/svn/test-project/eureka/
zuul:
routes:
routeTest:
path: /routeTest
url: http://www.souhu.com
该配置文件存储在SVN,配置了路由规则
访问http://127.0.0.1:9000/routeTest,跳转到搜狐网站
六 整合Spring Cloud Bus刷新配置
- 每次修改完配置文件,需要逐个端点去刷新/refresh(其他问题:网络延迟,部分刷新失败,刷新节点暴露),这明显是不科学的,所以使用eureka-bus总线项目,当外部程序访问/bus/refresh时,使用消息中间件通知其他项目进行配置的刷新
外部程序访问/bus/refresh端点后,eureka-bus会发送消息到RabbitMQ中间件,然后RabbitMQ会将消息广播到各个消费者。(spring-config-client和eureka-zuul)。这些消息客户端接受到消息后,会重新加载程序的配置。
实现以上的效果spring-config-Client和eureka-zuul要添加spring-cloud-starter-bus-ampq的依赖。