天天看点

【Spring Cloud总结】38.Spring Cloud Config 与Eureka配合使用

接上篇《37.Spring Cloud Config的安全认证》  Spring Cloud版本为Finchley.SR2版

上两篇我们讲解了有关Spring Cloud Config的安全认证机制,其核心依赖为spring-boot-starter-security,在服务端和客户端分别配置spring.security.user.name和spring.security.user.password,spring.cloud.config.username和spring.cloud.config.password属性即可开启和获取访问认证。

本篇我们来讲解Spring Cloud Config与Eureka的配合使用。

本部分官方文档:https://cloud.spring.io/spring-cloud-static/Finchley.SR4/single/spring-cloud.html#_security

注:好像Finchley.SR2的文档已经挂了,最新的是Finchley.SR4的文档。

一、为什么Spring Cloud Config需要集成Eureka

我们先来回顾一下我们目前的代码,之前已经陆续讲解了Spring Cloud Config的简介,编写了Config Server、Config Client,进行了Git仓库的配置,配置的对称和非对称加解密,以及Spring Cloud Config的安全认证。

以Config Client为观察对象,我们查看其bootstrap.yml配置:

spring:
  cloud:
    config:
      uri: http://localhost:8090
      username: user
      password: password123
      profile: dev
      label: master
           

可以看到我们与Config Server的交互,是配置了spring.cloud.config.uri,去指定要获取配置的服务端的路径。

上面的配置,其实是有弊端的。大致有以下几个弊端:

1、弊端1:集群环境下,增加Config客户端与服务端的交互成本

例如在生产环境中,Config Server需要实现高可用(即有集群),此时Config Server有好几个同时启动的应用,利用Nginx进行负载均衡,轮询访问该Config Server:

【Spring Cloud总结】38.Spring Cloud Config 与Eureka配合使用

此时为了配合Config Server的高可用,在Config Clinet和Config Server之前架设了类似Nginx(或F5等负载均衡软硬件)的负载均衡设备,这种情况下,在原有架构的基础上,增加了组件之间的交互代价,出现异常的几率也随之增加。

2、弊端2:服务端地址硬编码

我们当初学习Eureka的目的,实际上就是为解放客户端,即我们无需在客户端去硬编码服务端的具体地址,转而使用服务名这种规范来避免这种情况,这才是服务发现的优点。

目前我们通过spring.cloud.config.uri的方式配置客户端地址,无疑是使用了硬编码,不利于拓展和维护。例如Config Server迁移了服务器,更换了ip或域名,所有的Config Client都需要更新spring.cloud.config.uri。

3、弊端3:无法享受注册中心的福利

使用Eureka等服务注册中心,可以进行服务发现、服务注册、服务心跳检测(健康监控)、服务注册表同步、服务API查询等一些列操作,而单体的Config Client和Config Server都享受不到这些。

所以,我们要将Config Client和Config Server与服务发现组件配合使用,实现Config Client端无需硬编码Config Server的地址,并能享受服务发现组件的所有福利。

二、Config Server集成Eureka

我们将Config Server集成到之前的Eureka上,这里为了好区分,我们重新创建一个Server工程。

在Eclipse中新建一个名为“microserver-config-server-eureka”的工程:

【Spring Cloud总结】38.Spring Cloud Config 与Eureka配合使用
【Spring Cloud总结】38.Spring Cloud Config 与Eureka配合使用

然后在pom.xml中引入Spring Cloud的父工程、config-server、eureka-client的依赖:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <artifactId>microserver-config-server-eureka</artifactId>
  <name>microserver-config-server-eureka</name>
  
  <parent>
        <groupId>com.microserver.cloud</groupId>
        <artifactId>microserver-spring-cloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
  
  <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>
  
</project>
           

注意,这里为了便于版本统一管理,该工程的parent父工程和我们User、Movie工程一样,均依赖于microserver-spring-cloud工程(此父工程统一引入了spring-cloud-dependencies的Finchley.SR2版,这个在前面的章节已经讲过)。

然后我们新建启动类,与之前没有差别:

package com.microserver.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigServerEurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerEurekaApplication.class, args);
    }
}
           

注:Finchley.RELEASE版本的SpringCloud不需要@EnableEurekaClient注解就可以启用Eureka注册功能。在SpringBoot的自动配置类里启用了注册功能,只要引了eureka-client的依赖就会进行注册。紧接着就是我们的配置文件,除了Spring Cloud Config配置仓库的配置,还添加了application-name应用名以及eureka的注册配置:

server:
  port: 8090
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/jackzhucoder/Spring-Cloud-Config-Test
  application:
    name: microserver-config-server-eureka

eureka:
  client:
    serviceUrl:
      defaultZone: http://user:password[email protected]:8761/eureka
  instance:
    prefer-ip-address: true
           

注:这里的eureka1是我们在host配置的127.0.0.1的映射。

然后我们重启Config Server服务,以及之前的Eureka服务:

【Spring Cloud总结】38.Spring Cloud Config 与Eureka配合使用

重启后我们访问Eureka的服务主页,可以看到我们的Config Server已经注册进去了:

【Spring Cloud总结】38.Spring Cloud Config 与Eureka配合使用

到这里,我们的Config Server集成Eureka就完成了。

三、Config Client集成Eureka

上面对Config Server集成了Eureka,那么我们的Config Client也要集成Eureka,并且需要通过Eureka访问到Config Server,并获取相应的远程配置信息。

同理我们也新建一个名为“microserver-config-client-eureka”的工程,这里我们不再赘述创建过程,基本和之前一样,只说修改了哪些地方。

pom.xml在原有的基础上,新增了eureka-client的依赖:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <artifactId>microserver-config-client-erueka</artifactId>
  <name>microserver-config-client-erueka</name>
  
  <parent>
        <groupId>com.microserver.cloud</groupId>
        <artifactId>microserver-spring-cloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
  
  <dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>
  
</project>
           

启动类:

package com.microserver.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConfigClientEurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientEurekaApplication.class, args);
    }
}
           

application.yml中的配置不变:

server:
  port: 8091
spring:
  application:
    name: microserver-config-client
    
type: abcd
           

bootstrap.yml原本的配置为:

spring:
  cloud:
    config:
      uri: http://localhost:8090
      profile: dev
      label: master  #如果ConfigServer的后端存储是Git,默认就为master
           

现在修改为:

spring:
  cloud:
    config:
      profile: dev
      discovery:
        enabled: true
        service-id: MICROSERVER-CONFIG-SERVER-EUREKA
      
eureka:
  client:
    serviceUrl:
      defaultZone: http://user:password[email protected]:8761/eureka
  instance:
    prefer-ip-address: true
           

下面很清楚,是为了注册eureka。上面我们把之前的spring.cloud.config.uri等硬编码去除了,添加了spring.cloud.config.discovery.enabled和spring.cloud.config.discovery.service-id,其中spring.cloud.config.discovery.enabled的意思是标识Config Server的服务发现是允许的(即允许从注册中心发现Config Server的uri),spring.cloud.config.discovery.service-id即需要发现的Config Server在Eureka上的注册名。这里我们开启服务发现,并指定获取的Config Server的注册名。

此时我们在启动上面两个工程的基础上,启动Config Client工程:

【Spring Cloud总结】38.Spring Cloud Config 与Eureka配合使用

然后在Config Client启动的过程中,我们可以在控制台上看到“Fetching config from server at : http://192.168.3.1:8090/”的日志:

【Spring Cloud总结】38.Spring Cloud Config 与Eureka配合使用

这个地址其实就是Config Server在Eureka上注册时的ip地址:

【Spring Cloud总结】38.Spring Cloud Config 与Eureka配合使用

Config Client通过Eureka注册中心,发现并获取了应用名为“MICROSERVER-CONFIG-SERVER-EUREKA”的服务的真实ip,并从中获取了相应的配置信息。

我们通过Config Client来访问"/profileType"服务,看看能否成功:

【Spring Cloud总结】38.Spring Cloud Config 与Eureka配合使用

可看到,通过我们配置文件中配置的“profile: dev”,从远程仓库获取到了microserver-config-client-dev.yml”配置文件的内容。

以上就是Config Server与Config Client与Eureka配合使用的所有操作。

注:还记得上一章我们为Config Server和Config Client添加了安全认证的配置,有两种,一种是将username和password直接配置在spring.cloud.config.uri上,一种是添加spring.cloud.config.username和spring.cloud.config.password属性,我们现在就明白了,如果集成了Eureka时,没有spring.cloud.config.uri时,我们就可以使用第二种方式配置了。

参考:《51CTO学院Spring Cloud高级视频》

转载请注明出处:https://blog.csdn.net/acmman/article/details/106032406

继续阅读