SpringCloud学习点滴——euraka实践

eureka是spring cloud的服务注册组件,在微服务中扮演重要角色,他有三个组成部分,也是其三个核心要素

1、服务注册中心:提供服务注册与发现的功能

2、服务提供者:提供服务的应用,它将自己注册在eureka上,供其他应用发现。

3、服务消费者:消费服务提供者提供的服务,通过服务注册中心去获取服务。

 

接下来是eureka入门服务搭建,我们像建立一个spring boot那样建一个eureka-service的服务,下面是相关依赖

<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>eurekaDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-service</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

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

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

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

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</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>

</project>

然后在启动类加上@EnableEurekaServer注解,标明这是一个eureka服务中心

然后启动服务发现以下错误

org.springframework.boot.builder.SpringApplicationBuilder.<init>([Ljava/lang/Object;)V

这个错误的原因是spring boot的版本和spring cloud 的版本不兼容导致的,我们将boot的版本改成1.5.7

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

然后再次启动,发现EurekaServer这个注解错误,原因是找不到,其实这里还是版本不匹配问题

下面附一张两者的版本匹配表

Spring CloudSpring Boot
Finchley 兼容Spring Boot 2.0.x,不兼容Spring Boot 1.5.x
Dalston和Edgware 兼容Spring Boot 1.5.x,不兼容Spring Boot 2.0.x
Camden 兼容Spring Boot 1.4.x,也兼容Spring Boot 1.5.x
Brixton 兼容Spring Boot 1.3.x,也兼容Spring Boot 1.4.x
Angel 兼容Spring Boot 1.2.x

所以我们将依赖的版本都改好,访问http://localhost:1111/,会看到这个界面,表示服务开启成功

 

 

注意:yml配置文件,:号后面要有空格!!!!

 

关于serviceUrl和service-url的区别

https://blog.csdn.net/u011531425/article/details/81713441

posted @ 2018-12-19 16:45  duduInTheHeart  阅读(821)  评论(0)    收藏  举报