本章主要试验:eureka客户端注册 ,监控,eureka客户端信息

spring-cloud-provider-01

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>spring-cloud-provider-01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-cloud-provider-01</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Camden.SR7</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

application.yaml

spring:
  application:
    name: spring-cloud-provider
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    prefer-ip-address: true

SpringCloudProvider01Application.java

package com.example.demo;

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;
@EnableEurekaClient
//@EnableDiscoveryClient
@SpringBootApplication
public class SpringCloudProvider01Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudProvider01Application.class, args);
    }
}

spring-cloud-provider-02

spring-cloud-provider-02项目和spring-cloud-provider-01除了yaml之外,其他都是一样的

application.yaml

server:
  port: 8081
spring:
  application:
    name: spring-cloud-provider
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    prefer-ip-address: true

结果验证

image
image

监控模块

spring-boot-starter-actuator模块具有监控的功能

添加maven依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

浏览器验证

http://192.168.88.102:8080/health

{"description":"Spring Cloud Eureka Discovery Client","status":"UP","discoveryComposite":{"description":"Spring Cloud Eureka Discovery Client","status":"UP","discoveryClient":{"description":"Spring Cloud Eureka Discovery Client","status":"UP","services":["spring-cloud-provider"]},"eureka":{"description":"Remote status from Eureka server","status":"UNKNOWN","applications":{"SPRING-CLOUD-PROVIDER":1}}},"diskSpace":{"status":"UP","total":311385124864,"free":246084595712,"threshold":10485760},"refreshScope":{"status":"UP"},"hystrix":{"status":"UP"}}

eureka客户端相关信息

spring-cloud-provider-01和spring-cloud-provider-02

MicroServiceInfoController.java

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.netflix.discovery.EurekaClient;

@Controller
@RequestMapping(value="/micro_server")
public class MicroServiceInfoController {

    @Autowired
    private EurekaClient eurekaClient;
    @Autowired
    private DiscoveryClient discoveryClient;
    @ResponseBody
    @GetMapping(value="url")
    public String url(){
        return eurekaClient.getNextServerFromEureka("SPRING-CLOUD-PROVIDER", false).getHomePageUrl();
    }
    @ResponseBody
    @GetMapping(value="info")
    public ServiceInstance info(){
        return discoveryClient.getLocalServiceInstance();
    }
}

http://192.168.88.102:8081/micro_server/url

浏览器显示:http://192.168.88.102:8081/

http://192.168.88.102:8081/micro_server/info

浏览器显示

{“host”:”192.168.88.102”,”port”:8081,”metadata”:{},”uri”:”http://192.168.88.102:8081","secure":false,"serviceId":"spring-cloud-provider"}