(1)Eureka分为服务端和客户端,服务端作为服务注册中心,客户端则与服务注册中心进行交互,发送心跳维持连接。
(2)Eureka用于实现服务注册、服务发现。
(3)服务启动时,会提交该服务的主机信息、别名等信息到服务注册中心,在使用RPC框架调用其他服务时,利用别名到注册中心获取服务实例地址。
(4)SpringCloud已经给Eureka的依赖决定了版本号,可以使用默认版本号。
(1)
(2)
(3)编写pom文件
<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">
<parent>
<artifactId>demo20220821artifactId>
<groupId>com.wsh.springcloudgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-eureka-server7001artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
<dependency>
<groupId>com.wsh.springcloudgroupId>
<artifactId>cloud-api-commonartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
dependency>
dependencies>
project>
(4)编写yml文件
server:
port: 7001
eureka:
instance:
hostname: localhost
client:
# 服务端设置为false,表明该实例作为服务注册中心
register-with-eureka: false
# 服务端设置为false,表明该实例作为服务注册中心
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
service-url:服务注册中心的地址
(5)编写启动类
@EnableEurekaServer
@SpringBootApplication
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class, args);
}
}
(6)访问http://localhost:7001
(1)pom文件加入Eureka客户端依赖
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
(2)yml文件加入Eureka配置
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: wsh666
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.wsh.springcloud.entity
eureka:
client:
# 客户端设置为true
register-with-eureka: true
# 客户端设置为true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
(3)访问http://localhost:7001