1、RPC远程调用框架核心设计思想在于注册中心,因为使用注册中心管理每个服务与服务之间的依赖关系(服务治理)
2、注册中心:存放服务地址相关信息(接口地址)
3、服务提供者:提供服务接口
4、服务消费者:调用别人接口,进行使用
5、服务注册:将信息注册到注册中心上
6、服务发现:从注册中心上获取服务信息
SpringCloud支持三种注册中心: Eureka、Consul、Zookeeper
Dubbo之心两种:Redis、Zookeeper
过程:
- 启动注册中心(Eureka注册中心)
- 启动会员服务
- 会员服务启动的时候,会把当前服务基本信息(服务地址和接口)以别名方式注册到注册中心上
- 消费者在调用接口的时候,使用服务别名,也就是ServiceId,去注册中心获取实际rpc远程调用地址
- 消费者获取到rpc地址,使用本地HttpClient技术实现调用
一、 Eureka
1、介绍
基于Netflix Eureka做的二次封装,由两个组件组成:
- Eureka Server 注册中心
- Eureka Client 服务注册
2、Eureka Server
编写Eureka服务
(1)pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
(2)application.yml
### 服务端口
server:
port: 8761
eureka:
instance:
### 注册中心IP地址
hostname: 127.0.0.1
client:
service-url:
### 注册地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
###不许奥去检索服务信息
fetch-registry: false
### 是否需要将自己注册给自己的注册中心(集群时需要true)
register-with-eureka: false
(3)启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {}
(4)启动页面
3、Eureka Client(服务提供者)
编写Eureka客户端
(1)pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency> <!-- 该starter在新版必须添加,否则报错 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
(2)application.yml
### 会员项目端口号
server:
port: 8100
### 服务注册别名---注册中心名称
spring:
application:
name: allen-member
eureka:
client:
### 将服务注册到eureka '
### 新版本SpringCloud不再需要@EnableEurekaClient注解就可以注册,如果不想注册,将值改为false
register-with-eureka: true
###需要检索服务信息
fetch-registry: true
service-url:
### 当前会员服务注册到eureka服务地址,如何是其他服务,需修改
defaultZone: http://localhost:8761/eureka/
(3)Ctroller
@RestController
public class MemberController {
@RequestMapping("/getMember")
public String getMember() {
return "获取会员信息";
}
}
(4)启动类
注:低版本需添加注解@EnableEurekaClient,新版本不用
@SpringBootApplication
public class MemberApplication {
public static void main(String[] args) {
SpringApplication.run(MemberApplication.class, args);
}
}
(5)启动页面
4、Eureka Client(服务消费者)
(1)pom.xml
同服务提供者
(2)application.yml
同服务提供者
(3)Controller
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class OrderController {
//rest方式调用
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/getOrder")
public String getOrder() {
//rest有两种方式调用:
// 1、直接调用
//String result = restTemplate.getForObject("http://localhost:8100/getMember", String.class);
// 2、采用服务别名方式调用, 添加@LoadBalanced注解,实现ribbon负载均衡
String url = "http://allen-member/getMember";
String result = restTemplate.getForObject(url, String.class);
System.err.println("订单请求:" + result);
return result;
}}
(4)启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
//向Springboot注入RestTemplate,否则Controller无法注入,报错
@Bean
@LoadBalanced //该注解能让RestTemplate在请求时拥有客户端负载均衡的能力,别名时,使用
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
注意:负载均衡Ribbon是不支持下划线的,只支持横线,所有起别名的时候,不能用下划线,正确示例:allen-member
5、集群
(1)原理图
(2)启动member项目,端口号分别为8100和8110
(3)Eureka效果
(4)实现效果
6、高可用
Eureka搭建集群原理:使用相互注册原理,形成一组相互注册的注册中心。从而实现数据相互的同步,达到高可用效果。
(1)yml
两个相同的eureka服务,端口分别为8761和8762
### 服务端口
server:
port: 8762
eureka:
instance:
### 注册中心IP地址
hostname: 127.0.0.1
client:
service-url:
### 注册地址, 端口是另一个server.port
defaultZone: http://${eureka.instance.hostname}:8761/eureka/
###不需要去检索服务信息
fetch-registry: true
### 是否需要将自己注册给自己的注册中心(集群时需要true)
register-with-eureka: true
spring:
application:
name: allen_eureka
(2) 访问 http://localhost:8761/ 和 http://localhost:8762/
(3)Eureka Client 配置文件
eureka:
client:
### 将服务注册到eureka
register-with-eureka: true
###需要检索服务信息
fetch-registry: true
service-url:
### 当前会员服务注册到eureka服务地址
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
注意:在注册过程中,只会保证有一台注册中心服务有对应的服务信息数据,当8761注册中心宕机后,启动转移同步数据到8762注册中心。