SpringCloud学习二:服务注册与发现Eureka

SpringCloud学习二:服务注册与发现Eureka

  •  2019 年 3 月 4 日
  •  421
  •  Java SpringCloud Eureka 

话不多说直接开始~

创建父工程

在这个系列的文章中使用的是maven多模块项目构建,所以先创建一个父工程(spring-cloud-demo)来管理我们要依赖的jar包的坐标。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">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.13.RELEASE</version>
        <relativePath/>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
    <modules>
        <module>server-eureka</module>
    </modules>
    <groupId>site.syso</groupId>
    <artifactId>spring-cloud-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-demo</name>
    <description>Demo project for Spring Cloud</description>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR5</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>

创建注册中心

创建子模块server-eureka作为注册中心,pom.xml依赖如下:

<dependencies>
    <!-- eureka -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
    <!--test-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

启动类

启动类添加注解@EnableEurekaServer,表明这是一个注册中心

@SpringBootApplication
@EnableEurekaServer
public class ServerEurekaApplication {

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

}

配置文件

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url: 
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

  • register-with-eureka表示是否注册到server,这里设置为false
  • fetch-registry表示是否从server获取服务注册信息,也设置为false

启动server-eureka

启动后浏览器访问localhost:8761看到如下界面就成功了


可以看到图中红线标注的了No instances avaliable表示还没有服务注册上来,接下来我们创建一个服务提供者。

创建服务提供者

再创建一个子模块client-eureka,pom.xml依赖如下

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

配置文件

spring:
  application:
    name: client-eureka

server:
  port: 8762

eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

启动类

启动类添加注解@EnableEurekaClient,表明这是一个服务提供者

@SpringBootApplication
@EnableEurekaClient
@RestController
public class ClientEurekaApplication {

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

    @GetMapping("/")
    public String test(){
        return "hello eureka !";
    }

}

启动server和client,再次访问localhost:8761可以看到我们的服务已经注册上来了,访问localhost:8762也能看到hello eureka ! 

ApplicationAMIsAvailability ZonesStatus
CLIENT-EUREKAn/a (1)(1)UP (1) - bogon:client-eureka:8762

源码地址

https://github.com/chenpeidong/spring-cloud-demo

扫一扫分享到微信

已有 条评论
写评论