<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>${tomcat.scope}</scope>
</dependency>
</dependencies>
</dependencyManagement>
### 创建服务端子工程
* 创建注册中心第一个服务端项目registry
* 引入**eureka-server**的依赖
<groupId>com.cxy965</groupId>
<artifactId>registry</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>com.cxy965</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
* 编写启动类,增加**@EnableEurekaServer**注解,表示为服务端
package com.cxy965.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
- @Author:公众号:程序员965
- @create 2022-06-06
**/
@EnableEurekaServer
@SpringBootApplication
public class RegistryApplication {
public static void main(String[] args) {
SpringApplication.run(RegistryApplication.class, args);
}
}
* 新建application.yml配置文件,定义端口等信息
server:
port: 8001
spring:
application:
name: registry
eureka:
instance:
hostname: 0.0.0.0
lease-renewal-interval-in-seconds: 10 #服务的注册涉及到心跳连接,默认为每30s
lease-expiration-duration-in-seconds: 30 #服务被移除的时间默认为90s
client:
healthcheck:
enabled: true #开启心跳
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://
e
u
r
e
k
a
.
i
n
s
t
a
n
c
e
.
h
o
s
t
n
a
m
e
:
{eureka.instance.hostname}:
eureka.instance.hostname:{server.port}/eureka/
registry-fetch-interval-seconds: 30 #eureka client间隔多久去拉取服务注册信息,默认为30秒
这样第一个Eureka服务端就开发完成了,接下来我们创建集群。
## 三、Eureka服务端集群搭建
如果要搭建服务端集群,需要开启 Eureka 集群配置,各服务端启动时 Eureka Server 会将注册信息向其它 Eureka Server 进行同步,因此搭建高可用集群架构只需要将 Eureke Server 配置指向其它可用的 serviceUrl 即可。
* 复制上面 Eureka 单个服务端项目,创建集群第一个服务eurekaserver1;
* 修改application.yml配置文件;
在上面 Eureka 单个服务端项目的基础上,修改配置文件中的 *register-with-eureka*、***fetch-registry以及****serviceUrl.defaultZone*参数,如下:
server:
port: 8001
spring:
application:
name: registry
eureka:
instance:
hostname: 0.0.0.0
lease-renewal-interval-in-seconds: 10 #服务的注册涉及到心跳连接,默认为每30s
lease-expiration-duration-in-seconds: 30 #服务被移除的时间默认为90s
client:
healthcheck:
enabled: true #开启心跳
register-with-eureka: true
fetch-registry: true
serviceUrl:
defaultZone: http://localhost:8002/eureka/,http://localhost:8003/eureka/
registry-fetch-interval-seconds: 30 #eureka client间隔多久去拉取服务注册信息,默认为30秒
* 同样的,再复制eurekaserver1为eurekaserver2,eurekaserver3两个项目;
* 将eurekaserver2、eurekaserver3的端口分别改成8002、8003;
* 将eurekaserver2、eurekaserver3的注册地址分别改成8001/8003和8001/8002;
分别启动eurekaserver1、eurekaserver2、eurekaserver3三个项目即可。
***注意,因为为本地测试项目,三个项目均部署在本地localhost,作为高可用集群环境,为避免服务器崩溃后集群中其他项目可用,需部署在不同服务器上,部署在不同服务器后,端口可以改为相同的。***
## 四、创建Eureka客户端
* 创建第一个客户端项目client
* 与服务端不同的是,客户端需要引入**eureka-client**的依赖
org.springframework.boot spring-boot-starter-web
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
```
- 编写启动类,注意增加的是**@EnableEurekaClient**注解,表示此为客户端
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @Author:公众号:程序员965
* @create 2022-06-06
**/
@EnableEurekaClient
@SpringBootApplication
public class AppApplication {
public static void main(String[] args) {
SpringApplication.run(AppApplication.class, args);
}
}
- 创建application.yml配置文件,注册至服务端
server:
port: 8002
spring:
application:
name: app
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
五、项目启动验证
分别启动服务端和客户端,打开浏览器中输入http://localhost:8001/;显示客户端注册成功:
总结:Eureka Server之间通过复制的方式完成数据的同步,Eureka还提供了客户端缓存机制,即使所有的Eureka Server都挂掉,客户端依然可以利用缓存中的信息消费其他服务的API。综上,Eureka通过心跳检查、客户端缓存等机制,确保了系统的高可用性、灵活性和可伸缩性。
六、Spring cloud版本介绍
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。*
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!