Eureka服务注册、发现、集群配置
概念理解:
服务治理:管理服务之间依赖关系,实现服务注册、调用、负载均衡、降级容错等。
eureka就处在服务注册这个环节。
服务注册系统架构
eureka介绍
eureka2.0是spring-cloud的服务注册中心模块,实现服务注册、发现,与其他高级组件一块实现服务熔断、负载、降级等。
eureka由两个组件构成:eureka-server 和eureka-client。
eureka-server 是eureka注册服务端,管理各个微服务在eureka上的注册信息。
eureka-client 是服务注册工具,方便微服务到eureka服务上注册。
eureka工作流程
eureka服务启动后被动接受各个微服务注册信息,每个微服务的服务名、ip:port信息以key value形式存储起来。消费端拿着key到eureka服务获取到实际微服务ip:port地址后就可以访问了。若有多个微服务,eureka根据轮询负载均衡器(ribbon等)提供服务。
实现
一、eureka服务配置(集群,7002端口的服务配置也一样)
1.新建springboot项目我用的2.2.2版本,pom引入cloud的eureka依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.修改yml文件,配置eureka实例
spring:
application:
name: cloud-eureka7001
server:
port: 7001
eureka:
instance:
#eureka实例名,给客户端使用的,要修改etc/host文件,映射127.0.01
hostname: eureka.com7001
client:
#是否把自己注册在eurka里
register-with-eureka: false
#是否抓取eureka中的注册服务信息
fetch-registry: false
#eureka服务地址
service-url:
defaultZone: http://eureka.com7002:7002/eureka/
server:
enable-self-preservation: true #是否开启保护模式
eviction-interval-timer-in-ms: 90000 #检测心跳频率
eureka.com7001域名的host配置 文件地址 C:\Windows\System32\drivers\etc\hosts 或linux:etc\hosts
3.主启动类配置@EnableEurekaServer注解
二、实现服务提供者(集群,8002端口的服务配置也一样)注册进eureka,注册的实例名可供消费者调用。这样消费者就无需关注具体服务者ip端口。
1.同理,springboot建微服务端项目,pom引入客户端依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.修改yml文件,配置eureka实例
spring:
application:
name: cloud-payment-service
server:
port: 8001
eureka:
client:
#是否把自己注册在eurka里
register-with-eureka: true
#是否抓取eureka中的注册服务信息
fetch-registry: true
#eureka服务地址
service-url:
defaultZone: http://eureka.com7001:7001/eureka/,http://eureka.com7002:7002/eureka/
instance:
instance-id: payment8001 #暴露名称
prefer-ip-address: true #显示ip
#Eureka客户端向服务端发送心跳的实际间隔,单位为秒(默认为30秒)
lease-renewal-interval-in-seconds: 30
#Eureka服务端收到最后一次心跳后等待时间上线,单位为秒(默认为90秒) 超时将剔除服务
lease-expiration-duration-in-seconds: 90
3.业务逻辑随便搞个controller
package com.han.cloud.controller;
import com.han.common.entities.CommonResult;
import com.han.common.entities.Payment;
import com.han.cloud.service.PaymentService;
import com.netflix.discovery.DiscoveryManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* 模块:
* PaymentController
* han
* 2020/7/18
*/
@Controller
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@Value("${server.port}")
private String serverport;
@Resource
private DiscoveryClient discoveryClient;
//@PostMapping("/payment/create")
@ResponseBody
@RequestMapping(value="/payment/create",method = RequestMethod.POST)
public CommonResult create(@RequestBody Payment payment){
log.info("创建Payment");
int result = paymentService.create(payment);
if(result>0) {
return new CommonResult(200, "插入成功serverport="+serverport, result);
}else{
return new CommonResult(400, "插入失败serverport="+serverport);
}
}
//@GetMapping("/payment/getPaymentId/{id}")
@ResponseBody
@RequestMapping(value="/payment/getPaymentId/{id}",method = RequestMethod.GET)
public CommonResult<Payment> getPaymentId(@PathVariable Long id){
log.info("获取Payment");
Payment result = paymentService.getPaymentById(id);
if(result!=null) {
return new CommonResult(200, "获取成功serverport="+serverport, result);
}else{
return new CommonResult(400, "获取失败serverport="+serverport);
}
}
@ResponseBody
@GetMapping("/payment/getdiscovery")
public CommonResult discovery(){
log.info(discoveryClient.getServices().toString());
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance ins:instances) {
log.info(ins.getServiceId()+"\t"+ins.getHost()+"\t"+ins.getPort());
log.info(ins.getUri().toString());
}
return new CommonResult(200,"获取到服务注册信息",discoveryClient);
}
}
2.主启动类配置@EnableEurekaClient注解
三、eureka消费端配置,配合RestTemplate(ribbon)实现负载均衡
1.消费端建接口、RestTemplate Bean配置
package com.han.cloud.controller;
import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.han.common.entities.CommonResult;
import com.han.common.entities.Payment;
import com.han.common.utils.HttpEntityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 模块:
* ConsumerController
* han
* 2020/7/18
*/
@RestController()
@Slf4j
public class ConsumerController {
@Resource
RestTemplate restTemplate;
private final String url="http://CLOUD-PAYMENT-SERVICE";
@RequestMapping(value = "/consumer/payment/create",method = RequestMethod.POST)
public CommonResult create( Payment payment){
log.info("调用创建Payment接口");
return restTemplate.postForObject(url + "/payment/create", HttpEntityUtil.httpJsonEntity(payment), CommonResult.class);
}
@RequestMapping(value="/consumer/payment/get/{id}",method = RequestMethod.GET)
public CommonResult<Payment> get(@PathVariable Long id){
return restTemplate.getForObject(url+"/payment/getPaymentId/"+id,CommonResult.class);
}
@RequestMapping(value="/consumer/payment/getdiscovery",method = RequestMethod.GET)
public CommonResult getDiscovery(){
return restTemplate.getForObject(url+"/payment/getdiscovery",CommonResult.class);
}
}
package com.han.cloud.configuration;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* 模块:
* RestTemplationConfiguration
* han
* 2020/7/18
*/
@Configuration
public class RestTemplateConfiguration {
@Bean
@LoadBalanced //ribbon负载均衡
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
四、eureka服务注册情况。接下来就可以访问消费端的接口了!
五、服务下线、自我保护机制
1.eureka默认开启自我保护机制。当有服务因网络延迟等情况未在规定时间(90s)内发送心跳,不会删掉该服务。
2.eureka-server可设置关掉自我保护机制。也可在instance下设置心跳频率(ms)。
enable-self-preservation: false #是否开启保护模式
eureka-client可在instance下设置
lease-renewal-interval-in-seconds: 30
本文地址:https://blog.csdn.net/qq_34150524/article/details/107500000
推荐阅读
-
spring-cloud入门之eureka-server(服务发现)
-
spring-cloud入门之eureka-client(服务注册)
-
SpringCloud之服务注册与发现Spring Cloud Eureka实例代码
-
springcloud干货之服务注册与发现(Eureka)
-
详解Spring Cloud Consul 实现服务注册和发现
-
SpringCloud之服务注册与发现Spring Cloud Eureka实例代码
-
基于Docker的Consul集群实现服务发现
-
1、微服务--为什么有consul,consul注册,心跳检测,服务发现
-
服务注册和发现(Consul)
-
springcloud使用之服务的注册发现与消费