美文网首页
SpringCloud学习六:Eureka入门

SpringCloud学习六:Eureka入门

作者: yuluo | 来源:发表于2018-10-07 16:54 被阅读9次

Eureka Server搭建

首先,在IDEA中新建一个module或者project。

引入依赖

国际惯例,引入依赖。

<?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>online.suiyu</groupId>
    <artifactId>eureka-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.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>Finchley.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-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </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>
</project>

其中,起主要作用的是spring-cloud-starter-netflix-eureka-server,它向我们标识了这是一个eureka server。

配置application.yml

server:
  port: 7001
eureka:
  instance:
    hostname: localhost #eureka服务端实例名称
  client:
    register-with-eureka: false #是否向注册中心注册自己
    fetch-registry: false #false表示本就是注册中心,职责就是维护服务实例,而非检索服务
    eureka-server-u-r-l-context: http://${eureka.instance.hostname}:${server.port}/eureka/ #请求地址

启用Eureka服务

在主程序中加入注解@EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class EurekaDemoApplication {

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

至此,一个简单的Eureka Server搭建成功

Eureka Client

首先,创建一个简单的Springboot项目,此处我用到了以前写的简单项目,可以翻以前的同系列文章。

添加依赖

<!-- spring-cloud-starter-eureka client-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <!--<version>1.4.5.RELEASE</version>-->
</dependency>

另外,因为这需要是一个SpringCloud项目,因此,需要一些其他配置

<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>Finchley.SR1</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>

添加配置

Eureka Client需要一些配置,让它去找Eureka Server注册

eureka:
  client: #eureka client向server注册
    service-url:
      defaultZone: http://localhost:7001/eureka/ #注册地址

另外,需要给Client添加名称

spring:
  application:
    name: microservicecloud-dept

添加注解

同样,在主程序中添加@EnableEurekaClient即可

以上成功后,即可运行Server与Client,此时Client已经向Server注册本身服务,打开http://localhost:7001/,看到如下内容

Eureka
其中,microservicecloud-dept就是我们注册的服务名称。

相关文章

网友评论

      本文标题:SpringCloud学习六:Eureka入门

      本文链接:https://www.haomeiwen.com/subject/fpilaftx.html