第1篇:服务注册中心与发现(Eureka)

一、创建服务注册中心

eureka是一个服务注册和发现模块
1.1、右键工程->New Project-> 选择spring initialir 如下图:
这里写图片描述
1.2、next->选择cloud discovery->eureka server
这里写图片描述

1.3 一路next,最后finish

二、配置服务注册中心

2.1、 启动一个服务注册中心,只需要一个注解@EnableEurekaServer,这个注解需要在springboot工程的启动application类上

@SpringBootApplication
@EnableEurekaServer
public class HbuLiveEurakaServerApplication {

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

2.2 配置文件
application.properties

server.port=8761
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

server.port=8761 指明端口
eureka.instance.hostname=localhost 部署到本地(若部署到远程服务器,这里为远程服务器地址)
通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server.
eureka.client.service-url.defaultZone 服务注册中心地址

2.3、启动工程,打开浏览器访问: 127.0.0.1:8761

现在没有服务注册所以是No application available 没有服务被发现 ……

这里写图片描述

三、创建一个服务提供者 (eureka client)

3.1 创建工程同上

3.2修改配置

1、通过注解@EnableEurekaClient 表明自己是一个eurekaclient.

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

2、配置文件

server.port=8762
spring.application.name=eureka-client
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

server.port 端口号
spring.application.name 微服务的名称,很重要
eureka.client.service-url.defaultZone 注册中心地址

3、启动工程
再次访问http://localhost:8761 ,刚刚的eureka-client服务成功注册上了
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值