环境的搭建
首先搭建eruka的注册中心
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.tuling.cloud</groupId> <artifactId>microservice-eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <!-- 引入spring boot的依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!-- spring boot admin监控客户端依赖 --> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.1.4</version> </dependency> </dependencies> <!-- 引入spring cloud的依赖 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- 添加spring-boot的maven插件 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yml
server: port: 8761 # 指定该Eureka实例的端口 eureka: client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://localhost:8761/eureka/ # 参考文档:http://projects.spring.io/spring-cloud/docs/1.0.3/spring-cloud.html#_standalone_mode # 参考文档:http://my.oschina.net/buwei/blog/618756
EurekaApplication启动类
package com.tuling.cloud.study; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * 使用Eureka做服务发现. */ @SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
接下来我们来看admin sererhttps://blog.csdn.net/a1036645146/article/details/97249153
参考博客:https://blog.csdn.net/a1036645146/article/details/97249153
Spring Boot Admin是一个社区项目,用于管理和监视基于SpringBoot的应用程序。客户端应用可以通过Spring Boot Admin Client或者注册中心就可以注册到Spring Boot Admin服务端进行监控。Spring Boot Admin 是在 Spring Boot Actuator 的基础上提供简洁的可视化 WEB UI。2.X版本使用Vue.js重写了UI界面,简洁。
2.3 修改application.yml配置文件,端口可以自己修改: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.tuling.cloud</groupId> <artifactId>spring-boot-admin-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <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> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- eureka注册中心--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.0.2.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.2 启动类添加相关注解,@EnableAdminServer:
package com.tuling.cloud.study; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.context.annotation.Configuration; import de.codecentric.boot.admin.server.config.EnableAdminServer; @Configuration @EnableAutoConfiguration @EnableAdminServer @EnableDiscoveryClient public class SpringBootAdminApplication { public static void main(String[] args) { SpringApplication.run(SpringBootAdminApplication.class, args); } }
2.3 修改application.yml配置文件,端口可以自己修改:
server: port: 6666 spring: application: name: SpringBootAdmin boot: admin: ui: title: SpringBootAdmin-Server # eureka注册中心 #eureka: # client: # serviceUrl: # defaultZone: http://localhost:1001/eureka/ #Actuator配置:暴露敏感路径,默认情况下,敏感路径并不暴露 management: endpoints: web: exposure: # 暴露xxx端点,如需暴露多个,用,分隔;如需暴露所有端点,用'*' include: "*" endpoint: health: # 是否展示健康检查详情 show-details: ALWAYS eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true
2.4 启动服务端应用:启动先启动注册中心,然后在启动admin server
admin Server应用已经把自己注册上到了注册中心,同时也监控了自己
接下来,我们要让admin server监控其他客户端应用, Spring Boot Admin Client 客户端搭建 同样创建一个Springboot 应用,基于2.1.4版本,pom依赖如下:
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.tuling.cloud</groupId> <artifactId>microservice-provider-user</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <!-- 引入spring boot的依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- spring boot admin监控客户端依赖 --> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-sleuth-zipkin</artifactId> </dependency> </dependencies> <!-- 引入spring cloud的依赖 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- 添加spring-boot的maven插件 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yml如下
server:
port: 8888
spring:
boot:
admin:
client:
# server地址
url: http://localhost:6666
instance:
#client地址
service-base-url: http://localhost:${server.port}
jpa:
generate-ddl: false
show-sql: true
hibernate:
ddl-auto: none
datasource: # 指定数据源
platform: h2 # 指定数据源类型
schema: classpath:schema.sql # 指定h2数据库的建表脚本
data: classpath:data.sql # 指定h2数据库的数据脚本
application:
name: SpringBootAdmin-Client
main:
allow-bean-definition-overriding: true
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
#Actuator配置:暴露敏感路径,默认情况下,敏感路径并不暴露
management:
endpoints:
web:
exposure:
# 暴露xxx端点,如需暴露多个,用,分隔;如需暴露所有端点,用'*'
include: "*"
endpoint:
health:
# 是否展示健康检查详情
show-details: ALWAYS
# info信息会显示到SpringBootAdmin的server端,这里取的是pom文件中的数据
info:
version: @project.version@
groupId: @project.groupId@
artifactId: @project.artifactId@
这里需要特别需要特别强调的是,因为admin server是注册在了注册中心上面,admin server 的客户端如果也注册到了注册中心上面, -admin-starter-client会从注册中心获得admin Server的地址,所以上面的
不要在手动指定admin server的地址,这里写成下面的形式
server:
port: 4444
spring:
jpa:
generate-ddl: false
show-sql: true
hibernate:
ddl-auto: none
datasource: # 指定数据源
platform: h2 # 指定数据源类型
schema: classpath:schema.sql # 指定h2数据库的建表脚本
data: classpath:data.sql # 指定h2数据库的数据脚本
application:
name: user
main:
allow-bean-definition-overriding: true
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
#Actuator配置:暴露敏感路径,默认情况下,敏感路径并不暴露
management:
endpoints:
web:
exposure:
# 暴露xxx端点,如需暴露多个,用,分隔;如需暴露所有端点,用'*'
include: "*"
endpoint:
health:
# 是否展示健康检查详情
show-details: ALWAYS
# info信息会显示到SpringBootAdmin的server端,这里取的是pom文件中的数据
info:
version: @project.version@
groupId: @project.groupId@
artifactId: @project.artifactId@
data.sql
insert into user (id, username, name, age, balance) values (1, 'account1', '张三', 20, 100.00);
insert into user (id, username, name, age, balance) values (2, 'account2', '李四', 28, 180.00);
insert into user (id, username, name, age, balance) values (3, 'account3', '王五', 32, 280.00);
schema.sql
drop table user if exists;
create table user (id bigint generated by default as identity, username varchar(40), name varchar(20), age int(3), balance decimal(10,2), primary key (id));
jpa访问h2数据库
package com.tuling.cloud.study.repository; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import com.tuling.cloud.study.entity.User; @Repository public interface UserRepository extends JpaRepository<User, Long> { }
package com.tuling.cloud.study.entity; import java.math.BigDecimal; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column private String username; @Column private String name; @Column private Integer age; @Column private BigDecimal balance; public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Integer getAge() { return this.age; } public void setAge(Integer age) { this.age = age; } public BigDecimal getBalance() { return this.balance; } public void setBalance(BigDecimal balance) { this.balance = balance; } }
package com.tuling.cloud.study.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.tuling.cloud.study.entity.User; import com.tuling.cloud.study.repository.UserRepository; @RestController public class UserController { @Autowired private UserRepository userRepository; @Autowired private Registration registration; @GetMapping("/{id}") public User findById(@PathVariable Long id) throws Exception { Thread.sleep(3000); User findOne = userRepository.findById(id).get(); return findOne; } @GetMapping("/getIpAndPort") public String findById() { return registration.getHost() + ":" + registration.getPort(); } @GetMapping("/testException") public User testException() throws Exception { Thread.sleep(2000); throw new RuntimeException("userService throw new RuntimeException "); } @RequestMapping(value = "/aa", method = RequestMethod.GET) public String aa(){ try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "aa is sb"; } }
启动类的代码
package com.tuling.cloud.study; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class ProviderUserApplication { public static void main(String[] args) { SpringApplication.run(ProviderUserApplication.class, args); } }
3.3 启动客户端应用,显示已注册上,选项内容挺丰富的,具体自己运行去看下: