spring cloud
Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发, 如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。
本篇文章要介绍的是如何使用Spring Cloud集成Consul做服务发现,并且利用Promtheus对其进行系统监控。Consul及Promtheus使用docker容器安装。
一. 项目搭建
建立Spring Boot项目,接下来贴一下pom文件
<?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.cloud.consul</groupId> <artifactId>cloud</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>cloud</name> <description>Cloud project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.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>Finchley.M9</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.0.0.M7</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-consul-dependencies</artifactId> <version>2.0.0.M7</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> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories></project>
加载主类
package com.cloud.consul.cloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;@SpringBootApplication@EnableAutoConfiguration@EnableDiscoveryClient@RestControllerpublic class CloudApplication { @RequestMapping("/") public Object home() { System.out.println("Cloud-Server-1"); return "Cloud-Server-1"; } /** * JSON数据格式 * body:{ * "imageUrl":"http://grafana.org/assets/img/blog/mixed_styles.png", * "message":"Someone is testing the alert notification within grafana.", * "ruleId":0, * "ruleName":"Test notification", * "ruleUrl":"http://grafana.52itstyle.com/", * "state":"alerting", * "title":"[Alerting] Test notification", * "evalMatches":[ * {"value":100,"metric":"High value","tags":null}, * {"value":200,"metric":"Higher Value","tags":null} * ] * } */ @RequestMapping("/webhook") public String webhook(String body,HttpServletRequest request) { //处理预警信息(邮件、短信、钉钉)// logger.info("webhook警报系统,body:{}",body); System.out.println("********************************************************"); return "success"; } public static void main(String[] args) { SpringApplication.run(CloudApplication.class, args); } }
接下来是比较重要的配置文件啦
server.port=8001spring.application.name=Consul-Server-1spring.security.user.name=admin spring.security.user.password=admin spring.cloud.consul.host=192.168.3.3spring.cloud.consul.port=8500spring.cloud.consul.enabled=truespring.cloud.consul.discovery.ena bled=truespring.cloud.consul.discovery.instanceId=tomcat1 spring.cloud.consul.discovery.serviceName=tomcat spring.cloud.consul.discovery.hostname=192.168.3.3spring.cloud.consul.discovery.port=${server.port} spring.cloud.consul.discovery.healthCheckUrl=http://192.168.3.3:8001/actuator/healthspring.cloud.consul.discovery.healthCheckInterval=10s spring.cloud.consul.discovery.tags=prometheus-target management.endpoint.prometheus.enabled=truemanagement.endpoints.web.exposure.include=*
这样咱们Spring的项目就已经搭建完毕,并且启动服务后会自动注册到Consul中。下面咱们来配置一下Consul
二. Consul
咱们使用docker容器进行安装,这里暂不对docker做多介绍了。
# 搜索镜像 docker search consul# 安装镜像 docker pull progrium/consul# 启动容器 docker run -d -p 8400:8400 -p 8500:8500 -p 8600:53/udp -h node1 progrium/consul -server -bootstrap -ui-dir /ui
这时候输入ip:8500就可以访问到我们consul的ui了。那么我们把之前的spring项目启动起来,看看会不会自动注册到刚刚部署好的consul中。如下图,已经注册进去了。
consul.png
接下来我们来使用docker部署Prometheus
三. Prometheus
# 搜索镜像 docker search prometheus# 安装镜像 docker pull prom/prometheus# 启动容器 docker run -d --name=prometheus -p 9090:9090 prom/prometheus# 进入容器,修改配置文件,加入consul docker exec -it <containerId> /bin/sh vi /etc/prometheus/prometheus.yml
配置文件如下图:
prometheus.png
这个时候打开ip:9090查看prometheus可以看到,已经可以监控通过consul注册的spring应用了
prometheus-targets.png
更深入的应用,比如说结合Grafana这样的前端展示,以后慢慢更新吧~~~
想写一些东西分享,欢迎转载,请注明出处。 简书-板凳儿儿 https://www.jianshu.com/p/d97fe71331a1
作者:板凳儿儿
链接:https://www.jianshu.com/p/d97fe71331a1