SpringBoot + Spring Cloud Eureka 服务注册与发现
- 什么是Spring Cloud Eureka
Eureka是Netflix公司开发的开源服务注册发现组件,服务发现可以说是微服务开发的核心功能了,微服务部署后一定要有服务注册和发现的能力,Eureka就是担任这个角色。如果你用过Dubbo的话,Dubbo里服务注册和发现就是通过Zookeeper框架完成的。
Eureka 目前是2.2.x版本,目前官方已经宣布不再维护和更新了,不过Eureka 做注册中心已经在生产环境中大规模使用了,可以说很稳定了。从我个人的角度看,目前大家使用的更多的是阿里的Nacos和Consul 这两个组件实现了不止服务发现和注册,微服务开发不用再去依赖更多的组件和框架。这篇文章模拟一下Eureka Server集群和服务提供者集群和服务消费。
版本说明
父工程pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
服务中心注册
添加spring-cloud-starter-netflix-eureka-server依赖
springboot启动类添加注释@EnableEurekaServer
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
服务端yml配置
server:
port: 6868
eureka:
client:
register-with-eureka: false #是否将自己注册到eureka中
fetch-registry: false #是否从eureka中获取信息
service-url:
defaultZone: http://127.0.0.1:6868/eureka/
spring:
application:
name: tensquare_eureka
服务提供者
提供spring-cloud-starter-netflix-eureka-client依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
服务提供者yml配置
ureka:
client:
service-url:
defaultZone: http://127.0.0.1:6868/eureak/
instance:
prefer-ip-address: true
zuul yml配置
zuul:
routes:
tensquare-base: #基础
path: /base/** #配置请求URL的请求规则
serviceId: tensquare-base #指定Eureka注册中心中的服务id
strip-prefix: true
sentiviteHeaders:
customSensitiveHeaders: true
tensquare-article: #基础
path: /article/** #配置请求URL的请求规则
serviceId: tensquare-article #指定Eureka注册中心中的服务id
strip-prefix: true
sentiviteHeaders:
customSensitiveHeaders: true
注意如果是jdk11版本的需要加上下面的依赖
<!-- jdk11与springcloud需加上-->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.2</version>
</dependency>
版权声明:本文为qq_37575728原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。