您的位置:首页 > 编程语言 > Java开发

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

2019-06-21 17:11 302 查看

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://www.geek-share.com/detail/2755770068.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐