Eureka的基本使用

Eureka是Netflix开发的服务发现框架,可以使用Eureka管理各种服务功能包括服务的注册、发现、熔断、负载、降级等。

Eureka包含两个组件:Eureka Server和Eureka Client。下图显示了Eureka Server与Eureka Client的关系:

 

其中,Service Provider是服务提供方,需要将自己注册到Eureka,从而使服务消费方能够找到,Service Consumer为服务消费方,从Eureka获取注册服务列表。

Eureka的使用:

EurekaServer

1.添加依赖:

<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.mc_74120</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eurekaserver7002</artifactId>
<dependencies>
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>com.mc_74120</groupId>
<artifactId>api_commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--boot web actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--一般通用配置-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
2.在启动类添加注解:
@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class springBootEureka7002 {
public static void main(String[] args) {
SpringApplication.run(springBootEureka7002.class,args);
}
}
3.在application.yml中进行参数配置
eureka:
instance:
hostname: eureka7002.com #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
#集群指向其它eureka
defaultZone: http://localhost:7001/eureka/
#单机就是7001自己
#defaultZone: http://eureka7001.com:7001/eureka/
#server:
#关闭自我保护机制,保证不可用服务被及时踢除
#enable-self-preservation: false
server:
port: 7002
Eureka Client
1.添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.在主启动类上添加注解:
@EnableEurekaClient或者@EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
public class SpringBootOrderApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootOrderApplication.class,args);
}
3.配置application.yml
spring:
application:
name: cloud-order-service
server:
port: 90
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true。
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
#单机版
#defaultZone: http://localhost:7001/eureka
# 集群版
defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka




 

 

 

 

 
posted @ 2020-09-08 17:36  第十八使徒  阅读(224)  评论(0)    收藏  举报