Eureka和Ribbon
- Eureka介绍
- 三大角色
- Eureka详解
- Eureka注册中心创建(springcloud-eureka模块)
- 服务注册-信息配置-自我保护机制(服务提供模块)
- CAP原则
- Ribbon:负载均衡及Ribbon
- 服务消费方(客户端)集成Ribbon
Eureka介绍
三大角色
Eureka Serrver:提供服务的注册与发现。zookeeper
Service Provider:将自身服务注册到 Eureka中,从而使消费方能够找到
Service Consumer:服务消费方从Eureka中获取注册服务列表,从而找到消费服务
Eureka详解
Eureka注册中心创建(springcloud-eureka模块)
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud-pom</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../springcloud-pom/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-eureka</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
- application.yml
server:
port: 8761
#Eureka配置
eureka:
instance:
hostname: localhost #Eureka服务端的实例名称
client:
register-with-eureka: false #表示是否向Eureka注册中心注册自己
fetch-registry: false #要不要去注册中心获取其他服务的地址
service-url: #监控页面
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
- 启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
- 大坑
Dependencies的sdk版本如果选择过高,springboot将无法启动
启动之后访问:http://localhost:8761/
服务注册-信息配置-自我保护机制(服务提供模块)
服务提供者注册到Eureka注册中心
- 加依赖
<!--完善监控信息-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
- Eureka配置
#Eureka配置,服务注册到哪里
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
instance-id: springcloud-server-provider #修改Eureka的默认描述信息
#info配置
info:
app.name: zzq-springcloud
company.name: blog.com
- 启动类加注解:@EnableEurekaClient
@EnableEurekaClient
@SpringBootApplication //在服务启动后自动注册到eureka
public class ServerProviderApplicaiton {
public static void main(String[] args) {
SpringApplication.run(ServerProviderApplicaiton.class,args);
}
}
CAP原则
C:一致性
A:可用性
P:容错性
Ribbon:负载均衡及Ribbon
Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具
Ribbon能干嘛
服务消费方(客户端)集成Ribbon
- pom.xml
<!--ribbon负载均衡-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--Eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
- application.yml
#Eureka配置
eureka:
client:
register-with-eureka: false #不向Eureka注册自己
service-url:
defaultZone: http://localhost:8761/eureka/ #客户端注册进Eureka
- configBean
@Configuration
public class ConfigBean {
//配置负载均衡实现RestTemplate
@Bean
@LoadBalanced //Ribbon配置
public RestTemplate geteRestTemplate(){
return new RestTemplate();
}
}
- controller
//Ribbon,我们这里的地址,应该是一个变量,通过服务名来访问 private static final String RESET_URL_PREFIX="http://SPRINGCLOUD-PROVIDER-DEPT";
@RestController
public class DeptConsumerController {
//理解:消费者,不应该有service层
@Autowired
private RestTemplate restTemplate; //提供多种便捷访问远程http服务的方法,简单的restFul服务模板
//Ribbon,我们这里的地址,应该是一个变量,通过服务名来访问
private static final String RESET_URL_PREFIX="http://SPRINGCLOUD-PROVIDER-DEPT";
@GetMapping("/consumer/dept/{deptNo}")
public Dept get(@PathVariable("deptNo") Long deptNo){
return restTemplate.getForObject(RESET_URL_PREFIX+"/dept/"+deptNo,Dept.class);
}
@GetMapping("/consumer/list")
public List<Dept> queryAll(){
return restTemplate.getForObject(RESET_URL_PREFIX+"/dept/queryAll",List.class);
}
@PostMapping("/consumer/dept/add")
public boolean add(@RequestBody Dept dept){
return restTemplate.postForObject(RESET_URL_PREFIX+"/dept/add",dept,Boolean.class);
}
}
Ribbon和Eureka整合以后,客户端可以直接调用,不用关心ip地址和端口号