Spring Cloud学习之Eureka服务发现

Spring Cloud Eureka
Eureka是Netflix开发的服务发现框架,集成在spring-cloud-netflix子项目中,用来作为服务注册发现之用。
1.创建一个eureka项目(使用IDE为Intellj Idea)
(1)选择File->New->Project->Spring Initializr

创建一个spring boot应用
(2)填写项目名为spring-cloud-eureka
这里写图片描述
(3)选择依赖组件
此处选择web和Cloud Discovery->Eureka Server,将此作为一个eureka注册发现的服务端
这里写图片描述
(4)项目创建完成,等待jar包下载完成。
pom.xml如下:

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

在application.properties中添加配置(配置文件也可以用application.yml或者bootstrap.yml或者bootstrap.properties,bootstrap*在application*之前加载)

#服务端口号,不指定,默认为8080
server.port=8761
#服务应用名称
spring.application.name=eureka-server
#服务实例指定用ip注册,默认为主机名注册
eureka.instance.prefer-ip-address=true
#服务实例id
eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}
#是否向注册中心注册自己
eureka.client.register-with-eureka=false
#是否检索服务
eureka.client.fetch-registry=false
#指定注册中心的地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
#关闭eureka的自我保护模式
eureka.server.enable-self-preservation=false

配置文件配置好之后,就可以在SpringCloudEurekaApplication类中启动项目了
项目启动成功之后,访问localhost:8761,可以看到注册中心控制台
这里写图片描述
可以看到,目前还没有一个服务实例注册到注册中心。
(5)创建一个spring-cloud-client项目,步骤同上,在选择依赖组件时,选择Eureka Discovery
这里写图片描述
pom.xml如下:

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>spring-cloud-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-cloud-client</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

application.properties配置:

#服务端口号,不指定,默认为8080
server.port=8880
#服务应用名称
spring.application.name=eureka-client
#服务实例指定用ip注册,默认为主机名注册
eureka.instance.prefer-ip-address=true
#服务实例id
eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}
#指定注册中心的地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

在启动类SpringCloudClientApplication上添加@EnableDiscoveryClient注解

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudClientApplication.class, args);
    }
}

启动项目,访问http://localhost:8761,查看eureka控制台,可以看到客户端已经成功注册到eureka服务发现中心。

这里写图片描述

猜你喜欢

转载自blog.csdn.net/x1172031988/article/details/80013153