从零到一简单构建和配置Eureka Server


一、构建Eureka Server

1、导入eureka-server的pom依赖

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    <version>2.2.4.RELEASE</version>
</dependency>

2、在application.yaml中进行相关配置:

server:
  port: 7001

# eureka 配置
eureka:
  instance:
    hostname: localhost # eureka服务端的实例名称
  client:
    register-with-eureka: false # 表示是否向eureka注册中心注册自己
    fetch-registry: false # fetch-registry为false,表示自己为注册中心
    service-url:  # 与服务中心交互的地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

3、主启动类,在上面添加@EnableEurekaServer注解

package com.xingyu.springcloud;

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

@SpringBootApplication
@EnableEurekaServer // 服务端启动类,可以接受其他服务注册进来
public class EurekaServer_7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer_7001.class, args);
    }
}

4、运行后访问http://localhost:7001/,可以访问到Eureka的初始页面:

在这里插入图片描述

注意:此时的Instances currently registered with Eureke 为空,因为还没有服务提供者注册服务


二、配置服务提供者

springcloud-provider-dept-8001

1、导入pom依赖:

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>1.4.7.RELEASE</version>
</dependency>

2、在application.yaml中进行相关配置:

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/
  instance:
    instance-id: springcloud-provider-dept8001

3、在主启动类上添加@EnableEurekaClient注解:

package com.xingyu.springcloud;

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


@SpringBootApplication
@EnableEurekaClient //在服务启动后,自动注册到eureka中
public class DeptProvider_8001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptProvider_8001.class, args);
    }
}

4、启动运行后,再次访问http://localhost:7001/,可以发现此时的Instances currently registered with Eureke 已经显示出来该实例:

在这里插入图片描述


三、其他操作

1、修改显示的实例id:

eureka:
  instance:
    instance-id: springcloud-provider-dept8001

2、配置点击实例链接后显示的内容(若不配置,显示的是404):

<!--    完善监控信息    -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>2.3.4.RELEASE</version>
</dependency>
info:
  app.name: xingyu-springcloud
  company.name: xingyu-XJTU

3、团队合作时,在服务中心可以通过 Discovery 实现服务发现机制:

启动类上增加注解:@EnableDiscoveryClient //服务发现

package com.xingyu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;


@SpringBootApplication
@EnableEurekaClient //在服务启动后,自动注册到eureka中
@EnableDiscoveryClient //服务发现
public class DeptProvider_8001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptProvider_8001.class, args);
    }
}

编写对应controller进行测试:

...
import org.springframework.cloud.client.discovery.DiscoveryClient;

@Autowired
private DiscoveryClient client;

...
    
@ApiOperation("注册进来的微服务,获取一些消息")
@GetMapping("/dept/discovery")
public Object discovery(){
    //获取微服务列表的清单
    List<String> services = client.getServices();
    System.out.println(services);

    //得到一个具体的微服务信息,通过具体的微服务id获取(application)
    List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
    for (ServiceInstance inctance:instances) {
        System.out.println(
                inctance.getHost()+"\t"
                +inctance.getPort()+"\t"
                +inctance.getUri()+"\t"
                +inctance.getInstanceId());

    }
    return client;
}

输出:

[springcloud-provider-dept]
DESKTOP-JSLIG7K	8001	http://DESKTOP-JSLIG7K:8001	springcloud-provider-dept8001

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值