[toc]java
前言
前面说到,要用微服务, 就要解决微服务的几个核心问题 具体可看第一篇文章。这篇文章是学习使用Spring Cloud Netflix 的 Eureka 组件。经过这个组件,咱们能够建立一个服务的注册与发现中心服务来管理咱们服务。这是解决以后的其余问题的基础。只有全部的服务都注册到注册中心中, 才有利于作后续的服务间调用和服务监控等。git
建立统一的依赖管理项目
为何要建立依赖管理项目?
在建立服务注册中心的项目前, 咱们先建立一个依赖管理项目。为何要有依赖管理项目?github
spring-boot-starter-parent
这个依赖能够看到它的父pom就是一个依赖管理项目(spring-boot-dependencies
)建立项目
建立一个目录名为hello-spring-cloud-dependencies
, 新建pom文件,内容以下:web
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<groupId>com.domain</groupId>
<artifactId>hello-spring-cloud-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>hello-spring-cloud-dependencies</name>
<url>http://www.baidu.com</url>
<inceptionYear>2018-Now</inceptionYear>
<properties>
<!-- Environment Settings -->
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Spring Settings -->
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
<spring-boot-admin.version>2.0.1</spring-boot-admin.version>
<zipkin.version>2.10.1</zipkin.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Spring Cloud Begin 微服务框架-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Spring Cloud End -->
<!-- Spring Boot Admin Begin 用来管理和监控spring boot 应用程序-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
<!-- Spring Boot Admin End -->
<!-- ZipKin Begin 链路追踪系统-->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin</artifactId>
<version>${zipkin.version}</version>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
<version>${zipkin.version}</version>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
<version>${zipkin.version}</version>
</dependency>
<!-- ZipKin End -->
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!-- 资源文件配置 -->
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
复制代码
大体说下pom文件内容意思, parent 继承spring-boot-starter-parent
表示这是一个spring boot
项目,而后声明了,项目的组织,项目名版本等。而后在dependencyManagement
节点声明了 一些依赖, 这些依赖因为声明在dependencyManagement
节点中, 因此并真实依赖进本项目或者是子项目,只是起了一个声明做用。 若是须要真实依赖使用, 须要在使用地方再次声明一次,此次声明不带版本。能够看出, 咱们这个依赖管理项目, 只是把依赖抽取到这个地方统一管理而已。若是实在看不懂pom文件内容的,建议去补maven知识。spring
建立服务注册中心项目
依赖管理项目建立好了, 如今要作的建立一个服务注册中心项目继承它。pom内容以下:apache
<?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>
<parent>
<groupId>com.domain</groupId>
<artifactId>hello-spring-cloud-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath>
</parent>
<artifactId>hello-spring-cloud-eureka</artifactId>
<packaging>jar</packaging>
<name>hello-spring-cloud-eureka</name>
<url>http://www.baidu.com</url>
<inceptionYear>2018-Now</inceptionYear>
<dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.domain.hello.spring.cloud.eureka.EurekaApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
复制代码
**内容说明:**这里就两个依赖, 一个是spring boot测试包依赖, 一个是eureka
依赖, 这个就是Spring cloud 提供的服务注册与发现组件, 当咱们微服务中的各个服务启动后, 会自动注册到这个注册中心, 方便咱们对各个服务管理、使用。bash
eureka配置
配置文件内容以下:服务器
spring:
application:
name: hello-spring-cloud-eureka
server:
port: 8881
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
复制代码
内容说明: 前面是配置咱们服务的名称,端口。eureka 这个节点下面是eureka的相关配置,hostname是咱们服务器地址, client的两个属性是配置是否客户端, 这里写false,表示这是eureka的服务端。其余服务要做为客户端来注册到咱们这个服务端。defaultZone 是eureka的地址。客户端就是经过这个地址来注册到咱们 eureka的服务端。app
服务查看页面
当服务注册到eureka后, 咱们能够经过一个web 页面来查看。启动服务后,访问服务地址。 框架
页面中红色框选的地方会显示当前全部注册到eureka的服务。不过当前尚未服务注册上去。等下一篇文章节说服务提供者的, 咱们把服务注册上去,就能看见了。
完整代码
完整代码在这里: github.com/domain9065/…
全部文章目录及说明
有关文章的说明和目录都在这里了:juejin.im/post/5dbadf…