Spring Cloud——服务发现与注册

版权声明:欢迎转载,转载请指明出处 https://blog.csdn.net/yjw123456/article/details/83050976

版本说明

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.16.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Edgware.SR4</spring-cloud.version>
	</properties>

服务发现

在计算机网络中,一种自动发现设备或服务的技术,通过服务发现协议(Service Discovery Protocol)实现。

常见协议

  • REST: HATEOSA
  • Web Services: UDDI(Universal Description Discovery and Integration)

服务注册

设备或服务主动/被动注册到管理中心,以便服务被发现和消费

常见注册中心

  • Zookeeper
  • Netflix Eureka
  • Consul

高可用

一种系统特性,致力于确保可接受程序的操作执行,通常采用上线时间作为基准。其中,以一年内的上线时间与自然时间的比率来描述可用性。

基本原则

  • 消灭单点故障
  • 可靠性交迭
  • 故障探测

服务发现:Eureka

是由Netflix公司发明的服务发现中间件,包括服务发现服务器和客户端。

核心组件

  • Eureka Server
  • Eureka Client

服务端:Eureka Server

是Eureka Client的注册服务中心,管理所有注册服务、以及其实例信息和状态。

运行Eureka Server

  • 依赖spring-cloud-starter-eureka-server
  • 激活:增加@EnableEurekaServer

创建服务端

创建的时候选择以下三个jar包

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>

在EurekaServerApplication添加@EnableEurekaServer

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

修改application.properties:

# 服务器应用名称
spring.application.name=spring-cloud-eureka-server

# 服务器服务端口
server.port=9090

# 管理端口安全关闭
management.security.enabled=false

然后启动,启动时成功了,但是会报错

[nfoReplicator-0] c.n.discovery.InstanceInfoReplicator     : There was a problem with the instance info replicator

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

问题原因:Eureka Server 既是注册服务器,也是客户端,默认情况,也需要配置注册中心地址。

##  取消向注册中心注册
eureka.client.register-with-eureka=false

## 取消向注册中心获取注册信息(服务、实例信息)
eureka.client.fetch-registry=false

客户端:Eureka Client

为当前服务提供注册、同步、查找服务以及其实例信息或状态等能力。

运行Eureka Client

  • 依赖 spring-cloud-starter-eureka
  • 激活@EnableEurekaClient@EnableDiscoveryClient

创建客户端

选择actuatorwebeureka discovery

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>

在EurekaClientApplication添加@EnableEurekaClient

@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {

同样,修改配置文件:

# 客户端应用名称
spring.application.name=spring-cloud-eureka-client

# 客户端服务端口
server.port=8080

# 管理端口安全关闭
management.security.enabled=false

然后启动,这里没有配置注册中心地址,也会报刚才和服务端一样的错误。

这里,我们配置一下,在上面的配置文件中添加

# 客户端注册到Eureka服务器
eureka.client.serviceUrl.defaultZone = http://localhost:9090/eureka

重启后,就不会有错误了

然后访问localhost:9090可以看到注册成功了

调整健康检查页面

Eureka Server会ping Eureka Client 的/health,看是否能ping通来检测客户端是否“健康”

# 调整状态页面
eureka.instance.status-page-url-path=/status

# 调整健康检查页面
eureka.instance.health-check-url-path=/health

我们把状态页面设为/status,同时新增自定义的StatusController:

package com.learn.eurekaclient.web.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 状态控制器
 */
@RestController
public class StatusController {
    @GetMapping("/status")
    public String status() {
        return "OK";
    }
}

访问localhost:9090会看到Status 的URL变了

Spring Cloud Config与Eureka整合

Config Server注册到Eureka

将Spring Cloud Config注册到Eureka上

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>

添加注解

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ConfigServerApplication.class, args);
	}
}

注册到Eureka服务器,增加如下配置

# 客户端注册到Eureka服务器
eureka.client.serviceUrl.defaultZone = http://localhost:8761/eureka

Config Client注册到Eureka

第一步 增加依赖

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>

第二步 增加注解

@Slf4j
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication {

第三步 增加配置

这里可以把以下配置增加到git仓库的config-client.yml 文件中,让它从配置服务器上去获取

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka

第四步 修改配置

对,没错,还是需要修改配置

spring:
  cloud:
    config:
      discovery:
        enabled: true #激活配置服务器 服务发现
        service-id: CONFIG-SERVER #配置服务器的应用名称
      profile: prod
  application:
    name: config-client

因为我们之前已经把配置服务器注册到了Eureka上了,
就可以通过激活配置服务器 服务发现来进行获取配置信息了。

只需要按照以上配置即可。

注意这里使用的Eureka服务器的默认端口(8761),如果Eureka服务器的端口不是这个默认端口,那么需要在上面的配置中显示地指定出来

项目地址

https://gitee.com/safika/springcloud-learn

猜你喜欢

转载自blog.csdn.net/yjw123456/article/details/83050976