spring cloud 注册与发现Eureka

spring cloud 注册与发现

在这里使用Eureka作为注册中心。

一、eureka-server

创建新的module:eureka-server

1.1 引入eureka-server依赖


    org.springframework.boot
    spring-boot-starter-web
    2.2.5.RELEASE


    org.springframework.boot
    spring-boot-starter-actuator
    2.2.5.RELEASE


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-server

1.2 配置文件

server.port=8761
#eureka server
eureka.instance.hostname=localhost
#注册到eureka服务中心
eureka.client.register-with-eureka=false
#检索服务
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

1.3 启动类

启动类添加注解@EnableEurekaServer

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

}

二、eureka-client

创建新的module:eureka-client

2.1 引入eureka-client坐标


    org.springframework.boot
    spring-boot-starter-web
    2.2.5.RELEASE


    org.springframework.boot
    spring-boot-starter-actuator
    2.2.5.RELEASE


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client
    2.2.2.RELEASE

2.2 配置文件

#注册进eureka,默认true
server.port=8001
eureka.client.register-with-eureka=true
#默认true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

spring.application.name=eureka-client

2.3 启动类

启动类添加注解@EnableEurekaClient

@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {

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

}

三、查看注册中心

访问eureka注册中心,可以查看已经注册到eureka的服务有哪些。

3.1 运行

依次启动eureka-server、eureka-client
spring cloud 注册与发现Eureka_第1张图片

3.2访问

访问http://localhost:8761
spring cloud 注册与发现Eureka_第2张图片

在这里可以看到已经注册进来的实例都有哪些。

你可能感兴趣的:(spring cloud 注册与发现Eureka)