Eureka是springcloud的官方推荐使用的服务注册中心,但是现在Eureka停止了开源维护,所以之后何去何从就不知道了,但是我个根据很多的开发人员的回答发现都对阿里的SpringCloud Alibaba Nacos抱有很大的期望。这个就需要自己私下学习了。
话不多说
开发工具–IDEA
语言–JAVA
首先
1、创建一个Eureka的微服务。
2、然后选择版本–这里的JDK版本根据自己的进行选择,最好是1.8以上的。1.8以下的我也没有用过。没办法进行测试。
3、命名
4、创建后的列表
5、写pom文件
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
6、创建主启动类和yml文件。
6.1、启动类,添加@EnableEurekaServer
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}
6.2、yml配置文件
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
#集群指向其它eureka
#defaultZone: http://eureka7002.com:7002/eureka/
#单机就是7001自己
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
7、测试
启动Eureka7001端口的微服务。
打开这个页面,表示Eureka服务注册中心,开启成功。
8、生产者服务注册服务
我的是8001端口。因为之前就建好了8001端口的服务,所以这里都不在一步一步的搭建了,不会的请看上面1、2、3步。
8.1、改pom文件,添加Eureka-client的jar包。
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
8.2、修改application.yml配置文件。添加红框内容
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true。
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
#单机版
defaultZone: http://localhost:7001/eureka
# 集群版
#defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
注意每个的位置,yml对格式要求非常严格,多个空格或者位置不对就会造成报错,无法启动。
8.3、修改主启动类。添加@EnableEurekaClient
最后启动测试8001端口是否注册成功。