SpringCloud服务的注册与发现(一)
一、什么是服务注册与发现
Spring Cloud Eureka 模块提供的功能是被动式的服务发现。
服务注册:每个用户去聊天室服务器上注册。
服务发现:这样他的好友们就能看到你,你同时也将获取好友的上线列表.
微服务中,服务就相当于聊天室的用户,而服务注册中心就像聊天室服务器一样。
目前服务发现的解决方案有Eureka,Consul,Zookeeper等等。SpringCloud默认使用eureka作为服务注册中心。
二、Eureka使用过程
简单一点就是:
上图简要描述了Eureka的基本架构,由3个角色组成
1、Eureka Server
提供服务注册和发现
2、Service Provider
服务提供方
将自身服务注册到Eureka,从而使服务消费方能够找到
3、Service Consumer
服务消费方
从Eureka获取注册服务列表,从而能够消费服务
二、创建服务注册中心
在这里,我还是采用Eureka作为服务注册与发现的组件,至于Consul 之后会出文章详细介绍。
2.1 首先创建一个maven主工程。
首先创建一个主Maven工程,在其pom文件引入依赖,spring Boot版本为2.0.3.RELEASE,Spring Cloud版本为Finchley.RELEASE。这个pom文件作为父pom文件,起到依赖版本控制的作用,其他module工程继承该pom。这一系列文章全部采用这种模式,其他文章的pom跟这个pom一样。再次说明一下,以后不再重复引入。代码如下:
4.0.0 com.forezp spring-cloud 0.0.1-SNAPSHOT pom spring-cloud Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE eureka-server service-hi UTF-8 UTF-8 1.8 Finchley.SR1 org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
**2.2 然后创建2个model工程:**一个model工程作为服务注册中心,即Eureka Server,另一个作为Eureka Client。
下面以创建server为例子,详细说明创建过程:
右键工程->创建model-> 选择spring initialir 如下图:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mpMBIPP9-1651021737082)(http://upload-images.jianshu.io/upload_images/2279594-de33b84a79858106.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)]
下一步->选择cloud discovery->eureka server ,然后一直下一步就行了。
创建完后的工程,其pom.xml继承了父pom文件,并引入spring-cloud-starter-netflix-eureka-server的依赖,代码如下:
4.0.0 com.forezp eureka-server 0.0.1-SNAPSHOT jar eureka-server Demo project for Spring Boot com.forezp spring-cloud 0.0.1-SNAPSHOT UTF-8 UTF-8 1.8 Finchley.SR1 org.springframework.cloud spring-cloud-starter-netflix-eureka-server
2.3 启动一个服务注册中心,只需要一个注解@EnableEurekaServer,这个注解需要在springboot工程的启动application类上加:
package com.forezp.eurekaserver;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}
}
**2.4 **eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成),在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。eureka server的配置文件application.properties:
## 该注册服务器的端口
server.port=8761## 应用启动名称
spring.application.name=eureka-server## eureka配置
## 注册服务器的名称
eureka.instance.hostname=localhost
## 在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为
## 是否从eureka服务器获取注册信息,所以这里是false
eureka.client.fetch-registry=false
## 是否注册自身到eureka服务器,因为当前这个应用就是eureka服务器,没必要注册自身,所以这里是false
eureka.client.register-with-eureka=false
## 服务发布的地址
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server.
2.5eureka server 是有界面的,启动工程,打开浏览器访问:
http://localhost:8761,界面如下:
No application available 没有服务被发现 ……_
因为没有注册服务当然不可能有服务被发现了。
三、创建一个服务提供者 (eureka client)
当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。
创建过程同server类似,创建完pom.xml如下:
4.0.0 com.forezp service-hi 0.0.1-SNAPSHOT jar service-hi Demo project for Spring Boot com.forezp spring-cloud 0.0.1-SNAPSHOT org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-maven-plugin
通过注解@EnableEurekaClient 表明自己是一个eurekaclient.
package com.forezp.servicehi;import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@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(value = "name", defaultValue = "forezp") String name) {return "hi " + name + " ,i am from port:" + port;}
}
仅仅@EnableEurekaClient是不够的,还需要在配置文件中注明自己的服务注册中心的地址,application.properties配置文件如下:
## 该服务发布的端口
server.port=8762## 注意这里是作为服务注册名称,在eureka注册中心注册这个名称,自动转化为全大写,之后调用服务用服务名调用
spring.application.name=service-hi
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
## 注册服务中心的配置
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 。
启动工程,打开http://localhost:8761,即eureka server 的网址:
你会发现一个服务已经注册在服务中了,服务名为SERVICE-HI ,端口为8762
这时打开http://localhost:8762/hi?name=forezp,你会在浏览器上看到 :
四、参考资料
原文链接:https://blog.csdn.net/forezp/article/details/81040925
原文链接:https://blog.csdn.net/saytime/article/details/80570279
http://blog.csdn.net/forezp/article/details/69696915
http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html
)
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!