写于:2018-12-18 08:52:37

# Introduce

官方文档介绍

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 能够实现服务间的注册发现。

# Getting Started

项目结构如下:

# 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.properteisapplication.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 修改了,相应的配置也需要进行修改

案例如下

更多内容,查阅文档

精彩内容推送,请关注公众号!
最近更新时间: 4/13/2020, 9:09:21 PM