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

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

<!--    Eureka    -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

在提供者yml加上


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

主启动类

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文件加入

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

提供者的yml文件

#info配置:
info:
  app.name: kuangshen-springcloud
  company.name: blog.kuangstudy.com

 

 这个是显示公司信息用的

自我保护机制

 信息显示机制

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

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

yml加一个

#info配置:
info:
  app.name: kuangshen-springcloud
  company.name: blog.kuangstudy.com

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

@EnableDiscoveryClient//服务发现~
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写上方法

   @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

在启动提供者 访问

 

猜你喜欢

转载自blog.csdn.net/qq_53374893/article/details/132389530