创建服务的注册与发现 Eureka

1、创建一个工程

2、向pom文件中增加如下:

<dependencies>
        <!--eureka-server服务端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
</dependencies>

3、添加application.yml文件

server:
  port: 7001
eureka:
  instance:
    hostname: localhost #eureka服务端的实例名称
  client:
    register-with-eureka: false #false表示不向注册中心注册自己。Eureka不响自己注册
    fetch-registry: false       #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ (单机)
      #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

4、增加服务启动类,并在类上增加  @EnableEurekaServer 注解, 如下所示:

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

@SpringBootApplication
@EnableEurekaServer       // EurekaServer服务器端启动类,接受其它微服务注册进来
public class EurekaServer7001
{
    public static void main(String[] args)
    {
        SpringApplication.run(EurekaServer7001.class, args);
    }
}

5、启动服务,在浏览器中打开 http://localhost:7001,显示如下图

猜你喜欢

转载自www.cnblogs.com/yufeng218/p/10726972.html