一、前言
spring cloud是基于springboot的,所以需要开发中对springboot有一定的了解,如果不了解的话可以看Spring Boot教程:
Spring Cloud版本 | Spring Boot版本 | JAVA | IDE |
Greenwich.SR1 | 2.1.6.RELEASE | 1.8 | IDEA |
涉及项目:
alh-eureka-server(6000)
alh-tms(8001)
完整教程目录:https://blog.csdn.net/tuoyun6647/article/details/93501029
二、Eureka介绍
Spring Cloud Eureka是Spring Cloud Netflix服务套件中的一部分,它基于Netflix Eureka做了二次封装,主要负责完成微服务架构中的服务治理功能。Eureka包含了服务器端和客户端组件。服务器端,也被称作是服务注册中心,用于提供服务的注册与发现。Eureka支持高可用的配置,当集群中有分片出现故障时,Eureka就会转入自动保护模式,它允许分片故障期间继续提供服务的发现和注册,当故障分片恢复正常时,集群中其他分片会把他们的状态再次同步回来。
客户端组件包含服务消费者与服务生产者。在应用程序运行时,Eureka客户端向注册中心注册自身提供的服务并周期性的发送心跳来更新它的服务租约。同时也可以从服务端查询当前注册的服务信息并把他们缓存到本地并周期性的刷新服务状态。
三、 创建服务注册中心
3.1创建Eureka Server
其主要引入了spring-cloud-starter-netflix-eureka-server的依赖,完整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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kcsm.alh</groupId>
<artifactId>alh-eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>alh-eureka-server</name>
<description>Eureka Server</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
3.2 配置文件
application.properties配置文件内容如下
#服务注册中心
server.port=6000
eureka.instance.hostname=localhost
#表示是否将自己注册在EurekaServer上,默认为true。由于当前应用就是EurekaServer,所以置为false
eureka.client.register-with-eureka=false
#表示表示是否从EurekaServer获取注册信息,默认为true。单节点不需要同步其他的EurekaServer节点的数据
eureka.client.fetch-registry=false
#注册中心地址,${eureka.instance.hostname}为配置文件中变量值获取方式
defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
#应用名称
spring.application.name=alh-eureka-server
3.3 启动程序
在springboot工程的启动application类上加@EnableEurekaServer注解:
package com.kcsm.alh.alheurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class AlhEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(AlhEurekaServerApplication.class, args);
}
}
启动项目,启动成功后输入http://localhost:6000,即可看到管理页面
四、创建Client
4.1创建项目
项目名称为alh-tms,其主要引入了spring-cloud-starter-netflix-eureka-client依赖,完整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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kcsm.alh</groupId>
<artifactId>alh-tms</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>alh-tms</name>
<description>Transportation Management System</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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>
4.2配置文件
#注册中心地址.默认将自己注册在EurekaServer上,eureka.client.register-with-eureka默认为true,无需配置
eureka.client.service-url.defaultZone=http://localhost:6000/eureka/
#应用端口及名称,注册到注册中心后,之后服务与服务之间相互调用一般都是根据spring.application.name调用
server.port=8001
spring.application.name=alh-tms
如果你目前只是正在搭建或是入门学习,请跳过下面这段话
重点说明:eureka的client注册到server时默认是使用hostname(主机名)而不是ip,这就导致client在多台机器时,服务间相互调用时也会使用hostname进行调用,从而调用失败。这时候就需要客户端使用ip把服务注册到eureka-server上,需要在eureka的client增加配置如下:
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=192.168.1.100:8001
第一项表示使用 ip地址的方式注册,第二项表示客户端注册的ip地址,如果设置了eureka.instance.ip-address 属性,则使用该属性配置的ip,否则自动获取除环路ip外的第一个ip地址
在我们的实际生产中,各个微服务部署在docker容器中,此时hostname即为容器id,会出现服务间通过网关调不通的情况,如果出现此情况时,请回过头来看这个内容。
4.3启动程序
启动类加@EnableEurekaClient注解,表面自己是Client。其实当前版本的Spring Cloud只要依赖了以spring-cloud-starter-netflix为前缀的库(例如spring-cloud-starter-netflix-eureka-client),就启用了服务注册发现功能;因此不用加@EnableEurekaClient注解都可以。
package com.kcsm.alh.alhtms;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class AlhTmsApplication {
public static void main(String[] args) {
SpringApplication.run(AlhTmsApplication.class, args);
}
}
启动应用后,进入注册中心http://localhost:6000可看到服务列表中已有此服务