您的位置:首页 > 编程语言 > Java开发

SpringCloud Finchley 实战入门(基于springBoot 2.0.3)【二 服务治理 Eureka】

2018-07-24 17:21 1131 查看

开发工具及环境说明

  • Idea 2018.1
    建议使用idea作为开发工具,因为idea在我们创建项目的时候可以通过选择组件的方式帮助在pom.xml自动引用对应的jar位置。
  • jdk 1.8
    springBoot 2.x就默认需要使用jdk1.8的,这个没有什么好说
  • maven 3.3.9
  • windows 10

服务治理 springCloud Eureka

springCloud Eureka是spring Cloud Netflix微服务套件的一部分。它基于Netflix Eureka做了二次封装的。该组件的主要任务是负责微服务架构中的服务治理功能。
服务治理可以说是微服务架构中最为核心和基础的模块,主要是用来实现各个微服务实例的自动注册与发现。

快速入门

首先我们先创建一个基础的项目,最为接下来项目组件项目的父类项目。
1. 创建parent项目

选择spring Initializr

填写项目名称

创建一个空的项目

创建完成把项目下除了pom.xml文件全部删除

pom文件添加以下的配置

<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>

<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>

<!--使用aliyun镜像-->
<repositories>
<repository>
<id>alimaven</id>
<name>Maven Aliyun Mirror</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

搭建服务注册中心

在父类项目下新建一个module。

把module命名为”eureka-register-center”

选择Eureka-server组件

这个时候pom.xml的内容应该是这样子的

<?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.example</groupId>
<artifactId>eureka-register-center</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>eureka-register-center</name>
<description>Demo project for Spring Boot</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>

配置application.yml

将resources目录下的application.properties改名为application.yml。springBoot的配置文件格式有yaml和properties两种格式。本人喜欢使用yaml格式。所以项目中的配置文件都是以yml来使用。
配置文件内容如下:相关的字段也有解释

server:
port: 8761
spring:
application:
name: service-register
eureka:
instance:
prefer-ip-address: false
hostname: localhost
client:
fetch-registry: false
register-with-eureka: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
wait-time-in-ms-when-sync-empty: 0
# 测试时关闭自我保护机制,保证不可用服务及时踢出 默认打开状态,建议生产环境打开此配置
enable-self-preservation: true #(设为false,关闭自我保护主要)
eviction-interval-timer-in-ms: 4000 #清理间隔(单位毫秒,默认是60*1000)

注意:
fetch-registry 由于注册中心的任务是维护实例服务,他并不需要去检索服务,所以也设置为false
register-with-eureka 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己

项目主类名添加@EnableEurekaServer注解

通过上面的配置就把eureka 服务注册中心搭建起来了。
启动应用并访问地址 http://localhost:8761/ 。可以看到如下图显示的eureka信息面板。Instances currently registered with Eureka这里是空的。说明该注册中心还没有注册任何服务。

到这里说明我们的eureka服务注册中心是正常的。

注册服务提供者

按照创建服务注册中心的方式创建一个eureka-client 项目。
pom文件和上面的一样。只是application.yml不同

spring:
application:
name: service-eureka-client
server:
port: 8800

然后在项目主类名添加@EnableDiscoveryClient注解。表示该项目作为一个服务实例注册到服务注册中心。

到这里其实服务提供者的一个服务实例就基本搭建好了,我们启动服务如果没有报错那应该就没有问题。
确保服务正常启动后,我们重新再访问 http://localhost:8761/

在服务注册中心的项目面板,我们可以看到了eureka-cient服务实例已经注册到了服务注册中心。

PS: 可能有人会疑问~为什么我们的服务提供者eureka-client项目中的配置文件只配置了端口和服务名称外,就没有其他额外的配置了。为什么这个服务实例会自动注册到服务注册中心的?
我们可以查看源码:EurekaClientConfigBean这个类
在这个类我们可以看到,注册中心的地址是默认为http://localhost:8761的,这也是我们为什么会在之前的注册中心配置port为8761.

@(bolg)ConfigurationProperties(EurekaClientConfigBean.PREFIX)
public class EurekaClientConfigBean implements EurekaClientConfig {

public static final String PREFIX = "eureka.client";

public static final String DEFAULT_URL = "http://localhost:8761" + DEFAULT_PREFIX
+ "/";

public static final String DEFAULT_ZONE = "defaultZone";

private static final int MINUTES = 60;

我们其实也可以在application.yml显示的指明服务注册中心的地址。

spring:
application:
name: service-eureka-client
server:
port: 8800
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka

到这里一个基础的服务注册中心和服务提供者项目实例就已经搭建好了。

项目的源码

在微服务架构中,我们需要充分的考虑发生故障的情况,所以在生产环境中必须对各个组件进行高可用部署。上面演示的只是单个服务,下一篇我们会讲一下服务注册中心怎样高可用部署。

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: