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-server
server.port=8081
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
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;
@RestController
public 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
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(
ClientApplication.class)
.web(true).run(args);
}
}
配置文件 application.properties
spring.application.name=eureka-client
server.port=8082
eureka.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
@EnableAdminServer
public class SpringBootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}
}
配置文件 application.properties
server.port=8083
spring.application.name=admin-control
eureka.client.serviceUrl.defaultZone=http://localhost:8081/eureka/
management.security.enabled=false
info.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 环境属性