Spring Cloud中的Eureka服务注册与发现

Eureka Server 端

导入依赖

Spring Cloud 版本为Finchley:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
application.properties
#我这个项目只提供给别人注册服务的平台,我自己不注册,也不需要用别人的服务
spring.application.name=eureka-server
#服务注册中心端口号
server.port=8080
#服务注册中心实例的主机名
eureka.instance.hostname=localhost
#是否向服务注册中心注册自己
eureka.client.register-with-eureka=false
#是否检索服务
eureka.client.fetch-registry=false
#服务注册中心的配置内容,指定服务注册中心的位置
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

启动类注解@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}

在浏览器中输入localhost:8080,出现Eureka 页面
在这里插入图片描述

Eureka Client 端

将自身服务注册到 Eureka Server,端口不能唯一

Eureka Client 端所依赖的注解如下:

<!--eureka-client客户端-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

项目配置文件需要添加如下信息:

spring.application.name=eureka-student
server.port=8091
eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka/

启动类添加@EnableEurekaClient 注解

@SpringBootApplication
@MapperScan("com.changan.eurekastudent.mapper")
@EnableEurekaClient
//@EnableDiscoveryClient
/*第二步*/
@EnableFeignClients
public class EurekaStudentApplication {

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

}

启动后再次打开localhost:8080

在这里插入图片描述
出现你注册的服务

转载出处 https://blog.csdn.net/eson_15/article/details/85002026

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值