返回 导航

SpringBoot / Cloud

hangge.com

SpringCloud - 服务注册与发现组件Eureka的使用详解2(创建并注册服务提供者)

作者:hangge | 2020-07-01 08:10
    前文演示了如何搭建一个服务注册中心(点击查看),本文接着演示如何将一个既有的 Spring Boot 应用加入 Eureka 的服务治理体系中去。

二、创建并注册服务提供者

1,添加依赖

首先创建一个 Spring Boot 工程,然后在 pom 文件中引入相关依赖:
<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-client</artifactId>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2,Eureka Client 相关配置

接着编辑项目的 application.properites 文件,添加如下配置为服务命名,并指定服务注册中心的地址:
#为服务命名
spring.application.name=hello-service
#指定服务注册中心的地址
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

3,启动 Eureka 客户端

    在主类上添加 @EnableDiscoveryClient 注解,激活 Eureka 中的 DiscoveryClient 实现(自动化配置,创建 DiscoveryClient 接口针对 Eureka 客户端的 EurekaDiscoveryClient 实例):
@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context
                = SpringApplication.run(DemoApplication.class, args);
    }
}

4,打印 DiscoveryClient 相关内容

创建一个 Controller,通过注入 DiscoveryClient 对象,在日志中打印出所有服务实例的相关内容:
注意:这一步不是必须的,只是演示如何获取 DiscoveryClient 对象。
@RestController
public class HelloController {

    // 服务发现客户端
    @Autowired
    private DiscoveryClient client;

    @GetMapping("/hello")
    public void hello() {
        //获取所有的服务实例并打印出来
        client.getServices().forEach(id -> {
            client.getInstances(id).forEach(instance -> {
                System.out.println("host:" + instance.getHost()
                        + ", service_id:" + instance.getServiceId());
            });
        });
    }
}

5,运行测试

(1)启动项目,可以看到控制台中 com.netflix.discovery.DiscoveryClient 对象打印了该服务的注册信息,表示服务注册成功:

(2)通过访问 Eureka 的信息面板,在 Instances currently registered with Eureka 一栏可以看到服务的注册信息:

(3)而访问我们前面定义的 /hello 接口,控制台则会输出如下信息:

附:其他的一些常用配置

1,服务注册

(1)默认情况下,eureka.client.register-with-eureka 参数默认值为 true。也就是说“服务提供者”在启动的时候会通过发送 REST 请求的方式将自己注册到 Eureka Server 上(同时带上自身服务的一些元数据信息)

(2)如果将其设置为 false 将不会自动注册操作。
#不注册到Eureka Server上
eureka.client.register-with-eureka=false

2,服务续约

(1)在注册完服务之后,服务提供者会维护一个心跳用来持续告诉 Eureka Server我还活着”,以防止 Eureka Server 的“剔除任务”将该服务实例从服务列表中排除出去,我们称该操作为服务续约(Renew

(2)关于服务续约有如下两个重要属性,我们可以根据需求来调整:
#定义服务续约任务的调用时间间隔(即发送心跳给server端的频率),默认为30秒
eureka.instance.lease-renewal-interval-in-seconds=30
#定于服务失效的时间(即server端多长时间没收到心跳后就将此实例剔除),默认为90秒
eureka.instance.lease-expiration-duration-in-seconds=90

3,使用IP地址进行服务注册

(1)默认情况下,服务提供者向 Eureka 注册中心注册,默认以 hostname 的形式显示:

(2)如果我们需要显示成“IP+端口”形式,可以修改服务提供者的配置,增加如下两个配置即可:
注意:经过测试,事实上只需要添加第二个配置即可。但为保险起见,两个配置都添加。
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}

(3)重启服务,可以看到注册列表里已经显示成 IP 形式了:
评论

全部评论(0)

回到顶部