Eureka:服务注册-信息配置-自我保护机制

首先在提供者服务下,添加一个依赖

XML 复制代码
<!--    Eureka    -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

在提供者yml加上

XML 复制代码
#Eureka的配置,服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/

主启动类

java 复制代码
package com.kuang.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient //在服务启动后自动注册到 eureka服务端
public class DeptApplicationProvider {
    public static void main(String[] args) {
        SpringApplication.run(DeptApplicationProvider.class,args);
    }
}

先启动服务端,

在启动提供者

访问http://localhost:7001/

自我保护机制

有些提供者再提供的时候断掉了连接,eureka会启动自我保护机制

需要完善监控信息

再提供者的pom文件加入

XML 复制代码
<!--    完善监控信息    -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

提供者的yml文件

XML 复制代码
#info配置:
info:
  app.name: kuangshen-springcloud
  company.name: blog.kuangstudy.com

这个是显示公司信息用的

自我保护机制

信息显示机制

在提供者里pom文件加一个依赖

XML 复制代码
<!--    完善监控信息    -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

yml加一个

XML 复制代码
#info配置:
info:
  app.name: kuangshen-springcloud
  company.name: blog.kuangstudy.com

在主启动类上面加一个注解

复制代码
@EnableDiscoveryClient//服务发现~
java 复制代码
package com.kuang.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient //在服务启动后自动注册到 eureka服务端
@EnableDiscoveryClient//服务发现~
public class DeptApplicationProvider_8001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptApplicationProvider_8001.class,args);
    }
}

在controller写上方法

java 复制代码
   @GetMapping("/dept/discover")
    //注册进来的微服务~,获取一些消息
    public Object discovery(){
        //获取微服务列表的清单
        List<String> services = client.getServices();
        System.out.println("discovery=>services:"+services);

        //得到一个具体的微服务信息,通过具体的微服务id,applicationName
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println(
                    instance.getHost()+"\t"+
                    instance.getPort()+"\t"+
                    instance.getUri()+"\t"+
                    instance.getServiceId()


            );
        }
        return this.client;

    }

然后先启动eureka

在启动提供者 访问

相关推荐
RainbowSea16 小时前
12. LangChain4j + 向量数据库操作详细说明
java·langchain·ai编程
RainbowSea16 小时前
11. LangChain4j + Tools(Function Calling)的使用详细说明
java·langchain·ai编程
考虑考虑20 小时前
Jpa使用union all
java·spring boot·后端
用户37215742613520 小时前
Java 实现 Excel 与 TXT 文本高效互转
java
浮游本尊21 小时前
Java学习第22天 - 云原生与容器化
java
Serverless社区1 天前
函数计算的云上计费演进:从请求驱动到价值驱动,助力企业走向 AI 时代
阿里云·云原生·serverless
渣哥1 天前
原来 Java 里线程安全集合有这么多种
java
间彧1 天前
Spring Boot集成Spring Security完整指南
java
间彧1 天前
Spring Secutiy基本原理及工作流程
java
Java水解1 天前
JAVA经典面试题附答案(持续更新版)
java·后端·面试