简介
为了更好地支撑日益增长的庞大业务量,我们常常需要把服务进行整合、拆分,使我们的服务不仅能通过集群部署抵挡流量的冲击,又能根据业务在其上进行灵活的扩展。随着分布式的普及、服务的快速增长与云计算技术的进步,微服务架构也因其特有的优势而备受关注。微服务架构的本质,是把整体的业务拆分成很多有特定明确功能的服务,通过很多分散的小服务之间的配合,去解决更大,更复杂的问题。对被拆分后的服务进行分类和管理,彼此之间使用统一的接口来进行交互。
本系列讲述了在阿里云Kubernetes容器服务基础之上,如何快速搭建基于Spring Cloud的微服务架构中的基础设施:
- 第一篇:分布式服务注册与发现系统
- 第二篇:分布式配置管理系统
- 第三篇:API网关服务Zuul 系统
- 第四篇:分布式追踪系统
- 第五篇:分布式弹性服务与容错处理框架Hystrix及其监控仪表板
- 第六篇:熔断器聚合监控Hystrix Turbine
本文是系列中的第一篇,着重介绍分布式服务的注册与发现。
在Spring Cloud里,负责微服务注册与发现的项目是Spring Cloud Netflix项目中的Eureka组件。Eureka分为两大部分,Eureka Server与Eureka Client。相应地,Eureka Server负责管理、协调所有的微服务提供者,即Eureka Client。
下面讲述一下在阿里云Kubernetes容器服务基础之上,如何快速搭建一套分布式服务注册与发现系统。
准备Kubernetes环境
阿里云容器服务Kubernetes 1.9.3目前已经上线,可以通过容器服务管理控制台非常方便地快速创建 Kubernetes 集群。
体验通过应用目录简便部署
点击左侧的应用目录
,在右侧选中ack-springcloud-eureka
,如下:
点击参数
, 可以通过修改参数配置进行定制化。例如,可以通过运行2个实例,并进行互相注册的方式来实现高可用的部署。
replicaCount: 2
image:
repository: registry.cn-hangzhou.aliyuncs.com/aliacs-app-catalog/eureka
tag: 1.5.13.RELEASE
pullPolicy: Always
service:
enabled: true
type: LoadBalancer
externalPort: 8761
internalPort: 8761
management:
endpointsEnabled: true
修改之后,在右侧选择对应的集群、命名空间,指定发布名称,然后点击部署。
几分钟之后,一个高可用的Eureka Server实例就可以创建出来。
体验Eureka Server
点击左侧的服务
,在右侧点击刚创建的Eureka Server服务提供的访问地址,如下所示:
在打开的Eureka Server界面中,可以看到如下类似的内容,则证明Eureka Server已正常启动,并且通过运行2个实例,进行互相注册的方式来实现高可用的部署。
开发微服务并注册到Eureka Server
为了演示微服务如何注册到Eureka Server, 我们提供了1个基于Spring Boot开发的微服务示例,具体代码请参阅: https://github.com/AliyunContainerService/spring-cloud-k8s-sample/tree/master/sample-service-eureka-client
import java.net.MalformedURLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
@RestController
@EnableHystrix
public class SampleServiceApplication {
@Autowired
private DiscoveryClient discoveryClient;
public static void main(String[] args) {
SpringApplication.run(SampleServiceApplication.class, args);
System.out.println("Running " + SampleServiceApplication.class + " via Spring Boot!");
}
@RequestMapping("/")
public String home(@RequestParam(value = "service", required = false) String serviceName)
throws MalformedURLException {
List<ServiceInstance> list = discoveryClient.getInstances(serviceName);
if (list != null && list.size() > 0) {
String serviceURL = list.get(0).getUri().toURL().toString();
serviceURL += "/hello?service=" + serviceURL;
RestTemplate restTemplate = new RestTemplate();
return restTemplate.getForObject(serviceURL, String.class);
}
return "Hello! This is from Sample Service 1!";
}
@RequestMapping("/hello")
public String hello(@RequestParam(value = "service", required = false) String serviceName) {
return "Hello! This is from " + serviceName + "!";
}
}
注意,该Spring Boot的 application.yaml中需要确保如下配置:
eureka:
instance:
preferIpAddress: true
这样注册到Eureka Server上的是IP地址,而不是连不上的pod的hostname。
在打开的Eureka Server界面中,可以看到如下类似的内容,则证客户端也能正常注册服务。
总结
我们可以利用阿里云Kubernetes容器服务,快速搭建一套分布式服务注册与发现系统,为应用引
入和配置Eureka服务。欢迎大家使用阿里云上的容器服务,快速搭建一套分布式服务注册与发现系统Eureka,比较简单地集成到自己项目的微服务开发中。