注册服务端Eureka

依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?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">
<parent>
<artifactId>SpringcCloud</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>Springcloud-Eureka-7001</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
</dependencies>

</project>

配置文件:

1
2
3
4
5
6
7
8
9
10
11
server:
port: 7001

eureka:
instance:
hostname: localhost
client:
register-with-eureka: false #是否向注册中心注册自己,由于自己就是注册中心,所以这个选项为false
fetch-registry: true #s 是否是注册中心
service-url:
defaultZone: Http://${eureka.instance.hostname}:${server.port}/eureka

这里实现了动态管理地址。使用${}来取值;

关于配置文件的defaultZone的来源:

image-20201023210011801

image-20201023205801863

明显可以看出来,serviceUrl的一个属性就叫做defaultZone,我们要想改变就是在里面改写属性;

主启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.saxon.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class Springcloud_Eureka {
public static void main (String[] args) {
SpringApplication.run (Springcloud_Eureka.class,args);
}
}

启动的时候,如果出现版本问题就去网上寻找答案;