zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Spring认证中国教育管理中心-Apache Geode 的 Spring 数据教程十

2023-04-18 16:08:25 时间

原标题:Spring认证中国教育管理中心-Apache Geode 的 Spring 数据教程十(Spring中国教育管理中心)

6.6.运行时配置使用Properties

除了Configurers,基于注解的配置模型中的每一个注解属性都关联了一个对应的配置属性(以 为前缀spring.data.gemfire.),可以在Spring Bootapplication.properties文件中声明 。

在前面的示例的基础上,客户端的application.properties文件将定义以下属性集:

客户 application.properties

spring.data.gemfire.cache.log-level=info
spring.data.gemfire.pool.Venus.servers=venus[48484]
spring.data.gemfire.pool.Venus.max-connections=200
spring.data.gemfire.pool.Venus.min-connections=50
spring.data.gemfire.pool.Venus.ping-interval=15000
spring.data.gemfire.pool.Venus.pr-single-hop-enabled=true
spring.data.gemfire.pool.Venus.read-timeout=20000
spring.data.gemfire.pool.Venus.subscription-enabled=true
spring.data.gemfire.pool.Saturn.locators=skullbox[20668]
spring.data.gemfire.pool.Saturn.subscription-enabled=true
spring.data.gemfire.pool.Neptune.servers=saturn[41414],neptune[42424]
spring.data.gemfire.pool.Neptune.min-connections=25

相应的服务器application.properties文件将定义以下属性:

服务器 application.properties

spring.data.gemfire.cache.log-level=info
spring.data.gemfire.cache.server.port=40404
spring.data.gemfire.cache.server.Venus.port=43434
spring.data.gemfire.cache.server.Saturn.port=41414
spring.data.gemfire.cache.server.Neptune.port=41414

然后您可以将@ClientCacheApplication类简化为以下内容:

春天@ClientCacheApplication类

@SpringBootApplication
@ClientCacheApplication
@EnablePools(pools = {
    @EnablePool(name = "Venus"),
    @EnablePool(name = "Saturn"),
    @EnablePool(name = "Neptune")
})
class ClientApplication { .. }

此外,@CacheServerApplication该类变为以下内容:

春天@CacheServerApplication类

@SpringBootApplication
@CacheServerApplication(name = "SpringServerApplication")
@EnableCacheServers(servers = {
    @EnableCacheServer(name = "Venus"),
    @EnableCacheServer(name = "Saturn"),
    @EnableCacheServer(name = "Neptune")
})
class ServerApplication { .. }

前面的示例说明了为什么“命名”基于注释的 bean 很重要(除了因为在某些情况下需要它)。这样做可以从 XML、属性和 Java 引用 Spring 容器中的 bean。甚至可以将注解定义的 bean 注入应用程序类,无论出于何种目的,如下例所示:

@Component
class MyApplicationComponent {

  @Resource(name = "Saturn")
  CacheServer saturnCacheServer;

  ...
}

同样,命名注解定义的 bean 允许您编写代码Configurer以自定义特定的“命名”bean,因为beanName传递给回调的 2 个参数中的 1 个。

通常,关联的注释属性属性采用两种形式:“命名”属性和“未命名”属性。

以下示例显示了这种安排:

spring.data.gemfire.cache.server.bind-address=10.105.20.1
spring.data.gemfire.cache.server.Venus.bind-address=10.105.20.2
spring.data.gemfire.cache.server.Saturn...
spring.data.gemfire.cache.server.Neptune...

虽然CacheServers上面有三个命名,但还有一个未命名的CacheServer属性为该属性的任何未指定值提供默认值,即使是 "named" CacheServers。因此,当“Venus”设置并覆盖它自己的 时bind-address,“Saturn”和“Neptune”继承自“unnamed” spring.data.gemfire.cache.server.bind-address属性。

请参阅注释的 Javadoc,了解注释属性支持基于属性的配置,以及它们是否支持“命名”属性而不是默认的“未命名”属性。

6.6.1.Properties的Properties

在通常的 Spring 方式中,您甚至可以Properties用 other 表示Properties,无论是通过以下示例显示在application.properties文件中设置的嵌套属性:

属性属性

spring.data.gemfire.cache.server.port=${gemfire.cache.server.port:40404}

以下示例显示了在 Java 中设置的嵌套属性:

属性占位符嵌套

@Bean
CacheServerConfigurer cacheServerPortConfigurer(
    @Value("${gemfire.cache.server.port:${some.other.property:40404}}")
    int cacheServerPort) {
  ...
}

属性占位符嵌套可以任意深。

6.7.配置嵌入式服务

Apache Geode 提供了启动应用程序所需的许多不同嵌入式服务的能力,具体取决于用例。

6.7.1.配置嵌入式定位器

如前所述,客户端使用 Apache Geode Locators 来连接和查找集群中的服务器。此外,加入现有集群的新成员使用定位器来寻找他们的同伴。

对于应用程序开发人员来说,通常很方便,因为他们正在为 Apache Geode 应用程序开发 Spring Boot 和 Spring Data,以启动一个由两个或三个 Apache Geode 服务器组成的小集群。您可以使用 注释您的 Spring Boot@CacheServerApplication类@EnableLocator,而不是启动单独的 Locator 进程,如下所示:

Spring,CacheServer运行嵌入式定位器的Apache Geode应用程序

@SpringBootApplication
@CacheServerApplication
@EnableLocator
class ServerApplication { .. }

该@EnableLocator注释在Spring的Apache的Geode开始嵌入定位器CacheServer上运行的应用程序localhost,侦听默认定位器端口上,10334。您可以使用相应的注释属性自定义host(绑定地址)和port嵌入的 Locator 绑定到的。

或者,您可以@EnableLocator通过在 中设置相应的 spring.data.gemfire.locator.host和属性来设置 spring.data.gemfire.locator.port属性application.properties。

然后,您可以@CacheServerApplication通过使用以下命令连接到此 Locator来启动其他启用Spring Boot 的应用程序:

Spring,Apache GeodeCacheServer应用程序连接到定位器

@SpringBootApplication
@CacheServerApplication(locators = "localhost[10334]")
class ServerApplication { .. }

您甚至可以将前面显示的两个应用程序类合并为一个类,并使用 IDE 创建不同的运行配置文件配置,以通过使用 Java 系统属性稍微修改配置来启动同一类的不同实例,如下所示:

CacheServer运行嵌入式定位器并连接到定位器的Spring应用程序

@SpringBootApplication
@CacheServerApplication(locators = "localhost[10334]")
public class ServerApplication {

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

  @EnableLocator
  @Profile("embedded-locator")
  static class Configuration { }

}

然后,对于每个运行配置文件,您可以设置和更改以下系统属性:

IDE 运行配置文件配置

spring.data.gemfire.name=SpringCacheServerOne
spring.data.gemfire.cache.server.port=41414
spring.profiles.active=embedded-locator

只有 1 个ServerApplication该类的运行配置文件应该设置-Dspring.profiles.active=embedded-locator Java 系统属性。然后,您可以更改..name并..cache.server.port为每个其他运行配置文件,并在本地系统上运行的Apache的Geode服务器的一小簇(分布式系统)。

该@EnableLocator批注仅用作开发时批注,而不是应用程序开发人员在生产中使用的批注。我们强烈建议在集群中将定位器作为独立的独立进程运行。

可以在此处找到有关 Apache Geode Locators 如何工作的更多详细信息 。

6.7.2.配置嵌入式管理器

Apache Geode Manager 是集群中负责集群“管理”的另一个对等成员或节点。管理包括创建Regions,Indexes,DiskStores,除其他事项外,与监控群集组件的运行时操作和行为一起。

Manager 允许启用 JMX 的客户端(例如Gfsh shell 工具)连接到 Manager 以管理集群。也可以使用 JDK 提供的工具(例如 JConsole 或 JVisualVM)连接到 Manager,因为它们也是支持 JMX 的客户端。

也许您还希望将@CacheServerApplication之前显示的 Spring 也启用为 Manager。要做到这一点,你的注释春季@Configuration或@SpringBootApplication类@EnableManager。

默认情况下,Manager 绑定到localhost,侦听 的默认 Manager 端口1099。可以使用注释属性或相应的属性来配置 Manager 的几个方面。

以下示例显示了如何在 Java 中创建嵌入式管理器:

CacheServer运行嵌入式管理器的Spring应用程序

@SpringBootApplication
@CacheServerApplication(locators = "localhost[10334]")
public class ServerApplication {

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

  @EnableLocator
  @EnableManager
  @Profile("embedded-locator-manager")
  static class Configuration { }

}

有了前面的类,你甚至可以使用Gfsh来连接小集群并进行管理,如下:

$ gfsh
    _________________________     __
   / _____/ ______/ ______/ /____/ /
  / /  __/ /___  /_____  / _____  /
 / /__/ / ____/  _____/ / /    / /
/______/_/      /______/_/    /_/    1.2.1

Monitor and Manage {data-store-name}

gfsh>connect
Connecting to Locator at [host=localhost, port=10334] ..
Connecting to Manager at [host=10.99.199.5, port=1099] ..
Successfully connected to: [host=10.99.199.5, port=1099]

gfsh>list members
         Name          | Id
---------------------- | ----------------------------------------------------
SpringCacheServerOne   | 10.99.199.5(SpringCacheServerOne:14842)<ec><v0>:1024
SpringCacheServerTwo   | 10.99.199.5(SpringCacheServerTwo:14844)<v1>:1025
SpringCacheServerThree | 10.99.199.5(SpringCacheServerThree:14846)<v2>:1026

因为我们还启用了嵌入式定位器,所以我们可以通过定位器间接连接到管理器。定位器允许 JMX 客户端连接并在集群中查找管理器。如果不存在,定位器将承担管理器的角色。但是,如果不存在 Locator,我们需要使用以下命令直接连接到 Manager:

Gfsh connect命令直接连接到 Manager

gfsh>connect --jmx-manager=localhost[1099]

与@EnableLocator注解一样,@EnableManager注解也意味着只在开发时使用注解,而不是应用程序开发人员在生产中使用的注解。我们强烈建议管理器,如定位器,是集群中独立、独立和专用的进程。

可以在此处找到有关 Apache Geode 管理和监控的更多详细信息 。

6.7.3.配置嵌入式 HTTP 服务器

Apache Geode 还能够运行嵌入式 HTTP 服务器。当前的实现由Eclipse Jetty支持 。

嵌入式 HTTP 服务器用于托管 Apache Geode 的 Management (Admin) REST API(不是公开广告的 API)、Developer REST API和Pulse Monitoring Web Application。

但是,要使用任何这些 Apache Geode 提供的 Web 应用程序,您必须在系统上安装完整的 Apache Geode,并且必须将GEODE_HOME环境变量设置为您的安装目录。

要启用嵌入式 HTTP 服务器,请将@EnableHttpService注释添加到任何@PeerCacheApplication 或带@CacheServerApplication注释的类,如下所示:

CacheServer运行嵌入式 HTTP 服务器的Spring应用程序

@SpringBootApplication
@CacheServerApplication
@EnableHttpService
public class ServerApplication { .. }

默认情况下,嵌入式 HTTP 服务器在端口上侦听7070HTTP 客户端请求。当然,您可以根据需要使用注解属性或相应的配置属性来调整端口。

点击前面的链接,了解有关 HTTP 支持和所提供服务的更多详细信息。

6.7.4.配置嵌入式 Memcached 服务器 (Gemcached)

Apache Geode 还实现了 Memcached 协议,能够为 Memcached 客户端提供服务。也就是说,Memcached 客户端可以连接到 Apache Geode 集群并执行 Memcached 操作,就好像集群中的 Apache Geode 服务器是实际的 Memcached 服务器一样。

要启用嵌入式 Memcached 服务,请在@EnableMemcachedServer任何@PeerCacheApplication 或带@CacheServerApplication注释的类中添加注释,如下所示:

CacheServer运行嵌入式 Memcached 服务器的Spring应用程序

@SpringBootApplication
@CacheServerApplication
@EnabledMemcachedServer
public class ServerApplication { .. }

可以在此处找到有关 Apache Geode 的 Memcached 服务(称为“Gemcached”)的更多详细信息 。

6.7.5.配置嵌入式Redis服务器

Apache Geode 还实现了 Redis 服务器协议,该协议使 Redis 客户端能够连接到 Apache Geode 服务器集群并与之通信以发出 Redis 命令。在撰写本文时,Apache Geode 中的 Redis 服务器协议支持仍处于试验阶段。

要启用嵌入式Redis服务,请在@EnableRedisServer任何@PeerCacheApplication 或带@CacheServerApplication注释的类中添加注释,如下所示:

CacheServer运行嵌入式 Redis 服务器的Spring应用程序

@SpringBootApplication
@CacheServerApplication
@EnableRedisServer
public class ServerApplication { .. }

您必须 org.apache.geode:geode-redis在 Spring [Boot] 应用程序类路径上显式声明该模块。