Chapter 1 快速搭建-服务的注册与发现(Eureka)

Chapter 1 快速搭建-服务的注册与发现(Eureka)

一、Spring Cloud简介

为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线)。分布式系统的协调导致了样板模式, 使用Spring Cloud开发人员可以快速地支持实现这些模式的服务和应用程序。他们将在任何分布式环境中运行良好,包括开发人员自己的笔记本电脑,裸机数据中心,以及Cloud Foundry等托管平台。

 

特性:

•       Spring Cloud专注于提供良好的开箱即用经验的典型用例和可扩展性机制覆盖。

•       分布式/版本化配置

•       服务注册和发现

•       路由

•       service - to - service调用

•       负载均衡

•       断路器

•       分布式消息传递

二、开发环境

• Jdk1.8

• Maven

• IntelliJ IDEA

三、环境搭建(服务注册中心)

3.1首先创建一个maven主工程。

3.2 然后创建2个model工程:一个model工程作为服务注册中心,即Eureka Server,另一个作为Eureka Client。

 

下面以创建server为例子,详细说明创建过程:

 

右键工程->创建model->选择spring initialir 如下图

Chapter 1 快速搭建-服务的注册与发现(Eureka)

下一步->选择clouddiscovery->eureka server ,然后一直下一步就行了。

Chapter 1 快速搭建-服务的注册与发现(Eureka)

创建完后的工程的pom.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <
modelVersion>4.0.0</modelVersion>

   <
groupId>com.harry</groupId>
   <
artifactId>springclouddemo</artifactId>
   <
version>0.0.1-SNAPSHOT</version>
   <
packaging>jar</packaging>

   <
name>springclouddemo</name>
   <
description>Demo project for Spring Boot</description>

   <
parent>
      <
groupId>org.springframework.boot</groupId>
      <
artifactId>spring-boot-starter-parent</artifactId>
      <
version>2.0.1.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>Finchley.M9</spring-cloud.version>
   </
properties>

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

      <
dependency>
         <
groupId>org.springframework.boot</groupId>
         <
artifactId>spring-boot-starter-test</artifactId>
         <
scope>test</scope>
      </
dependency>
   </
dependencies>

   <
dependencyManagement>
      <
dependencies>
         <
dependency>
            <
groupId>org.springframework.cloud</groupId>
            <
artifactId>spring-cloud-dependencies</artifactId>
            <
version>${spring-cloud.version}</version>
            <
type>pom</type>
            <
scope>import</scope>
         </
dependency>
      </
dependencies>
   </
dependencyManagement>

   <
build>
      <
plugins>
         <
plugin>
            <
groupId>org.springframework.boot</groupId>
            <
artifactId>spring-boot-maven-plugin</artifactId>
         </
plugin>
      </
plugins>
   </
build>

   <
repositories>
      <
repository>
         <
id>spring-milestones</id>
         <
name>Spring Milestones</name>
         <
url>https://repo.spring.io/milestone</url>
         <
snapshots>
            <
enabled>false</enabled>
         </
snapshots>
      </
repository>
   </
repositories>


</
project>

从pom文件中可以看到,我们使用的springboot 版本是<version>2.0.1.RELEASE</version>

Spring cloud 版本 <spring-cloud.version>Finchley.M9</spring-cloud.version>

注释:Finchley.M9

Spring Cloud Finchley 的第 9 个里程碑版 M9 已发布。需注意的是,自上个 M8 版本开始,项目已与 Spring Boot 2 兼容,不过不兼容 Spring Boot 1.x.y 。本次主要更新内容如下:

Spring Cloud Gateway

·       Support Rolling Deployments

·       Refresh routes from service discovery heart beat

Spring Cloud Netflix

·       Upgraded to Eureka 1.8.7 to address thread pool issue whenEureka health check is enabled

Spring Cloud Sleuth

·       Bug Fixes

·       Further Alignment With Brave

·       Fixed interop with Spring Cloud Gateway

Spring Cloud Contract

·       Added byte array for DSL

·       Better support for RestDocs parametrized names

·       Added exposure of multiple versions of the same artifact in the sametest

·       Added verbose messages for collection assertions in the generatedtests

·       Allows to keep unpacked stubs after the stubs got downloaded andtest finished

·       Added new overview sections to documentation

Spring Cloud Commons

·       Refactored APIs related to configuring Spring Retry

Spring Cloud Config

·       Adds ability to remove untracked local branches

·       Support YAML configuration for composite repos

Spring Cloud Stream

·       See Spring Cloud Stream release notes

 

3.3 启动一个服务注册中心,只需要一个注解@EnableEurekaServer,这个注解需要在springboot工程的启动application类上加:


@EnableEurekaServer
@SpringBootApplication
public class SpringclouddemoApplication {

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

 

3.4 eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成),在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。eureka server的配置文件appication.yml

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

 

注释:在项目构建的过程中application.properties是默认生成的配置文件,我们可以使用appication.yml 更有层次感。

通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server.

 

3.5 eureka server 是有界面的,启动工程,打开浏览器访问:

http://localhost:8761 ,界面如下:

Chapter 1 快速搭建-服务的注册与发现(Eureka)

No applicationavailable 没有服务被发现 ,因为没有注册服务当然不可能有服务被发现了。

四、创建一个服务提供者 (eureka client)

当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。

创建过程同server类似,通过注解@EnableEurekaClient表明自己是一个eurekaclient.

eureka client配置文件appication.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8762
spring:
  application:
    name: springcloudclient

启动项目:刷新http://localhost:8761如图可以看到:

Chapter 1 快速搭建-服务的注册与发现(Eureka)

你会发现一个服务已经注册在服务中了,服务名为springcloudclient,端口为8762.

EMERGENCY! EUREKA MAY BEINCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSERTHAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.

你可以看到上面有一段红字,Eureka的自我保护模式

如果在Eureka Server的首页看到以下这段提示,则说明Eureka已经进入了保护模式。

保护模式主要用于一组客户端和Eureka Server之间存在网络分区场景下的保护。一旦进入保护模式,Eureka Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)。

注释:Spring Cloud中,Eureka常见问题总结 看文:http://www.itmuch.com/spring-cloud-sum-eureka/

 

我们在springcloudclient 中创建一个demo,使用restful风格:

@RestController
public class HelloController {

    @Value("${server.port}")
    String port;
    @RequestMapping("/hello")
    @ResponseBody
    public String hello(@RequestParam String name) {
        return "hello "+name+",i am from port:" +port;
    }
}

 

界面如下

Chapter 1 快速搭建-服务的注册与发现(Eureka)

 

此文章主要是自己用于学习,做的笔记,借鉴https://blog.****.net/forezp/article/details/69696915