版本
Spring Boot 2.7.3
Spring Cloud 2021.0.4
Spring Cloud Eureka Server配置,即注册中心搭建
Maven依赖
- pom.xml
<parent>
<groupId>com.demo</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
- parent pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.4</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>
使用注解@EnableEurekaServer
@EnableEurekaServer //开启Eureka Server
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
配置文件
- 如果是单节点配置,配置文件如下:application.yml
server:
port: 8801
eureka:
instance:
hostname: localhost #hostname也可以取巧直接使用ip地址
instance-id: ${eureka.instance.hostname}:${server.port}
#prefer-ip-address: true
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
- 如果是HA配置(高可用配置),以两个节点为例,配置文件如下:
节点1:
spring:
application:
name: eureka-server
server:
port: 8801
eureka:
instance:
hostname: node1.eureka-server.com #注意:搭建高可用注册中心时,eureka.instance.hostname必须不相同,否则无法完成数据同步,尤其在同一台机器上搭建测试的时候,要尤其注意。
instance-id: ${eureka.instance.hostname}:${server.port}
#prefer-ip-address: true
client:
register-with-eureka: true # 把自己也注册到注册中心去, eureka server 的高可用就是把自身当作服务实例注册到服务注册中心去
fetch-registry: true #开启服务发现,会从注册中心获取已注册的服务实例信息
serviceUrl:
defaultZone: http://node2.eureka-server.com:8801/eureka/ # 如果有多个,使用逗号分隔,并不需要把所有注册中心地址都列出来,只要能保证互相连通就可以了
节点2:
spring:
application:
name: eureka-server
server:
port: 8801
eureka:
instance:
hostname: node2.eureka-server.com
instance-id: ${eureka.instance.hostname}:${server.port}
#prefer-ip-address: true
client:
register-with-eureka: true
fetch-registry: true
serviceUrl:
defaultZone: http://node1.eureka-server.com:8801/eureka/
说明:如果开启eureka.instance.prefer-ip-address的话,就是用本机ip当作服务地址注册到注册中心去,这时就无需配置eureka.instance.hostname了;另外,yml配置文件中支持使用${}读取环境变量
验证Eureka Server
到此,Eureka注册中心已经搭建完成,启动服务后,用浏览器访问地址:http://127.0.0.1:8801,就可以看到服务信息了
Spring Cloud Eureka Client配置,这个配置就是在自己的业务项目中配置的
在Maven项目的pom.xml文件中引入以下依赖(spring-cloud-dependencies参考上面)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>...</version>
</dependency>
配置文件添加注册中心信息
eureka:
instance:
#hostname: localhost
instance-id: ${spring.cloud.client.ip-address}:${server.port}
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://registry-server.fwzf.dy-technology.com/eureka/
说明:如果开启了prefer-ip-address的话,可以使用${spring.cloud.client.ip-address}引用ip标识ip地址,这是个内置属性的引用。
使用注解@EnableDiscoveryClient和@EnableEurekaClient(可以省略)
区别:
- @EnableEurekaClient,只能用于Eureka为注册中心的client端。
- @EnableDiscoveryClient,可以用于Eureka为注册中心也可以兼容其他注册中心。
说明:
从Dalston(不含)之后的版本已经不需要再写@EnableEurekaClient或@EnableDiscoveryClient了。
//@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
[目录页链接] 点我回到顶级目录
本文暂时没有评论,来添加一个吧(●'◡'●)