二、微服务之注册中心

二、微服务之注册中心

Spring 版本确认

# 本次使用版本 
spring-cloud: Hoxton.Release 
spring-boot: 2.2.1.Release 
spring-boot-admin: 2.2.1 

# 超稳定版本 
spring-cloud: Greenwich.SR2 
spring-boot: 2.1.6.Release 
spring-boot-admin: 2.1.6

Maven 依赖

<?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 https://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.2.1.RELEASE</version>
        <relativePath/>
        <!--        lookup parent from repository -->
    </parent>
    <groupId>com.fatcat</groupId>
    <artifactId>fc-eureka</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>fc-eureka</name>
    <description>服务注册中心</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </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>
    <!--    maven仓库,id对应setting的mirrorOf-->
    <repositories>
        <repository>
            <id>public</id>
            <name>public</name>
            <url>http://nexus.fatcat.fan/repository/maven-public/</url>
        </repository>
    </repositories>
    <!--    组件库-->
    <pluginRepositories>
        <pluginRepository>
            <id>public</id>
            <name>public</name>
            <url>http://nexus.fatcat.fan/repository/maven-public/</url>
        </pluginRepository>
    </pluginRepositories>
    <!--    上传本地jar的路径-->
    <distributionManagement>
        <repository>
            <id>releases</id>
            <name>releases</name>
            <url>http://nexus.fatcat.fan/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <name>snapshots</name>
            <url>http://nexus.fatcat.fan/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
</project>

注册中心配置文件(bootstrap.properties 或 application.properties)

# 服务端口号 
server.port=8761 
# 当前服务名 
spring.application.name=fc-eureka 
# 自我保护 
#eureka.server.enable-self-preservation=false 
# 以下配置均以客户端角度配置,eureka可忽略 
# 是否注册自身 
eureka.client.register-with-eureka=false 
# 是否检索其他服务,开启后项如果没有其他服务会报错 
eureka.client.fetch-registry=false 
# 实例注册至eureka的地址,缺省会有默认值 
# eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ 
# 实例的主机地址 
# eureka.instance.hostname=localhost 
# 服务的心跳时间,即30s检索一次服务是否存活 
# eureka.instance.lease-renewal-interval-in-seconds=30 
# 服务心跳时间过期时间,即60s没有心跳注册中心剔除服务 
# eureka.instance.lease-expiration-duration-in-seconds=60

注册中心启动类

/* 启用注册中心 Server 端服务 */ 
@SpringBootApplication 
@EnableEurekaServer 
public class FcEurekaApplication { 
  public static void main(String[] args) { 
    SpringApplication.run(FcEurekaApplication.class, args); 
  } 
}