Spring cloud eureka+Client+Spring boot admin 服务注册监控

来源:互联网 发布:高铁订票软件 知乎 编辑:程序博客网 时间:2024/06/05 03:18

Spring cloud eureka 服务注册中心

创建一个springboot项目 eureka-server
pom.xml

<!-- spring-boot基础配置 --><parent>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-parent</artifactId>   <version>1.5.7.RELEASE</version>   <relativePath/> <!-- lookup parent from repository --></parent><dependencies>   <dependency>     <groupId>org.springframework.cloud</groupId>     <artifactId>spring-cloud-starter-eureka-server</artifactId>   </dependency></dependencies><dependencyManagement>    <dependencies>      <dependency>                              <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-dependencies</artifactId>        <version>Dalston.SR1</version>        <type>pom</type>        <scope>import</scope>      </dependency>   </dependencies></dependencyManagement>

入口文件 EurekaApplication.java

import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** *  * @author su  *   *     服务注册中心  eureka-server  *  * 备注:  *    通过@EnableEurekaServer注解启动一个服务注册中心提供给其他应用进行对话  *    在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所  *    以我们需要禁用它的客户端注册行为  */ @EnableEurekaServer @SpringBootApplication public class EurekaApplication {     public static void main(String[] args) {        new SpringApplicationBuilder(EurekaApplication.class)                    .web(true).run(args);    } }

配置文件 application.properties

spring.application.name=eureka-serverserver.port=8081eureka.instance.hostname=localhosteureka.client.register-with-eureka=falseeureka.client.fetch-registry=false

访问路径:http://localhost:8081

Client 服务提供者

创建一个springboot项目 eureka-client
pom.xml

<!-- spring-boot基础配置 -->    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.7.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <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-actuator</artifactId>        </dependency>    </dependencies>    <dependencyManagement>        <dependencies>            <dependency>               <groupId>org.springframework.cloud</groupId>               <artifactId>spring-cloud-dependencies</artifactId>               <version>Dalston.SR1</version>               <type>pom</type>               <scope>import</scope>            </dependency>        </dependencies>    </dependencyManagement>

controller SuController.java

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cloud.client.discovery.DiscoveryClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class SuController {    @Autowired    DiscoveryClient discoveryClient;    @GetMapping("/su")    public String su() {    /**    * discoveryClient.getServices():    * 通过Spring Cloud定义的DiscoveryClient接口在eureka的实现中    * 获取到的所有服务清单    */       String services = "Services: " + discoveryClient.getServices();       System.out.println(services);       return services;    }}

入口文件 ClientApplication .java

import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;/** *  * @author su *  服务提供者  eureka-server *  * 备注: *      通过@EnableDiscoveryClient注解, *      才能激活Eureka中的DiscoveryClient实现 *      这样才能实现Controller中对服务信息的输出 */@EnableDiscoveryClient@SpringBootApplicationpublic class ClientApplication {    public static void main(String[] args) {        new SpringApplicationBuilder(                ClientApplication.class)            .web(true).run(args);    }}

配置文件 application.properties

spring.application.name=eureka-clientserver.port=8082eureka.client.serviceUrl.defaultZone=http://localhost:8081/eureka/

启动该工程后,再次访问:http://localhost:8081/
访问路径:http://localhost:8082/su

Spring boot admin 服务监控

创建一个springboot项目 admin-control
pom.xml

<!-- spring-boot基础配置 --><parent>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-parent</artifactId>   <version>1.5.7.RELEASE</version>   <relativePath/> <!-- lookup parent from repository --></parent><dependencies>   <dependency>     <groupId>org.springframework.cloud</groupId>     <artifactId>spring-cloud-starter-eureka-server</artifactId>   </dependency></dependencies><dependencyManagement>    <dependencies>      <dependency>                              <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-dependencies</artifactId>        <version>Dalston.SR1</version>        <type>pom</type>        <scope>import</scope>      </dependency>   </dependencies></dependencyManagement>

入口文件 SpringBootAdminApplication.java

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import de.codecentric.boot.admin.config.EnableAdminServer;@SpringBootApplication@EnableDiscoveryClient@EnableAdminServerpublic class SpringBootAdminApplication {    public static void main(String[] args) {        SpringApplication.run(SpringBootAdminApplication.class, args);    }}

配置文件 application.properties

server.port=8083spring.application.name=admin-controleureka.client.serviceUrl.defaultZone=http://localhost:8081/eureka/management.security.enabled=falseinfo.version=@project.version@management.security.enabled=false

启动服务注册中心后,再次启动该工程
访问路径:http://localhost:8083

logback.xml

<?xml version="1.0" encoding="UTF-8"?>  <configuration>     <include resource="org/springframework/boot/logging/logback/base.xml"/>  </configuration>

spring boot admin可监控信息:
(具体配置后续跟进)

显示 name/id 和版本号显示在线状态Logging日志级别管理JMX beans管理Threads会话和线程管理Trace应用请求跟踪应用运行参数信息,如:Java 系统属性Java 环境变量属性内存信息Spring 环境属性
原创粉丝点击