一、创建服务注册中心
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服务成功注册上了