Author:赵志乾
Date:2019-02-17
Declaration:All Right Reserved!!!
1、注册中心
微服务架构下,服务变得比较分散,如下图。此时需要一套治理工具进行服务的管理、维护。而注册中心,便是最常用的服务治理解决方案。
注册中心在微服务架构中,处在各服务中间,作为各服务信息和状态的统一管理者。服务于服务之间的交互需要通过注册中心来确定其交互的实体,即服务消费者只关注服务的逻辑提供者,在具体消费时,借助注册中心转化物理提供者。如下图所示:
2、Spring Cloud Eureka
Eureka原本属于Netflix,是注册中心的一种具体实现,经过二次封装后成为了Spring Cloud Eureka。此时其作为基础组件,正式被纳入Spring Cloud体系。
————————————————————————————————————————————————————————
注册中心搭建
1、新建一个maven项目,命名为EurekaServer;
2、pom文件中配置依赖信息,如下:
<?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.zhaozhiqian</groupId>
<artifactId>eureka-server</artifactId>
<version>1.0-SNAPSHOT</version>
<!--指定父项目,目的是继承依赖项-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<!--jaxb模块引用- start -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<!-- jaxb模块引用- end -->
<!-- eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!--指定Spring Cloud为父项目,从而实现多继承-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
注:pom文件中各标签含义见《POM文件之常用标签》
3、创建启动类EurekaServerApplication.java
package com.zhaozhiqian.start;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
//@SpringBootApplication注解表示该类为启动类
@SpringBootApplication
//@EnableEurekaServer注解表示开启EurekaServer
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args){
SpringApplication.run(EurekaServerApplication.class,args);
}
}
4、在目录src/main/resources下创建一个application.properties属性文件,内容如下:
spring.application.name=zhaozhiqian-eureka
server.port=8888
eureka.instance.hostname=localhost
#由于该应用为注册中心,所以设置为 false,代表不向汪册中心注册自己
eureka.client.register-with-eureka=false
#由于注册中心的职责就是维护服务实例,他并不需要去检索服务,所以也设置为 false
eureka.client.fetch-registry=false
5、运行EurekaServerApplication,输出日志如下则表明启动成功。
2019-02-17 11:04:42.798 INFO 5192 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8888 (http)
2019-02-17 11:04:42.798 INFO 5192 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8888
2019-02-17 11:04:42.802 INFO 5192 --- [ main] c.z.start.EurekaServerApplication : Started EurekaServerApplication in 4.402 seconds (JVM running for 4.881)
6、访问网址:http://localhost:8888/,即可看到Eureka提供的WEB控制台。
参考资料:《SpringCloud微服务全栈技术与案例解析》