最简单的Spring Cloud教程第一篇:服务的注册与发现(Eureka)

网友投稿 222 2023-04-16


最简单的Spring Cloud教程第一篇:服务的注册与发现(Eureka)

前言

本文主要给大家介绍关于Spring Cloud服务注册与发现(Eureka)的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍:

一、spring cloud简介

spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑。另外说明spring cloud是基于springboot的,所以需要开发中对springboot有一定的了解,如果不了解的话可以看这篇文章。另外对于“微服务架构” 不了解的话,可以通过搜索引擎搜索“微服务架构”了解下。

二、创建服务注册中心

在这里,我们需要用的的组件上Spring Cloud Netflix的Eureka ,eureka是一个服务注册和发现模块。

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

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

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

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

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

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

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.forezp

eurekaserver

0.0.1-SNAPSHOT

jar

eurekaserver

Demo project for Spring Boot

org.springframework.boot

spring-boot-starter-parent

1.5.2.RELEASE

UTF-8

FKzzgmaBKUTF-8

1.8

org.springframework.cloud

spring-cloud-starter-eureka-server

org.springframework.boot

spring-boot-starter-test

test

org.springframework.clouFKzzgmaBKd

spring-cloud-dependencies

Dalston.RC1

pom

import

org.springframework.boot

spring-boot-maven-plugin

spring-milestones

Spring Milestones

https://repo.spring.io/milestone

false

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.forezp

eurekaserver

0.0.1-SNAPSHOT

jar

eurekaserver

Demo project for Spring Boot

org.springframework.boot

spring-boot-starter-parent

1.5.2.RELEASE

UTF-8

FKzzgmaBKUTF-8

1.8

org.springframework.cloud

spring-cloud-starter-eureka-server

org.springframework.boot

spring-boot-starter-test

test

org.springframework.clouFKzzgmaBKd

spring-cloud-dependencies

Dalston.RC1

pom

import

org.springframework.boot

spring-boot-maven-plugin

spring-milestones

Spring Milestones

https://repo.spring.io/milestone

false

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

@EnableEurekaServer

@SpringBootApplication

public class EurekaserverApplication {

public static void main(String[] args) {

SpringApplication.run(EurekaserverApplication.class, args);

}

}

**2.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/

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

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

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

No application available 没有服务被发现 ……^_^

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

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

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

创建过程同server类似,创建完pom.xml如下:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.forezp

service-hi

0.0.1-SNAPSHOT

jar

service-hi

Demo project for Spring Boot

org.springframework.boot

spring-boot-starter-parent

1.5.2.RELEASE

UTF-8

UTF-8

1.8

org.springframework.cloud

spring-cloud-starter-eureka

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.springframework.cloud

spring-cloud-dependencies

Dalston.RC1

pom

import

org.springframework.boot

spring-boot-maven-plugin

spring-milestones

Spring Milestones

https://repo.spring.io/milestone

false

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.forezp

service-hi

0.0.1-SNAPSHOT

jar

service-hi

Demo project for Spring Boot

org.springframework.boot

spring-boot-starter-parent

1.5.2.RELEASE

UTF-8

UTF-8

1.8

org.springframework.cloud

spring-cloud-starter-eureka

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.springframework.cloud

spring-cloud-dependencies

Dalston.RC1

pom

import

org.springframework.boot

spring-boot-maven-plugin

spring-milestones

Spring Milestones

https://repo.spring.io/milestone

false

通过注解@EnableEurekaClient 表明自己是一个eurekaclient.

@SpringBootApplication

@EnableEurekaClient

@RestController

public class ServiceHiApplication {

public static void main(String[] args) {

SpringApplication.run(ServiceHiApplication.class, args);

}

@Value("${server.port}")

String port;

@RequestMapping("/hi")

public String home(@RequestParam String name) {

return "hi "+name+",i am from port:" +port;

}

}

仅仅@EnableEurekaClient是不够的,还需要在配置文件中注明自己的服务注册中心的地址,application.yml配置文件如下:

eureka:

client:

serviceUrl:

defaultZone: http://localhost:8761/eureka/

server:

port: 8762

spring:

application:

name: service-hi

需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 。

启动工程,打开http://localhost:8761 ,即eureka server 的网址:

你会发现一个服务已经注册在服务中了,服务名为SERVICE-HI ,端口为7862

这时打开 http://localhost:8762/hi?name=forezp ,你会在浏览器上看到 :

hi forezp,i am from port:8762

源码下载:https://github.com/forezp/SpringCloudLearning/tree/master/chapter1

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对我们的支持。

参考资料

springcloud eureka server 官方文档

springcloud eureka client 官方文档


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java 正则,object中两个方法的使用(详解)
下一篇:关于Vue实现组件信息的缓存问题
相关文章

 发表评论

暂时没有评论,来抢沙发吧~