Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操做提供了一种简单的开发方式。html
Spring Cloud包含了多个子项目(针对分布式系统中涉及的多个不一样开源产品),好比:Spring Cloud Config、Spring Cloud Netflix、Spring Cloud0 CloudFoundry、Spring Cloud AWS、Spring Cloud Security、Spring Cloud Commons、Spring Cloud Zookeeper、Spring Cloud CLI等项目。mysql
微服务(Microservices Architecture)是一种架构风格,一个大型复杂软件应用由一个或多个微服务组成。系统中的各个微服务可被独立部署,各个微服务之间是松耦合的。每一个微服务仅关注于完成一件任务并很好地完成该任务。在全部状况下,每一个任务表明着一个小的业务能力。git
微服务的概念源于2014年3月Martin Fowler所写的章“Microservices”http://martinfowler.com/artic...github
微服务架构的核心思想是,一个应用是由多个小的、相互独立的、微服务组成,这些服务运行在本身的进程中,开发和发布都没有依赖。不一样服务经过一些轻量级交互机制来通讯,例如 RPC、HTTP 等,服务可独立扩展伸缩,每一个服务定义了明确的边界,不一样的服务甚至能够采用不一样的编程语言来实现,由独立的团队来维护。简单的来讲,一个系统的不一样模块转变成不一样的服务!并且服务能够使用不一样的技术加以实现!spring
那咱们在微服务中应该怎样设计呢。如下是微服务的设计指南:sql
因为Spring Cloud为服务治理作了一层抽象接口,因此在Spring Cloud应用中能够支持多种不一样的服务治理框架,好比:Netflix Eureka、Consul、Zookeeper。在Spring Cloud服务治理抽象层的做用下,咱们能够无缝地切换服务治理实现,而且不影响任何其余的服务注册、服务发现、服务调用等逻辑。数据库
Spring Cloud Eureka来实现服务治理。编程
Spring Cloud Eureka是Spring Cloud Netflix项目下的服务治理模块。而Spring Cloud Netflix项目是Spring Cloud的子项目之一,主要内容是对Netflix公司一系列开源产品的包装,它为Spring Boot应用提供了自配置的Netflix OSS整合。经过一些简单的注解,开发者就能够快速的在应用中配置一下经常使用模块并构建庞大的分布式系统。它主要提供的模块包括:服务发现(Eureka),断路器(Hystrix),智能路由(Zuul),客户端负载均衡(Ribbon)等。springboot
提供服务注册和发现服务器
在项目 lemon-eureka
中引入须要的依赖内容:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency>
经过 @EnableEurekaServer
注解启动一个服务注册中心提供给其余应用进行对话,这个注解须要在springboot工程的启动application类上加@EnableEurekaServer
@SpringBootApplication @EnableEurekaServer public class LemonEurekaApplication { public static void main(String[] args) { SpringApplication.run(LemonEurekaApplication.class, args); } }
在默认设置下,该服务注册中心也会将本身做为客户端来尝试注册它本身,因此咱们须要禁用它的客户端注册行为,只须要在application.yml
配置文件中增长以下信息:
registerWithEureka: false fetch-registry: false
完整配置
server: port: 7001 spring: application: name: lemon-eureka eureka: instance: hostname: eureka7001.com client: registerWithEureka: false fetch-registry: false service-url: defaultZone: http://eureka7001.com:7001/eureka/
启动工程后,访问:http://eureka7001.com:7001
能够看到下面的页面,其中尚未发现任何服务。
在项目 lemon-dept
中引入须要的依赖内容:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
在应用主类中经过加上 @EnableEurekaClient,但只有Eureka 可用,你也能够使用@EnableDiscoveryClient。须要配置才能找到Eureka注册中心服务器
@SpringBootApplication @EnableDiscoveryClient public class LemonDeptApplication { public static void main(String[] args) { SpringApplication.run(LemonDeptApplication.class, args); } }
须要配置才能找到Eureka服务器。例:
完整配置
server: port: 8001 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径 type-aliases-package: com.github.lemon.entities # 全部Entity别名类所在包 mapper-locations: - classpath:mybatis/mapper/**/*.xml # mapper映射文件 spring: application: name: lemon-dept datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操做类型 driver-class-name: com.mysql.jdbc.Driver # mysql驱动包 url: jdbc:mysql://localhost:3306/cloudDB01 # 数据库名称 username: root password: 123456 dbcp2: min-idle: 5 # 数据库链接池的最小维持链接数 initial-size: 5 # 初始化链接数 max-total: 5 # 最大链接数 max-wait-millis: 200 # 等待链接获取的最大超时时间 eureka: client: #客户端注册进eureka服务列表内 service-url: #defaultZone: http://eureka7001.com:7001/eureka defaultZone: http://eureka7001.com:7001/eureka/ instance: instance-id: lemon-dept prefer-ip-address: true #访问路径能够显示IP地址
其中defaultZone
是一个魔术字符串后备值,为任何不表示首选项的客户端提供服务URL(即它是有用的默认值)。
经过spring.application.name
属性,咱们能够指定微服务的名称后续在调用的时候只须要使用该名称就能够进行服务的访问
启动该工程后,再次访问启动工程后:http://eureka7001.com:7001
能够以下图内容,咱们定义的服务被成功注册了。