一、 什么是服务注册中心
服务注册中心是服务实现服务化管理的核心组件,类似于目录服务的作用,主要用来存储服务信息,譬如提供者url串、路由信息等。服务注册中心是SOA架构中最基础的设施之一。
1 服务注册中心的作用
1,服务的注册
2,服务的发现
2. 常见的注册中心有哪些
1,Dubbo 的注册中心Zookeeper
2,Sringcloud的注册中心Eureka
3. 服务注册中心解决了什么问题
- 服务管理;
- 服务的依赖关系管理;
4. 什么是Eureka注册中心
Eureka是Netflix开发的服务发现组件,本身是一个基于REST的服务。Spring Cloud将它集成在其子项目spring-cloud-netflix中,以实现Spring Cloud的服务注册于发现,同时还提供了负载均衡、故障转移等能力。
5. Eureka注册中心三种角色
5.1 Eureka Server
通过Register、Get、Renew等接口提供服务的注册和发现。
5.2 Application Service (Service Provider)
服务提供方
把自身的服务实例注册到Eureka Server中
5.3 Application Client (Service Consumer)
服务调用方
通过Eureka Server 获取服务列表,消费服务。
二、 在Eureka注册中心中构建provider服务
1、创建项目
2、修改pom.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.1.9.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.angei</groupId> 12 <artifactId>eurekaserver</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>eurekaserver</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 <spring-cloud.version>Greenwich.SR3</spring-cloud.version> 20 </properties> 21 22 <dependencies> 23 <dependency> 24 <groupId>org.springframework.cloud</groupId> 25 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 26 </dependency> 27 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-test</artifactId> 31 <scope>test</scope> 32 </dependency> 33 </dependencies> 34 35 <dependencyManagement> 36 <dependencies> 37 <dependency> 38 <groupId>org.springframework.cloud</groupId> 39 <artifactId>spring-cloud-dependencies</artifactId> 40 <version>${spring-cloud.version}</version> 41 <type>pom</type> 42 <scope>import</scope> 43 </dependency> 44 </dependencies> 45 </dependencyManagement> 46 47 <build> 48 <plugins> 49 <plugin> 50 <groupId>org.springframework.boot</groupId> 51 <artifactId>spring-boot-maven-plugin</artifactId> 52 </plugin> 53 </plugins> 54 </build> 55 56 </project>
提示:如果IDEA加载pom.xml时一直下载失败,可以在pom.xml中添加如下配置,使其从国内阿里云镜像中下载相关内容,下载速率将会大幅提升。
<repositories> <repository> <id>aliyun</id> <name>aliyun</name> <url>https://maven.aliyun.com/repository/public</url> </repository> </repositories>
3、修改application.properties全局配置文件
1 server: 2 port: 8761 3 eureka: 4 instance: 5 appname: provider-service 6 hostname: localhost 7 client: 8 service-url: 9 defaultZone: 10 http://localhost:8761/eureka/ 11 register-with-eureka: false 12 fetch-registry: false
说明:
1 server: 2 port: 8761 3 eureka: 4 instance: 5 #服务名,默认取 spring.application.name 配置值,如果没有则为 unknown 6 appname: provider-service 7 #设置当前实例的主机名称 8 hostname: localhost 9 client: 10 service-url: 11 #指定服务注册中心地址,类型为 HashMap,并设置有一组默认值, 12 #默认的Key为 defaultZone;默认的Value为 http://localhost:8761/eureka , 13 #如果服务注册中心为高可用集群时,多个注册中心地址以逗号分隔。 14 defaultZone: 15 http://localhost:8761/eureka/ 16 17 #是否将自己注册到Eureka-Server中,默认的为true 18 register-with-eureka: false 19 20 #是否冲Eureka-Server中获取服务注册信息,默认为true 21 fetch-registry: false
4、修改启动类
1 import org.springframework.boot.SpringApplication; 2 import org.springframework.boot.autoconfigure.SpringBootApplication; 3 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 4 5 @SpringBootApplication 6 @EnableEurekaServer 7 public class EurekaserverApplication { 8 9 public static void main(String[] args) { 10 SpringApplication.run(EurekaserverApplication.class, args); 11 } 12 13 }
5、通过浏览器访问Eureka-Server服务管理平台