服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - JAVA教程 - spring-cloud入门之eureka-server(服务发现)

spring-cloud入门之eureka-server(服务发现)

2021-03-24 14:14大黄蜂coder JAVA教程

本篇文章主要介绍了spring-cloud入门之eureka-server(服务发现),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

前言

eureka是一个服务发现和注册框架,细的来说,我们可以分为eureka-server(服务发现)和eureka-client(服务注册)两个,本次我们对eureka-server(服务发现)做一个项目搭建,作为spring-cloud的开篇。

开源地址:https://github.com/bigbeef

项目结构

spring-cloud入门之eureka-server(服务发现)

maven结构大家应该都清楚(不清楚的需要补一补,百度关于maven的文章不计其数),下面我们来看一看这些关键文件的配置

代码编写

cppba-spring-cloud > pom.xml

?
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
32
33
34
35
36
37
<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.cppba</groupid>
  <artifactid>cppba-spring-cloud</artifactid>
  <version>1.0.0</version>
  <packaging>pom</packaging>
  <name>${project.artifactid}</name>
 
  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>1.5.5.release</version>
  </parent>
 
  <properties>
    <project.build.sourceencoding>utf-8</project.build.sourceencoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>dalston.sr2</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>
 
  <modules>
    <module>cppba-spring-cloud-eureka-server</module>
  </modules>
</project>

cppba-spring-cloud-eureka-server > pom.xml

?
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
32
<?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>
  <artifactid>cppba-spring-cloud-eureka-server</artifactid>
  <packaging>jar</packaging>
  <name>${project.artifactid}</name>
 
  <parent>
    <groupid>com.cppba</groupid>
    <artifactid>cppba-spring-cloud</artifactid>
    <version>1.0.0</version>
  </parent>
 
  <dependencies>
    <dependency>
      <groupid>org.springframework.cloud</groupid>
      <artifactid>spring-cloud-starter-eureka-server</artifactid>
    </dependency>
  </dependencies>
 
    <build>
    <finalname>${project.name}</finalname>
    <plugins>
      <!--打包可执行的jar-->
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
   </build>
</project>

springcloudeurekaserverapplication.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.cppba;
 
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.cloud.netflix.eureka.server.enableeurekaserver;
 
@enableeurekaserver
@springbootapplication
public class springcloudeurekaserverapplication {
 
  public static void main(string[] args) {
    springapplication.run(springcloudeurekaserverapplication.class, args);
  }
}

application.properties

?
1
2
3
4
5
6
server.port=8761
 
eureka.instance.hostname=eureka-server
eureka.client.registerwitheureka=false
eureka.client.fetchregistry=false
eureka.client.serviceurl.defaultzone=http://${eureka.instance.hostname}:${server.port}/eureka/

到此项目搭建完成

启动项目

我们启动springcloudeurekaserverapplication中的main方法,访问http://127.0.0.1:8761

spring-cloud入门之eureka-server(服务发现)

到此,eureka-server(服务发现)项目搭建成功

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.jianshu.com/p/c71ce827b261

延伸 · 阅读

精彩推荐