EureKa服务治理学习笔记(摘抄)

以下内容部分来源于网络,个人笔记用于学习使用。

1.简介

EureKa在Spring Cloud全家桶中担任着服务的注册与发现的落地实现。Netflix在设计EureKa时遵循着AP原则,它基于R 
EST的服务,用于定位服务,以实现云端中间层服务发现和故障转移,功能类似于Dubbo的注册中心Zookeeper。

2.实现原理

EureKa采用C-S的设计架构,即包括了Eureka Server(服务端),EureKa client(客户端)。
1.EureKa Server 提供服务注册,各个节点启动后,在EureKa server中进行注册;

2 EureKa Client 是一个Java客户端,用于和服务端进行交互,同时客户端也是一个内置的默认使用轮询负载均衡算法的负载均衡器。在应用启动后,会向Eueka Server发送心跳(默认30秒)。如果EUR额卡 Server在多个心跳周期内没有接受到某个节点的心跳,EureKa Server将会从服务注册表中将这个服务移出(默认90秒)。

3 Eureka是Spring Cloud Netflix微服务套件中的一部分,可以与Springboot构建的微服务很容易的整合起来。
Eureka包含了服务器端和客户端组件。服务器端,也被称作是服务注册中心,用于提供服务的注册与发现。Eureka支持高可用的配置,当集群中有分片出现故障时,Eureka就会转入自动保护模式,它允许分片故障期间继续提供服务的发现和注册,当故障分片恢复正常时,集群中其他分片会把他们的状态再次同步回来。
客户端组件包含服务消费者与服务生产者。在应用程序运行时,Eureka客户端向注册中心注册自身提供的服务并周期性的发送心跳来更新它的服务租约。同时也可以从服务端查询当前注册的服务信息并把他们缓存到本地并周期性的刷新服务状态。

关系调用说明:

  • 服务生产者启动时,向服务注册中心注册自己提供的服务
  • 服务消费者启动时,在服务注册中心订阅自己所需要的服务
  • 注册中心返回服务提供者的地址信息个消费者
  • 消费者从提供者中调用服务

3.简单实例

  ①注册中心

       配置文件:

在默认情况下,服务注册中心也会把自己当做是一个服务,将自己注册进服务注册中心,所以我们可以通过配置来禁用他的客户端注册行为,在application.properties中添加如下配置:

#设置tomcat服务端口号
server.port=1111
#设置服务名称
spring.application.name=eureka-service

eureka.instance.hostname=localhost
#注册中心不需要注册自己
eureka.client.register-with-eureka=false
#注册中心不需要去发现服务
eureka.client.fetch-registry=false
#设置服务注册中心的URL
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka


主程序类:
在Springboot项目中的main入口,添加@EnableEurekaServer注解,来开启服务注册中心.
在主类上添加@EnableEurekaClient注解以实现Eureka中的DiscoveryClient实现。

@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApp {
//@Bean 应用在方法上,用来将方法返回值设为为bean
@Bean
@LoadBalanced //@LoadBalanced实现负载均衡
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class, args);
}
}
/*
这里也要用到@EnableDiscoveryClient, 让服务使用eureka服务器, 实现服务注册和发现*/

启动应用,并访问http://localhost:3333/即可看到Eureka信息面板
②服务端:

配置文件:

server.port=9090
#设置服务名
spring.application.name=hello-service
#设置服务注册中心的URL,本服务要向该服务注册中心注册自己
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka

主程序:
/***
* @EnableDiscoveryClient
* 让服务使用eureka服务器
* 实现服务注册和发现
*/
@EnableDiscoveryClient
@SpringBootApplication
public class HelloApp {
public static void main(String[] args) {
SpringApplication.run (HelloApp.class, args);
}
}
controller类:
org.springframework.cloud.client.discovery.DiscoveryClient;对象注入,并且在日志中打印出与服务相关的一些信息。
@RestController
public class HelloController {
Logger logger = LoggerFactory.getLogger (HelloController.class);

@Autowired
DiscoveryClient discoveryClient;

@RequestMapping("/hello")
public String hello() {
ServiceInstance instance = discoveryClient.getLocalServiceInstance ();
//打印服务的服务id
logger.info ("*********" + instance.getServiceId ());
return "hello,this is hello-service";
}
}



③消费端
配置文件
server.port=9999
spring.application.name=hello-consumer
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka
#这里的配置项目和服务提供者hello-service一样

主程序类
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApp {
//@Bean 应用在方法上,用来将方法返回值设为为bean
@Bean
@LoadBalanced //@LoadBalanced实现负载均衡
public RestTemplate restTemplate() {
return new RestTemplate();
}

public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class, args);
}
}
/*
这里也要用到@EnableDiscoveryClient, 让服务使用eureka服务器, 实现服务注册和发现*/
controller类:
@RestController
public class ConsumerController {

//这里注入的restTemplate就是在com.sam.ConsumerApp中通过@Bean配置的实例
@Autowired
RestTemplate restTemplate;

@RequestMapping("/hello-consumer")
public String helloConsumer() {
//调用hello-service服务,注意这里用的是服务名,而不是具体的ip+port
restTemplate.getForObject("http://hello-service/hello", String.class);
return "hello consumer finish !!!";
}
}

测试: 访问http://localhost:8088/hello可以在控制台中看到日志信息

猜你喜欢

转载自www.cnblogs.com/coloz/p/10373677.html