SpringCloud系列文章列表
0. SpringCloud实战专栏介绍准备
1. SpringCloud父工程搭建
2. 服务注册中心之Eureka(单机+集群+Ribbon调用)
3. 服务注册中心之Zookeeper
4. 服务注册中心之Consul
5. eureka、zookeeper和consul三种注册中心之间的区别
6. 负载均衡服务调用之Ribbon
7. 服务调用之OpenFeign
8. Hystrix断路器全面实战总结
9. SpringCloud Gateway网关
10. SpringCloud Config配置中心
11. SpringCloud Bus消息总线
12. SpringCloud Stream消息驱动
13. SpringCloud Sleuth分布式请求链路追踪
先附上中文文档 https://www.bookstack.cn/read/spring-cloud-docs/docs-user-guide-eureka.md
简介
Eureka采用了CS的设计架构,Eureka Server作为服务注册功能的服务器,它是服务注册中心。而系统中的其他微服务,使用Eureka的客户端连接到Eureka Server并维持心跳连接。这样系统的维护人员就可以通过Eureka Server来监控系统中各个微服务是否正常运行。
在服务注册与发现中,有一个注册中心。 当服务器启动的时候,会把当前自己服务器的信息比如服务地址通讯地址等以别名方式注册到注册中心上。另-方(消费者|服务提供者),以该别名的方式去注册中心上获取到实际的服务通讯地址,然后再实现本地RPC调用RPC远程调用框架核心设计思想:在于注册中心,因为使用注册中心管理每个服务与服务之间的一个依赖关系(服务治理概念。在任何rpc远程框架中,都会有一个注册中心(存放服务地址相关信息(接口地址);
两大组件
Eureka Server 提供服务注册服务
各个微服务节点通过配置启动后,会在EurekaServer中进行注册, 这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观看到。
EurekaClient 通过注册中心进行访问
是一个Java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的、 使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。 如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,EurekaServer将会从服务注册表中把这个服务节点移除(默认90秒)
系统架构图
- Eureka Server
服务注册中心,一般为了高可用会搭建集群; - Service Provider
服务提供者,例如支付服务,注册(Register)到注册中心,可以进行服务续约(Renew)、服务下线(Cancel);高可用的话也会搭建集群; - Service Consumer
服务消费者,就是调用服务的,通过注册中心获取服务列表(Get Registry),然后进行远程服务调用(Remote Call);
单机版搭建
我们会建三个子工程,名字对应如下图小括号内的名字;
- 注册中心 cloud-eureka-server7001
pom.xml
<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>
</dependencies>
application.yml
server:
port: 7001
spring:
application:
name: eureka-server7001
eureka:
instance:
hostname: localhost
client:
#不注册自己
register-with-eureka: false
#不需要检索服务
fetch-registry: false
service-url:
defaultZone: http://localhost:7001/eureka/
启动类
@SpringBootApplication
@EnableEurekaServer //启动Eureka服务
public class EurekaServerMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaServerMain7001.class, args);
}
}
ok,注册中心搞定;
启动服务,访问http://localhost:7001/
- 服务提供者 cloud-provider-payment8001
pom.xml
<dependencies>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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>
</dependencies>
application.yml
server:
port: 8001
spring:
application:
name: provider-payment
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
#注册中心地址
defaultZone: http://localhost:7001/eureka
instance:
instance-id: payment8001 #注册到eureka的主机名称
启动类
@SpringBootApplication
@EnableEurekaClient //启动Eureka连接
public class ProviderPaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(ProviderPaymentMain8001.class, args);
}
}
写个Web接口,便于测试
@RestController
@RequestMapping("/payment")
public class PaymentController {
@Value("${server.port}")
private Integer port;
@GetMapping("/hello")
public String getPaymentById() {
return "hello chouxiaozi:"+port;
}
}
启动服务,查看注册中心页面,服务注册成功;
测试controller接口成功;
- 消费者 cloud-consumer-order80
通过Ribbon(理解成 负载均衡+RestTemplate调用)去远程调用服务
pom.xml
<dependencies>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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>
</dependencies>
application.yml
server:
port: 80
spring:
application:
name: consumer-order80
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
instance:
instance-id: consumer-order80
启动类
@SpringBootApplication
@EnableEurekaClient #这个别忘了
public class ConsumerOrderMain80 {
public static void main(String[] args) {
SpringApplication.run(ConsumerOrderMain80.class, args);
}
}
RestTemplate配置类
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
Controller
@RestController
@RequestMapping("/consumer/payment")
public class ConsumerPaymentController {
//直接写服务名称 PROVIDER-PAYMENT
private final String url = "http://PROVIDER-PAYMENT";
@Autowired
RestTemplate restTemplate;
@GetMapping("/hello")
public String getPaymentById() {
return restTemplate.getForObject(url+"/payment/hello",String.class);
}
}
启动服务,查看注册中心如下
测试接口成功
集群版搭建
在上面的基础上增加一个注册中心,一个服务提供者;
注册中心: cloud-eureka-server7001 和 cloud-eureka-server7002
服务提供者: cloud-provider-payment8001 和 cloud-provider-payment8002
消费者:cloud-consumer-order80
便于区分主机;修改hosts文件添加映射
在C:\Windows\System32\drivers\etc目录下,
添加
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
- 注册中心
将7001复制一份,得到cloud-eureka-server7002
(1)修改cloud-eureka-server7001
application.yml
server:
port: 7001
spring:
application:
name: eureka-server7001
eureka:
instance:
hostname: eureka7001.com
client:
#不注册自己
register-with-eureka: false
#不需要检索服务
fetch-registry: false
service-url:
#单机版
#defaultZone: http://localhost:7001/eureka/
#集群(配置其他注册中心地址中间逗号分隔)
defaultZone: http://eureka7002.com:7002/eureka/
(2) 修改cloud-eureka-server7002
application.yml
server:
port: 7002
spring:
application:
name: eureka-server7002
eureka:
instance:
hostname: eureka7002.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
各自启动,如下图即为成功;
- 服务提供者
复制cloud-provider-payment8001,得到cloud-provider-payment8002
(1)修改cloud-provider-payment8001
yml
server:
port: 8001
spring:
application:
name: provider-payment
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
#单机
#defaultZone: http://localhost:7001/eureka
defauleZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
instance:
instance-id: payment8001 #注册到eureka的主机名称
(2)修改cloud-provider-payment8002
server:
port: 8002
spring:
application:
name: provider-payment
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
#单机
#defaultZone: http://localhost:7001/eureka
#集群
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
instance:
instance-id: payment8002
分别启动查看注册中心
- 消费者
消费者代码不变;启动测试如下:
8001和8002服务交替调用,因为Ribbon负载均衡的默认规则就是轮询;
点赞+评论+关注
本文源码地址: https://gitee.com/shuaidawang/SpringCloudDemo.git
有错误的地方欢迎指正!可以加入qq交流群: 700637673