赞
踩
Eureka是NetFix公司开发的服务治理框架
Eureka两大组件
cloud-eureka-server7001
<dependencies> <!-- eureka-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <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.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2018以前的版本引入的eureka的maven
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
springcloudAlibab使用的maven
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名
client:
register-with-eureka: false #false表示不向注册中心注册自己
fetch-registry: false #false表示自己端就是注册中心,维护实例,不需要检索服务
service-url:
defaultZone: http://
e
u
r
e
k
a
.
i
n
s
t
a
n
c
e
.
h
o
s
t
n
a
m
e
:
{eureka.instance.hostname}:
eureka.instance.hostname:{server.port}/eureka/
@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}
pom
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
yml
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
主启动类上加
@EnableEurekaClient
http://localhost:7001/
cloud-consumer-order80
pom
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
yml
server:
port: 80
spring:
application:
name: cloud-order-service
eureka:
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://localhost:7001/eureka
主启动类上加上
@EnableEurekaClient
host文件 找到C:\Windows\System32\drivers\etc\下的host文件
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名
client:
register-with-eureka: false #false表示不向注册中心注册自己
fetch-registry: false #false表示自己端就是注册中心,维护实例,不需要检索服务
service-url:
defaultZone: http://eureka7002.com:7002/eureka/ #向eureka7002注册相互注册
http://eureka7001.com:7001/
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册消息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
#defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
负载均衡
在消费方的地址不能写死,否则多个实例就没有意义
使用服务名访问http://CLOUD-PAYMENT-SERVICE
使用@LoadBalanced
注解赋予RestTemplate负载均衡的能力
@Configuration
public class MyConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
此时消费者消费会8001和8002轮询的使用
actuator
微服务信息完善instance
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
instance:
instance-id: payment8002
prefer-ip-address: true
对于注册到eureka里面的微服务,可以通过服务发现来获得该服务的信息
@RestController @Slf4j public class DiscoverController { @Resource private DiscoveryClient discoveryClient; @GetMapping("/discover") public Object discovery() { List<String> services = discoveryClient.getServices(); for (String service : services) { log.info("*****element:" + service); } List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE"); for (ServiceInstance instance : instances) { log.info(instance.getServiceId()+"\t" + instance.getHost() + "\t" + instance.getPort() +"\t" + instance.getUri()); } return this.discoveryClient; }
主启动类
@EnableDiscoveryClient
http://localhost:8001/payment/discovery
一句话: 某时刻某一个微服务不可用了,Eureka不会立即清理,依旧会对该微服务的信息进行保存
属于CAP里面的AP分支
注册中心eurekaServer端7001
出厂默认,自我保护机制是开启的(eureka.server.enbale-self-preservation=true)
使用eureka.server.enbale-self-preservation=false可以禁止自我保护
生产者客户端eurekaClient端8001
Eureka服务端
eureka:
server:
#关闭自我保护模式,保证不可用服务被及时删除
enable-self-preservation: false
eviction-interval-timer-in-ms: 2000
服务消费者 Payment eureka: client: #表示是否将自己注册进EurekaServer默认为true register-with-eureka: true #是否从EurekaServer抓取已有的注册消息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡 fetch-registry: true service-url: #集群版 #defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/ #单机版 defaultZone: http://eureka7001.com:7001/eureka/ instance: instance-id: payment8001 prefer-ip-address: true #访问路径可以显示ip #Eureka客户端向服务端发送心跳的实际间隔,单位为秒(默认为30秒) lease-renewal-interval-in-seconds: 1 #Eureka服务端收到最后一次心跳后等待时间上线,单位为秒(默认为90秒) 超时将剔除服务 lease-expiration-duration-in-seconds: 2
先启动7001再启动8001,在关闭8001
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。