eureka是一个服务注册中心,在微服务中,服务的发现与注册都离不开他,当然,从eureka停更后,我们可以用zookeep进行代替他的使用
上干货了!
- 新建一个空的项目,修改配置文件为yml文件
- 然后在配置文件中添加配置信息
#eureka的服务端,心跳单位毫秒
server:
port: 8088
eureka:
client:
service-url:
#设置交互的地址
#本机访问eureka是设置为localhost,在虚拟机中使用是用虚拟机IP,全项目唯一
defaultZone: http://root:root@localhost:8787/eureka
#是否从Eureka获取注册信息,当前是单点,不需要同步其他所以设置为false,在集群中要改为true
fetch-registry: false #通过该配置和下面的配置区分服务端和客服端 没有该配置的为客货端
#是否向注册中心注册自己
register-with-eureka: false
server:
#禁用自我保护模式
enable-self-preservation: false
# 续期时间,即扫描失效服务的间隔时间 没有禁用时是90s
eviction-interval-timer-in-ms: 2000
#开启用户验证
security:
basic:
enabled: true #开启用户验证 false为不开启
user: #设置用名密码
name: root
password: root
- 配置信息完成之后,我们需要添加我们的eureka的依赖,不然你的配置文件会报错的;
<!--Eureka依赖jar包start-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<!--Eureka send-->
<!--开启用户验证的依赖jar包start-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
- 下面在启动类上添加注解
@SpringBootApplication
@EnableEurekaServer /*标注为eureka的服务注册中心*/
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
- 就可以启动项目了