简单地说, 微服务是系统架构上的一种设计风格, 它的主旨是将一个本来独立的系统拆分红多个小型服务,这些小型服务都在各自独立的进程中运行,服务之间基于 RPC 进行通讯协做。 被拆分红的每个小型服务都围绕着系统中的某一项或一些耦合度较高的业务功能进行构建, 而且每一个服务都维护着自身的数据存储(划重点,每一个微服务都有本身的数据库实例)、 业务开发、自动化测试案例以及独立部署机制。java
tips:分布式事务自己的实现难度就很是大,因此在微服务架构中,咱们更强调在各服务之间进行 “ 无事务 ” 的调用,而对于数据一致性,只要求数据在最后的处理状态是一致的便可;若在过程当中发现错误,经过补偿机制来进行处理,使得错误数据可以达到最终的一致性。git
Spring Cloud 是一个基于Spring Boot实现的微服务架构开发工具。它为微服务架构中涉及的配置管理、服务治理、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操做提供了一种简单的开发方式。github
Spring Cloud 的出现,能够说是对微服务架构的巨大支持和强有力的技术后盾。它是一个解决微服务架构实施的综合性解决框架,它整合了诸多被普遍实践和证实过的框架做为实施的基础部件,又在该体系基础上建立了一些很是优秀的边缘组件。举个 Dubbo 和 Spring Cloud 差别性的例子:在使用 Dubbo 开发过程当中,分布式配置中心(百度的 Disconf、Netflix的Archaius、360的QConf、淘宝的 Diamond 等)、连接跟踪(京东的 Hydra、Twitter的 Zipkin 等)...一系列须要的组件,我都要去找第三方进行集成,还要考虑版本兼容的问题。而 Spring Cloud 就是一个微服务解决方案的“全家桶”,几乎我须要的所有微服务组件,我都能在其中找到“原装组件”:分布式配置中心(Config)、连接跟踪(Sleuth)、批量任务(Task),并且能够完美兼容。spring
服务治理体系能够说是微服务架构中最为核心和基础的模块, 它主要用来实现各个微服务实例的自动化注册与发现。服务治理体系中的三个核心角色: 服务注册中心、 服务提供者以及服务消费者。而 Eureka Server 就承担了 Spring Cloud 的服务注册中心。接下来捋一捋 Eureka Server 进行服务治理的过程:数据库
SpringBoot 版本号:2.1.6.RELEASE
SpringCloud 版本号:Greenwich.RELEASE缓存
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <version>2.1.0.RELEASE</version> </dependency>
server: port: 1111 eureka: instance: hostname: localhost prefer-ip-address: true client: # 表示不向注册中心注册本身 register-with-eureka: false # 注册中心的职责是维护实例,不须要去检索服务 fetch-registry: false service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ server: # 是否要打开自我保护机制 enable-self-preservation: true
eureka 的配置项主要有三项:instance、client、server。“instance”维护该服务的实例信息,包括 hostname、port 这类描述实例特征的元数据信息;“client”主要是服务注册数据的配置,好比超时时间、服务缓存时间等;“server”是服务注册中心特有的配置,配置 Eureka Server 的相关配置项,好比上面的是否打开自我保护。网络
//启动一个服务注册中心 @EnableEurekaServer @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
至此,咱们一个Eureka Server — 服务注册中心就搭建好了。架构
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
server: port: 2222 spring: application: name: cloud-eureka-client eureka: # 服务注册相关的配置信息 client: service-url: defaultZone: http://localhost:1111/eureka/ instance: # 是否优先使用IP地址做为主机名的标识 prefer-ip-address: true
就这样,咱们的一个 Eureka Client 算是注册到 Eureka Server 上了。接下来,让咱们试试用 DiscoveryClient 发现咱们的服务信息:app
// 自动化配置, 建立 DiscoveryClient 接口针对 Eureka 客户端的 EurekaDiscoveryClient 实例 @EnableDiscoveryClient @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application .class, args); } }
@RestController public class HelloController { private final Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private DiscoveryClient discoveryClient; @Value("${spring.application.name}") private String serviceId; @RequestMapping(value = "/hello", method = RequestMethod.GET) public String index() { List<ServiceInstance> instances = discoveryClient.getInstances(serviceId); ServiceInstance instance = instances.get(0); logger.info("/hello, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId()); return "Hello World"; } }
有了服务注册中心和服务提供者,咱们试试用 RestTemplate 调用一次服务吧!负载均衡
@RestController public class ConsumerController { @Autowired private RestTemplate restTemplate; @RequestMapping("/ribbon-consumer") public String helloConsumer() { // 这里访问的是服务名,而不是一个具体的地址(为了实现负载均衡策略),在服务治理框架中,这是一个很是重要的特性。 ResponseEntity<String> result = restTemplate.getForEntity("http://cloud-eureka-client/hello", String.class); return result.getBody(); } }