Spring Cloud简介
Spring Cloud是基于Spring Boot的一套实现微服务架构的生态组件。生态组件中包含Spring Cloud NetFlix,Spring Cloud Feign,Spring Cloud Config,Spring Cloud CloudFoundry,Spring Cloud Bus,Spring Cloud Security,Spring Cloud Stream等生态组件,用于解决微服务架构中的一系列问题。Dubbo等组件也能实现微服务构建,但对比与Spring Cloud来说,Spring Cloud的生态组件较完善,不必花像Dubbo等构建要去花精力选择其余框架。Spring Cloud文档较为完善,且组件一直在优化。所以,当前对于构建微服务架构来说,Spring Cloud是较好的选择。
服务的注册中心
Spring Cloud Netflix提供Euraka Server来实现注册中心。注册中心在整个微服务架构中是最核心的模块,用于提供注册中心给微服务实例实现自动化注册与发现。为实现注册中心的高可用,一般会创建多个注册中心,并相互注册(一般创建2个),避免单点故障导致服务不可用。
以下介绍如何搭建高可用的服务注册中心,并实现实例下线时注册中心对实例列表的动态删除。
说明:搭建基于Spring Cloud Netflix 2.0.2版本,开发工具为Idea。
1、创建Euraka Server,配置pom.xml依赖(可以使用Spring Initializr来创建)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.springcloud</groupId> <artifactId>server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SCServer</name> <description>Demo project for Spring Cloud Server</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
注意:在2.0版本中,依赖jar为 spring-cloud-starter-netflix-eureka-server,而在1.0版本中为 spring-cloud-starter-eureka-server。
2、配置application.yml
我们这里统一采用yml来作为配置文件,相对于properties来说,yml能支持自动补全提示,且格式更为清晰(使用yml是注意缩进)。
spring:
application:
name: eureka-server0
server:
port: 11110
eureka:
instance:
hostname: peer0
client:
register-with-eureka: false
service-url:
defaultZone: http://peer1:11111/eureka
fetch-registry: false
server:
enable-self-preservation: false
主要几个配置的说明:
- eureka.instance.hostname:注册中心的注册名。
- euraka.client.register-with-euraka:配置为false,表示不将自己注册到注册中心。
- euraka.client.serviceUrl.defaultZone:配置defaultZone中的注册地址,该项用于配置注册中心高可用时,需要向另一台注册中心注册自己。(此配置为key-value类型,defaultZone为默认Zone的key)
- euraka.client.fetch-registry:配置为false,表明该注册中心只负责管理实例,不负责去检索实例服务。
- euraka.server.enable-self-preservation:配置为false,用处是为了注册中心能及时删除下线服务,而不是保留,配置禁止其保护模式。(根据项目实际需求配置)
3、在入口程序中使用@EnableEurakaServer声明为euraka server
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class ScServerApplication { public static void main(String[] args) { SpringApplication.run(ScServerApplication.class, args); } }
说明:也可使用@EnableDiscoveryClient来注释,@EnableEurakaServer是使用Euraka作为注册中心时使用的注释,使用其它类型的注册中心,可使用@EnableDiscoveryClient。
4、同理配置另一个注册中心,application.yml如下(需要在hosts文件中增加127.0.0.1 peer0 127.0.0.1 peer1)。
spring:
application:
name: eureka-server1
server:
port: 11111
eureka:
instance:
hostname: peer1
client:
register-with-eureka: false
service-url:
defaultZone: http://peer0:11110/eureka
fetch-registry: false
server:
enable-self-preservation: false
5、启动程序,访问http://localhost:11110与http://localhost:11111可查看到两注册中心启动成功,且相互注册。至此,高可用的Eureka注册中心搭建完成。