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

SpringCloud组件Eureka之服务注册

2019-06-20 09:31 429 查看

SpringCloud组件Eureka之服务注册

1.首先新建一个Meven主工程

2.创建完之后 搭建服务注册中心(eureka-server)新建springboot项目

注意:包名必须和meven项目一致

加入Eureka Server依赖

3.然后开始写配置文件 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:{eureka.instance.hostname}:eureka.instance.hostname:{server.port}/eureka/

4.配置完之后开始在启动文件加入注解

@EnableEurekaServer 开启EurekaServer服务

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}

5.这个时候可以启动项目 访问端口号localhost://8080

可以看到如下界面


我们可以看出来 服务已经开启了 但是没有注册 我们接下来写一个注册

6.新建springboot项目(eureka-client)

创建方式和 eureka-server类似 只有在选择组件的时候勾选web 和 Eureka Disovery


创建目录如下

7.配置application.properties文件

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

8.在启动类加入注解

@EnableDiscoveryClient
开启EurekaClient服务

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

启动项目

这时我们会看到 EUREKA-CLIENT 已经注册成功

完毕!!!

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