项目示例 - 2.服务注册 - 1.Eureka

项目示例 - 2.服务注册 - 1.Eureka

关联知识:

  • 分布式微服务 - 2.服务注册 - 2.Eureka

内容提要

  • 服务注册基本使用
  • eureka server集群、eureka client集群

服务注册基本使用

Eureka Server

  1. 建Module:微服务起名为eureka-server
  2. 改pom:引入以下依赖
    <dependencies>
        <!-- eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
  1. 写yml:在resources目录下创建application.yml文件,并做以下配置
server:
  port: 7001

eureka:
  instance:
    # eureka server名称
    hostname: eureka-server7001
  client:
    # 不向服务注册中心注册自身
    register-with-eureka: false
    # 不会拉取服务列表
    fetch-registry: false
    service-url:
      # eureka server的地址
      defaultZone: http://localhost:${server.port}/eureka
  1. 主启动:在src下创建如下主启动类
package learn.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServer {

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

}
  1. 测试:启动本微服务后,在浏览器中输入localhost:7001 地址成功显示eureka server网页

服务提供者

  1. 建Module:微服务起名为eureka-provider
  2. 改pom:引入以下依赖
    <dependencies>
        <!-- eureka-client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
  1. 写yml:在resources目录下创建application.yml文件,并做以下配置
server:
  port: 8001

spring:
  application:
    name: eureka-provider

eureka:
  client:
    service-url:
      # 服务注册中心配置的地址
      defaultZone: http://localhost:7001/eureka
  1. 主启动:在src下创建如下主启动类
package learn.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class EurekaProvider {

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

}
  1. 业务构建:创建如下controller类
package learn.demo.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequestMapping("/eureka/provider/")
public class ProviderController {
    @Value("${spring.application.name} ${server.port}")
    private String server;

    @GetMapping("test")
    public String test() {
        return server+"\t"+ UUID.randomUUID();
    }

}
  1. 测试:依次启动eureka server和eureka provider微服务(注意:eureka client启动前需要先启动eureka server)后,浏览器访问接口localhost:8001/eureka/provider/test 后正确返回信息,且输入localhost:7001 地址打开eureka server网页后能看到名为EUREKA-PROVIDER的application,即服务提供者运行正常且注册成功。(注意:仅有服务提供者进行注册,服务消费者使用服务提供者的接口的步骤在后续服务调用 的知识点中)

集群

Eureka Server集群

  1. 建Module:微服务起名为eureka-server1
  2. 改pom:引入以下依赖(与eureka-server微服务相同)
    <dependencies>
        <!-- eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
  1. 写yml:在eureka-server1微服务的resources目录下创建application.yml文件,并做以下配置;同时修改eureka-server微服务的application配置,修改后的配置如下
## eureka-server1

server:
  port: 7002

eureka:
  instance:
    # eureka server名称
    hostname: eureka-server7002
  client:
    # 不向服务注册中心注册自身
    register-with-eureka: false
    # 不会拉取服务列表
    fetch-registry: false
    service-url:
      # eureka server的地址
      #defaultZone: http://localhost:${server.port}/eureka
      # eureka server集群模式下,配置除本身外其他实例的地址(多个实例时,逗号分隔),将自身注册到其他eureka server实例中
      defaultZone: http://localhost:7001/eureka
## eureka-server

server:
  port: 7001

eureka:
  instance:
    # eureka server名称
    hostname: eureka-server7001
  client:
    # 不向服务注册中心注册自身
    register-with-eureka: false
    # 不会拉取服务列表
    fetch-registry: false
    service-url:
      # eureka server的地址
      #defaultZone: http://localhost:${server.port}/eureka
      # eureka server集群模式下,配置除本身外其他实例的地址(多个实例时,逗号分隔),将自身注册到其他eureka server实例中
      defaultZone: http://localhost:7002/eureka
  1. 主启动:在eureka-server微服务的src下创建如下主启动类
package learn.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServer1 {

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

}
  1. 服务提供者注册:修改eureka-provider微服务application.yml配置中的defaultZone,修改后的配置如下
server:
  port: 8001

spring:
  application:
    name: eureka-provider

eureka:
  client:
    service-url:
      # 服务注册中心配置的地址
      #defaultZone: http://localhost:7001/eureka
      # 注册到eureka server集群
      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka
  1. 测试:
    1. 启动eureka-server和eureka-server1微服务后,浏览器输入localhost:7001 localhost:7002 地址后,能正常打开两个eureka首页,且在DS Replicas一栏下可以看到除自身外其他eureka server
    2. 启动eureka-provider微服务,浏览器输入localhost:8001/eureka/provider/test 访问接口能正确返回信息,且刷新两个eureka首页后,都能在看到EUREKA-PROVIDER的application,表明注册到eureka server集群成功

Eureka Client集群

  1. 建Module:微服务起名为eureka-provider1
  2. 改pom:引入以下依赖(与eureka-provider微服务相同)
    <dependencies>
        <!-- eureka-client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
  1. 写yml:在resources目录下创建application.yml文件,并做以下配置
server:
  port: 8002

spring:
  application:
    name: eureka-provider

eureka:
  client:
    service-url:
      # 服务注册中心配置的地址
      #defaultZone: http://localhost:7001/eureka
      # 注册到eureka server集群
      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka
  1. 主启动:在src下创建如下主启动类
package learn.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class EurekaProvider1 {

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

}
  1. 业务构建:创建controller类如下
package learn.demo.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequestMapping("/eureka/provider1/")
public class ProviderController {
    @Value("${spring.application.name} ${server.port}")
    private String server;

    @GetMapping("test")
    public String test() {
        return server+"\t"+ UUID.randomUUID();
    }

}
  1. 测试:
    1. 启动eureka-server和eureka-server1两个微服务,打开两个eureka server首页,正常显示信息
    2. 启动eureka-provider和eureka-provider1两个微服务,浏览器访问localhost:8001/eureka/provider/test localhost:8002/eureka/provider1/test 两个接口,能正确返回信息,即两个服务提供者正常启动
    3. 刷新两个eureka server首页,可以看到有一个EUREKA-PROVIDER的application,且status属性上显示有两个实例
  • 21
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值