springCloud-eureka
1) eureka是什么?
eureka是Netflix公司开源的一个**服务注册与发现**的组件.
eureka有eureka server(服务端)和eureka client(客户端):
客户端可以向服务端注册服务也可以获取其他客户端向服务端注册的服务
2) 快速入门
- 编写eureka-server端
① 导入eureka-server起步依赖
<!--引入Spring Cloud 依赖-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</dependencyManagement>
② 编写application.yml配置文件
server:
port: 8761
eureka:
instance:
hostname: localhost # 主机名
client:
service-url:
# eureka服务端地址,将来客户端使用该地址和eureka进行通信
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
# 是否将自己的路径 注册到eureka上。eureka server 不需要的,eureka provider client 需要
register-with-eureka: false
# 是否需要从eureka中抓取路径。eureka server 不需要的,eureka consumer client 需要
fetch-registry: false
③ 在引导类上加上@EnableEurekaServer注解
@SpringBootApplication
// 启用EurekaServer
@EnableEurekaServer
public class EurekaApp {
public static void main(String[] args) {
SpringApplication.run(EurekaApp.class,args);
}
}
⑤ 在浏览器中访问:http://localhost:8761/
- 编写eureka-client端
① 导入eureka-client起步依赖
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
② 编写application.yml相关配置
server:
port: 8001
eureka:
instance:
# 主机名
hostname: localhost
client:
service-url:
# eureka服务端地址,将来客户端使用该地址和eureka进行通信
defaultZone: http://localhost:8761/eureka
fetch-registry: true
register-with-eureka: true
spring:
application:
# 设置当前应用的名称。将来会在eureka中Application显示。将来需要使用该名称来获取路径
name: eureka-provider
③ 在引导类上加上@EnableEurekaClient注解
@EnableEurekaClient //该注解 在新版本中可以省略
@SpringBootApplication
public class ProviderApp {
public static void main(String[] args) {
SpringApplication.run(ProviderApp.class,args);
}
}
④ 使用discoveryClient动态从注册中心获取服务相应实例
/**
* 服务的调用方
*/
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/goods/{id}")
public Goods findGoodsById(@PathVariable("id") int id){
System.out.println("findGoodsById..."+id);
/*
动态从Eureka Server 中获取 provider 的 ip 和端口
1. 注入 DiscoveryClient 对象.激活
2. 调用方法, 获取提供方服务实例
3. 获取ip及端口
4. 拼接url地址
5. 使用restTemplate远程调用
*/
//演示discoveryClient 使用, -EUREKA-PROVIDER-为服务的名字
List<ServiceInstance> instances = discoveryClient.getInstances("EUREKA-PROVIDER");
//判断集合是否有数据
if(instances == null || instances.size() == 0){
//集合没有数据
return null;
}
ServiceInstance instance = instances.get(0);
String host = instance.getHost();//获取ip
int port = instance.getPort();//获取端口
System.out.println(host);
System.out.println(port);
String url = "http://"+host+":"+port+"/goods/findOne/"+id;
// 3. 使用restTemplate远程调用方法
Goods goods = restTemplate.getForObject(url, Goods.class);
return goods;
}
}
3) eureka属性
- -instance-相关属性
eureka:
instance:
# 主机名
hostname: localhost
# 将当前实例的ip注册到eureka server 中。默认是false 注册主机名
prefer-ip-address: true
# 设置当前实例的ip
ip-address: 127.0.0.1
# 设置web控制台显示的 实例id
instance-id: ${eureka.instance.ip-address}:${spring.application.name}:${server.port}
# 每隔3 秒发一次心跳包
lease-renewal-interval-in-seconds: 3
# 如果9秒没有发心跳包,服务器呀,你把我干掉吧~
lease-expiration-duration-in-seconds: 9
client:
service-url:
# eureka服务端地址,将来客户端使用该地址和eureka进行通信
defaultZone: http://localhost:8761/eureka
- -server-相关属性
eureka:
instance:
# 主机名
hostname: localhost
client:
service-url:
# eureka服务端地址,将来客户端使用该地址和eureka进行通信
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
# 是否将自己的路径 注册到eureka上。eureka server 不需要的,eureka provider client 需要
register-with-eureka: false
# 是否需要从eureka中抓取路径。eureka server 不需要的,eureka consumer client 需要
fetch-registry: false
server:
# 关闭自我保护机制 true是开启(默认,线上环境) false是关闭(开发环境)
enable-self-preservation: true
# 检查服务的时间间隔
eviction-interval-timer-in-ms: 3000
4) eureka高可用
- 搭建两个服务端eureka-server-1和eureka-server-2, 让他们相互注册
(1) eureka-server-1的application.yml
server:
port: 8761
eureka:
instance:
hostname: eureka-server1
client:
service-url:
defaultZone: http://eureka-server2:8762/eureka
register-with-eureka: true
fetch-registry: true
spring:
application:
name: eureka-server-ha
(2) eureka-server-2的application.yml
server:
port: 8762
eureka:
instance:
hostname: eureka-server2
client:
service-url:
defaultZone: http://eureka-server1:8761/eureka
register-with-eureka: true
fetch-registry: true
spring:
application:
name: eureka-server-ha
说明 : 其中1和2中defaultZone: eureka-server1, eureka-server2是配置了本地hosts
(3) eureka客户端测试
server:
port: 8001
eureka:
instance:
hostname: localhost
prefer-ip-address: true
ip-address: 127.0.0.1
instance-id: ${eureka.instance.ip-address}:${spring.application.name}:${server.port}
lease-renewal-interval-in-seconds: 3
lease-expiration-duration-in-seconds: 9
client:
service-url:
defaultZone: http://eureka-server1:8761/eureka,http://eureka-server2:8762/eureka
spring:
application:
name: eureka-provider