栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Cloud入门系列(四)——服务注册与发现之Eureka

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Cloud入门系列(四)——服务注册与发现之Eureka

服务集群

新建相同的模块,更改端口,即可(略)

服务发现


以 8001为例:

DiscoveryClient就是服务发现。

8001的PaymentController的代码如下:

package com.banana.springcloud.controller;

import com.banana.springcloud.entity.CommonResult;
import com.banana.springcloud.entity.Payment;
import com.banana.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@Slf4j
@RequestMapping("/payment")
public class PaymentController {
   

    @Resource
    private PaymentService paymentService;

    @Resource
    private DiscoveryClient discoveryClient;

    @Value("${server.port}")
    private String serverPort;

    
    @PostMapping("/create")
    public CommonResult create(@RequestBody Payment payment){
        int result =  paymentService.create(payment);
        log.info("****插入结果:"+result);
        if(result > 0){
            return new CommonResult(200,"插入数据成功,serverPort:"+serverPort,payment.getId());
        }
        return new CommonResult(400,"插入数据失败");
    }

    @GetMapping("/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id){
        //@PathVariable:路径变量,是spring3.0的新功能,能够接收请求路径中占位符的值
        Payment payment =  paymentService.getPaymentById(id);
        if(payment !=null ){
            log.info("****查询结果:"+payment);
            return new CommonResult(200,"查询数据成功,serverPort:"+serverPort,payment);
        }
        return new CommonResult(400,"没有对应的记录,查询id为:"+id);
    }

    @RequestMapping("/discovery")
    public Object discovery(){
        List services = discoveryClient.getServices();
        for (String service : services) {
            log.info("注册的服务名称:"+service);
        }
        List instances = discoveryClient.getInstances("cloud-payment-service");        for (ServiceInstance instance : instances) {
            log.info(instance.getServiceId()+"t"+instance.getHost()+"t"+instance.getPort()+"t"+instance.getUri());
        }
        return this.discoveryClient;
    }
测试

同时,可以看到控制台打印以下信息:


其实,此时8002的端口,已经被停掉

但是,8002的服务名称依然出现在列表中,这里就要多谈一句, Eureka的保护机制。

Eureka的保护机制


自我保护的定义


如何禁止自我保护


修改7001的application.yml,新增以下内容:

  server:
    # 关闭Eureka的自我保护机制
    enable-self-preservation: false
    # 如果服务不可用,2000ms后剔除
    eviction-interval-timer-in-ms: 2000

完整内容如下:

server:
  port: 7001

eureka:
  instance:
    #eureka服务端的实例名称
    hostname: localhost
    #hostname: eureka7001.com
  client:
    #false表示不向注册中心注册自己
    register-with-eureka: false
    #false表示自己端就是注册中心,职责是维护实例,并不需要检索服务
    fetch-registry: false
    service-url:
      #设置 服务的注册地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      #defaultZone: http://eureka7001.com:7001/eureka  #集群版  #集群指向其他eureka
  server:
    # 关闭Eureka的自我保护机制
    enable-self-preservation: false
    # 如果服务不可用,2000ms后剔除
    eviction-interval-timer-in-ms: 2000
写在最后

因为Eureka已经停止更新,已经不再推荐使用。

但是,它的设计理念,依然值得我们学习。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/300961.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号