写于:2018-12-18 08:52:37
一、概述
1.1、官方介绍
Service Discovery is one of the key tenets of a microservice based architecture. Trying to hand configure each client or some form of convention can be very difficult to do and can be very brittle. Eureka is the Netflix Service Discovery Server and Client. The server can be configured and deployed to be highly available, with each server replicating state about the registered services to the others.
粗略翻译成以下几点:
1、服务发现是微服务架构很重要的组成部分。 2、Eureka 是 Netflix 公司开发的一个服务发现和服务客户端为一体的工具。 3、Eureka 能够实现服务间的注册发现。
1.2、Netflix 停止维护 Eureka
首先,Eureka 已经经过市场的验证,能够符合大部分企业的应用需求。
于此同时,Spring Cloud 接力二次开发,提供 Spring Cloud Netflix Eureka 的持续迭代。
二、Getting Started
code.zip
项目结构如下:
step1、关键依赖
<!-- eureka server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
step1、 application.properteis
配置
## 服务名称
spring.application.name = eureka-registry
## port
server.port = 1111
## 实例主机名称
eureka.instance.hostname = localhost
## 不向注册中心注册自己
eureka.client.register-with-eureka = false
## 不拉取服务列表信息
eureka.client.fetch-registry = false
## 指定服务注册中心
eureka.client.eureka-server-url.defaultZone = http://${eureka.instance.hostname}:1111/eureka/
step2、引导类追加注解 @EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class ApplicationRegistryApplication {
......
}
step3、设置安全校验
pom 文件中追加依赖
<!-- security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
application.properteis
追加配置
## eureka 账号
spring.security.user.name = zhixing
spring.security.user.password = zhixing
step4、启动服务,并访问
step5、client 端注册到 eureka 中
client 端引入 pom 依赖
<!-- eureka client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Application.java 追加 @EnableDiscoveryClient
注解
@EnableDiscoveryClient
......
public class Application {
......
}
bootstrap.properteis
或 application.properteis
等位置追加配置
## eureka 服务地址
eureka.client.service-url.defaultZone = http://zhixing:zhixing@127.0.0.1:1111/eureka/
step5、查看此时的注册信息
三、Status Page and Health Indicator
The status page and health indicators for a Eureka instance default to “/info” and “/health” respectively, which are the default locations of useful endpoints in a Spring Boot Actuator application. You need to change these, even for an Actuator application if you use a non-default context path or servlet path (e.g.
server.servletPath=/foo
) Example:官方文档中说,默认status 和 health 使用的 actuator 中的 info 和 health 接口,所以需要注意2点: 第一点:客户端需要引入 Actuator 才能够进行访问 第二点:默认 为 /info 何 /health 所以当 server.servletPaht 修改了,相应的配置也需要进行修改
案例如下
更多内容,查阅文档