让我们用Kotlin开始一个Spring Cloud项目。
云原生是一种应用程序开发风格,旨在利用云计算框架,云框架由松散耦合的云服务组成。这意味着我们必须将任务分解为可以在不同位置的多个服务器上运行的单独服务。必须考虑冗余计划云原生应用程序,以便应用程序能够承受设备故障,并能够在硬件发生故障时自动重新映射IP地址。
在本教程中,我们将演示Spring Cloud Netflix和Kotlin的示例。Spring Cloud非常适合用Java构建微服务,现在我们将向Kotlin展示这个概念。Kotlin是一种用于构建后端系统的非常强大的语言,并且为产品目的采用它的组织数量正在增加。
Spring Cloud Netflix是一个针对云原生微服务的Spring项目,它通过自动配置和绑定到Spring环境为Spring Boot应用程序提供Netflix OSS集成。它提供了类似的模式:
其中大部分将在下面进行分析。
Config Server是一个集中且直接的解决方案,用于配置和检索所有微服务的配置。它提供了一种集中式方法来规划跨多个分布式服务和组件的所有配置。通常,配置文件(例如.yml文件)存在于Git存储库中,配置服务器检索所有服务的配置。
让我们从配置服务器创建开始。出于简单的原因,我们不会有用于配置的Git存储库; 我们将配置保存在服务器.yml文件中。您需要开始的是使用Kotlin和Maven创建一个Spring Boot项目(Gradle也可以),这些依赖项:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jre8</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
在ConfigServerApplication类中添加@EnableConfigServer。
@SpringBootApplication
@EnableConfigServer
class ConfigServerApplication
fun main(args: Array<String>) {
runApplication<ConfigServerApplication>(*args)
}
我们可以通过编辑application.yml将配置服务器配置到资源中的文件夹。正如我们所说,我们将仅使用本机文件系统来激活本机配置文件,因此我们在类路径中使用/ config位置。
Server:
port: 8888
spring:
profiles:
active: native
cloud:
config:
server:
native:
search-locations: classpath:config/
最后,我们运行Spring Boot应用程序,然后在端口8888上启动并运行配置服务器。下一步是设置发现服务器。
Eureka是Netflix OSS堆栈的发现服务。它是一种基于REST的服务,主要用于定位服务。Eureka附带Eureka客户端,可与服务进行交互。客户端具有嵌入式负载均衡器,可执行基本的循环负载平衡。Eureka在AWS中运行良好的原因是,在云环境中,存在连续的服务器传输,因此与使用具有已知IP地址和主机名的服务器的传统负载平衡器不同,需要检测没有此信息的服务器。Eureka会根据服务名称检测和负载均衡服务。还有负载均衡器可根据流量,资源使用等多种因素提供加权负载均衡。
每个地区都有一些地区和一个Eureka群集。群集中的Eureka服务器仅了解其区域中的实例。服务在Eureka注册,然后发送心跳以续订租约。如果客户端服务没有续订租约几次,则将其从服务器注册表中取出。来自任何区域的客户端都可以查找注册表信息以查找其服务并进行远程调用。要设置Eureka,我们需要创建另一个Spring Boot项目,就像我们为具有这些依赖项的配置服务器所做的那样:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jre8</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
我们在DiscoverServerApplication类中添加了@EnableEurekaServer。
@SpringBootApplication
@EnableEurekaServer
class DiscoveryServerApplication
fun main(args: Array<String>) {
runApplication<DiscoveryServerApplication>(*args)
}
在我们的application.yml文件中,我们可以声明服务器端口和应用程序名称。
server:
port: 8761
spring:
application:
name: "discovery-server"
我们运行应用程序,我们在端口8761上启动并运行了Eureka实例。
下一步是开始创建我们的微服务Spring Boot应用程序,它将从配置服务器检索配置并将其注册到Eureka。使用Eureka,微服务将能够相互查看并共享数据。Spring Cloud中的微服务之间存在许多数据共享概念的实现。有Feign Clients的REST选项,使用RabbitMQ或Apache Kafka的消息代理的异步方法,以及许多其他基于业务逻辑需求的方法。我们将使用Feign Client演示一个示例。
让我们创建另一个微服务,一个将从其他微服务收集数据的服务。我们将其命名为“数据 - 服务”。首先,像上面的例子一样创建一个Kotlin Spring Boot应用程序。如您所见,我们对Eureka客户端具有依赖性,因此该服务将注册到Eureka Server。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jre8</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
我们还将@EnableEurekaClient添加到Application类。
@SpringBootApplication
@EnableEurekaClient
class DataServiceApplication
fun main(args: Array<String>) {
runApplication<DataServiceApplication>(*args)
}
在application.yml的resources文件夹中,我们具有以下属性:
spring:
application:
name: "data"
eureka:
client:
healthcheck:
enabled: true
server:
port: 8080
我们可以选择一个名称,例如“data”来公开集群中的服务。Eureka将使用其声明的名称检测此服务。我们还声明服务器端口8080并启用向Eureka发送心跳的功能。
如果我们运行应用程序,我们会注意到它与Config Server连接。
Fetching config from server at: http://localhost:8888
Tomcat started on port(s): 8080 (http) with context path ''
EurekaAutoServiceRegistration : Updating port to 8080
DataServiceApplicationKt : Started DataServiceApplicationKt in 6.113 seconds (JVM running for 6.473)
Located environment: name=data, profiles=[default], label=null, version=null, state=null
Resolving eureka endpoints via configuration
上图是我们运行应用程序时来自日志文件的隔离消息,它显示了从配置服务器和集群中的应用程序配置文件获取配置。在此示例中,我们没有从Config Server到数据服务的特定配置,但您可以看到已建立连接。
如果我们回到配置服务器,我们可以通过将其添加到pom.xml依赖项来使其成为Eureka客户端:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
将客户端名称设置为application.yml,以便更新的配置服务器application.yml文件将是:
server:
port: 8888
spring:
application:
name: "config"
eureka:
client:
healthcheck:
enabled: true
profiles:
active: native
cloud:
config:
server:
native:
search-locations: classpath:config/
因此,我们创建了一个连接微服务系统来发现服务器。如果我们转到Eureka Server的网址http:// localhost:8761 /
,我们可以看到它可视化。
我们创建了一个小型生态系统,可以检测所有服务。在第2部分中,我们将添加另一项服务,编写一些Kotlin代码,并使用Feign Client在微服务之间共享数据。
在那之前,快乐的编码吧!
原文标题《Kotlin Microservices With Spring Cloud Netflix: Part 1》
译者:February
不代表云加社区观点,更多详情请查看原文链接
本文系外文翻译,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系外文翻译,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。