Eureka: service registration-information configuration-self-protection mechanism

First, under the provider service, add a dependency

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

In the provider yml add


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

main startup class

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);
    }
}

Start the server first,

at startup provider

Visit http://localhost:7001/

 

 

 

 

 

self-protection mechanism

 

 

 Some providers disconnected when they provided again, eureka will start the self-protection mechanism

Need to improve monitoring information

Then add the provider's pom file

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

Provider's yml file

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

 

 This is for displaying company information

self-protection mechanism

 information display mechanism

Add a dependency to the pom file in the provider

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

yml plus one

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

Add an annotation to the main startup class

@EnableDiscoveryClient//Service Discovery~
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);
    }
}

Write the method on the 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;

    }

Then start eureka first

Initiate provider access

 

 

Guess you like

Origin blog.csdn.net/qq_53374893/article/details/132389530
Recommended