parent
3e350dc766
commit
4c744ab351
@ -0,0 +1,16 @@
|
|||||||
|
package org.example;
|
||||||
|
|
||||||
|
import de.codecentric.boot.admin.server.config.EnableAdminServer;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <h1>监控中心服务器启动入口</h1>
|
||||||
|
* */
|
||||||
|
@EnableAdminServer
|
||||||
|
@SpringBootApplication
|
||||||
|
public class AdminApplication {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(AdminApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
package org.example.conf;
|
||||||
|
|
||||||
|
import de.codecentric.boot.admin.server.config.AdminServerProperties;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
|
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
|
||||||
|
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <h1>配置安全认证, 以便其他的微服务可以注册</h1>
|
||||||
|
* 参考 Spring Security 官方
|
||||||
|
* */
|
||||||
|
@Configuration
|
||||||
|
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
|
/** 应用上下文路径 */
|
||||||
|
private final String adminContextPath;
|
||||||
|
|
||||||
|
public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
|
||||||
|
|
||||||
|
this.adminContextPath = adminServerProperties.getContextPath();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
|
|
||||||
|
// 成功授权后的 successHandler 的处理
|
||||||
|
SavedRequestAwareAuthenticationSuccessHandler successHandler =
|
||||||
|
new SavedRequestAwareAuthenticationSuccessHandler();
|
||||||
|
|
||||||
|
successHandler.setTargetUrlParameter("redirectTo");
|
||||||
|
successHandler.setDefaultTargetUrl(adminContextPath + "/");
|
||||||
|
|
||||||
|
http.authorizeRequests()
|
||||||
|
// 1. 配置所有的静态资源和登录页可以公开访问
|
||||||
|
.antMatchers(adminContextPath + "/assets/**").permitAll()
|
||||||
|
.antMatchers(adminContextPath + "/login").permitAll()
|
||||||
|
// 2. 其他请求, 必须要经过认证
|
||||||
|
.anyRequest().authenticated()
|
||||||
|
.and()
|
||||||
|
// 3. 配置登录和登出路径
|
||||||
|
.formLogin().loginPage(adminContextPath + "/login")
|
||||||
|
.successHandler(successHandler)
|
||||||
|
.and()
|
||||||
|
.logout().logoutUrl(adminContextPath + "/logout")
|
||||||
|
.and()
|
||||||
|
// 4. 开启 http basic 支持, 其他的服务模块注册时需要使用
|
||||||
|
.httpBasic()
|
||||||
|
.and()
|
||||||
|
// 5. 开启基于 cookie 的 csrf 保护
|
||||||
|
.csrf()
|
||||||
|
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
|
||||||
|
// 6. 忽略这些路径的 csrf 保护以便其他的模块可以实现注册
|
||||||
|
.ignoringAntMatchers(
|
||||||
|
adminContextPath + "/instances",
|
||||||
|
adminContextPath + "/actuator/**"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package org.example.notifier;
|
||||||
|
|
||||||
|
import de.codecentric.boot.admin.server.domain.entities.Instance;
|
||||||
|
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
|
||||||
|
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
|
||||||
|
import de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent;
|
||||||
|
import de.codecentric.boot.admin.server.notify.AbstractEventNotifier;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <h1>自定义告警</h1>
|
||||||
|
* */
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
@SuppressWarnings("all")
|
||||||
|
public class QNotifier extends AbstractEventNotifier {
|
||||||
|
protected QNotifier(InstanceRepository repository) {
|
||||||
|
super(repository);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <h2>实现对事件的通知</h2>
|
||||||
|
* */
|
||||||
|
@Override
|
||||||
|
protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
|
||||||
|
|
||||||
|
return Mono.fromRunnable(() -> {
|
||||||
|
|
||||||
|
if (event instanceof InstanceStatusChangedEvent) {
|
||||||
|
// todo 当状态发生改变的时候, 后面自己设置发送邮件或者短信啥的
|
||||||
|
log.info("Instance Status Change: [{}], [{}], [{}]",
|
||||||
|
instance.getRegistration().getName(), event.getInstance(),
|
||||||
|
((InstanceStatusChangedEvent) event).getStatusInfo().getStatus());
|
||||||
|
} else {
|
||||||
|
log.info("Instance Info: [{}], [{}], [{}]",
|
||||||
|
instance.getRegistration().getName(), event.getInstance(),
|
||||||
|
event.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
server:
|
||||||
|
port: 7001
|
||||||
|
servlet:
|
||||||
|
context-path: /dev-protocol-springcloud-admin
|
||||||
|
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: dev-protocol-springcloud-admin
|
||||||
|
# 添加访问控制
|
||||||
|
security:
|
||||||
|
user:
|
||||||
|
name: baiye-test
|
||||||
|
password: 88888888
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
enabled: true
|
||||||
|
server-addr: 127.0.0.1:8848
|
||||||
|
namespace: 1bc13fd5-843b-4ac0-aa55-695c25bc0ac6
|
||||||
|
metadata:
|
||||||
|
management:
|
||||||
|
context-path: ${server.servlet.context-path}/actuator
|
||||||
|
# 添加访问控制 和上面配置的保持一致
|
||||||
|
user.name: baiye-test
|
||||||
|
user.password: 88888888
|
||||||
|
# vue 的检查配置
|
||||||
|
thymeleaf:
|
||||||
|
check-template: false
|
||||||
|
check-template-location: false
|
||||||
|
# 被监控的应用状态变更为 DOWN、OFFLINE、UNKNOWN 时, 会自动发出告警: 实例的状态、原因、实例地址等信息
|
||||||
|
# 需要在 pom.xml 文件中添加 spring-boot-starter-mail 依赖
|
||||||
|
# 配置发送告警的邮箱服务器
|
||||||
|
# 但是, 这个要能连接上, 否则会报错
|
||||||
|
# mail:
|
||||||
|
# host: qinyi.imooc.com
|
||||||
|
# username: qinyi@imooc.com
|
||||||
|
# password: QinyiZhang
|
||||||
|
# default-encoding: UTF-8
|
||||||
|
# 监控告警通知
|
||||||
|
# boot:
|
||||||
|
# admin:
|
||||||
|
# notify:
|
||||||
|
# mail:
|
||||||
|
# from: ${spring.mail.username}
|
||||||
|
# to: qinyi@imooc.com
|
||||||
|
# cc: qinyi@imooc.com
|
||||||
|
|
||||||
|
# 暴露端点
|
||||||
|
management:
|
||||||
|
endpoints:
|
||||||
|
web:
|
||||||
|
exposure:
|
||||||
|
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 *, 可以开放所有端点
|
||||||
|
endpoint:
|
||||||
|
health:
|
||||||
|
show-details: always
|
Binary file not shown.
After Width: | Height: | Size: 292 KiB |
@ -0,0 +1,18 @@
|
|||||||
|
package org.example;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <h1>Nacos Client 工程启动入口</h1>
|
||||||
|
* */
|
||||||
|
//@RefreshScope // 刷新配置
|
||||||
|
@EnableDiscoveryClient
|
||||||
|
@SpringBootApplication
|
||||||
|
public class NacosClientApplication {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(NacosClientApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
package org.example.service;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
public class NacosClientService {
|
||||||
|
|
||||||
|
private final DiscoveryClient discoveryClient;
|
||||||
|
|
||||||
|
public NacosClientService(DiscoveryClient discoveryClient) {
|
||||||
|
this.discoveryClient = discoveryClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <h2>打印 Nacos Client 信息到日志中</h2>
|
||||||
|
* */
|
||||||
|
public List<ServiceInstance> getNacosClientInfo(String serviceId) {
|
||||||
|
|
||||||
|
// 测试 UseHystrixCommandAnnotation 的超时
|
||||||
|
// try {
|
||||||
|
// Thread.sleep(2000);
|
||||||
|
// } catch (InterruptedException ex) {
|
||||||
|
// //
|
||||||
|
// }
|
||||||
|
|
||||||
|
// 测试 NacosClientHystrixCommand 熔断
|
||||||
|
// throw new RuntimeException("has some error");
|
||||||
|
|
||||||
|
log.info("request nacos client to get service instance info: [{}]", serviceId);
|
||||||
|
return discoveryClient.getInstances(serviceId);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,74 @@
|
|||||||
|
server:
|
||||||
|
port: 8000
|
||||||
|
servlet:
|
||||||
|
context-path: /e-commerce-nacos-client
|
||||||
|
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: e-commerce-nacos-client # 应用名称也是构成 Nacos 配置管理 dataId 字段的一部分 (当 config.prefix 为空时)
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
# 服务注册发现
|
||||||
|
discovery:
|
||||||
|
enabled: true # 如果不想使用 Nacos 进行服务注册和发现, 设置为 false 即可
|
||||||
|
server-addr: 127.0.0.1:8848
|
||||||
|
# server-addr: 127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850 # Nacos 服务器地址, 集群模式
|
||||||
|
# todo 根据你自己的业务来进行区分找到
|
||||||
|
namespace: 1ccc74ae-9398-4dbe-b9d7-4f9addf9f40c
|
||||||
|
# group: e-commerce
|
||||||
|
# metadata:
|
||||||
|
# management:
|
||||||
|
# context-path: ${server.servlet.context-path}/actuator
|
||||||
|
# # 配置管理
|
||||||
|
# config:
|
||||||
|
# prefix: imooc-e-commerce
|
||||||
|
# file-extension: yaml # 配置内容的数据格式, 默认为 properties
|
||||||
|
# enabled: true # 如果不想使用 Nacos 进行配置管理, 设置为 false 即可
|
||||||
|
# group: DEFAULT_GROUP # 组, 默认为 DEFAULT_GROUP
|
||||||
|
# namespace: 1bc13fd5-843b-4ac0-aa55-695c25bc0ac6
|
||||||
|
# server-addr: 127.0.0.1:8848
|
||||||
|
# kafka:
|
||||||
|
# bootstrap-servers: 127.0.0.1:9092
|
||||||
|
# producer:
|
||||||
|
# retries: 3
|
||||||
|
# consumer:
|
||||||
|
# auto-offset-reset: latest
|
||||||
|
# sleuth:
|
||||||
|
# sampler:
|
||||||
|
# # ProbabilityBasedSampler 抽样策略
|
||||||
|
# probability: 1.0 # 采样比例, 1.0 表示 100%, 默认是 0.1
|
||||||
|
# # RateLimitingSampler 抽样策略, 设置了限速采集, spring.sleuth.sampler.probability 属性值无效
|
||||||
|
# rate: 100 # 每秒间隔接受的 trace 量
|
||||||
|
# zipkin:
|
||||||
|
# sender:
|
||||||
|
# type: kafka # 默认是 web
|
||||||
|
# base-url: http://localhost:9411/
|
||||||
|
#
|
||||||
|
## Feign 的相关配置
|
||||||
|
#feign:
|
||||||
|
# # feign 开启 gzip 压缩
|
||||||
|
# compression:
|
||||||
|
# request:
|
||||||
|
# enabled: true
|
||||||
|
# mime-types: text/xml,application/xml,application/json
|
||||||
|
# min-request-size: 1024
|
||||||
|
# response:
|
||||||
|
# enabled: true
|
||||||
|
# # 禁用默认的 http, 启用 okhttp
|
||||||
|
# httpclient:
|
||||||
|
# enabled: false
|
||||||
|
# okhttp:
|
||||||
|
# enabled: true
|
||||||
|
# # OpenFeign 集成 Hystrix
|
||||||
|
# hystrix:
|
||||||
|
# enabled: true
|
||||||
|
|
||||||
|
# 暴露端点
|
||||||
|
#management:
|
||||||
|
# endpoints:
|
||||||
|
# web:
|
||||||
|
# exposure:
|
||||||
|
# include: '*'
|
||||||
|
# endpoint:
|
||||||
|
# health:
|
||||||
|
# show-details: always
|
@ -0,0 +1,58 @@
|
|||||||
|
### 获取 Token
|
||||||
|
POST http://127.0.0.1:8000/ecommerce-nacos-client/communication/rest-template
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"username": "Qinyi@imooc.com",
|
||||||
|
"password": "25d55ad283aa400af464c76d713c07ad"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
### 获取 Token, 带有负载均衡
|
||||||
|
POST http://127.0.0.1:8000/ecommerce-nacos-client/communication/rest-template-load-balancer
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"username": "Qinyi@imooc.com",
|
||||||
|
"password": "25d55ad283aa400af464c76d713c07ad"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
### 通过 Ribbon 去获取 Token
|
||||||
|
POST http://127.0.0.1:8000/ecommerce-nacos-client/communication/ribbon
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"username": "Qinyi@imooc.com",
|
||||||
|
"password": "25d55ad283aa400af464c76d713c07ad"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
### 通过原生 Ribbon Api 去获取 Token
|
||||||
|
POST http://127.0.0.1:8000/ecommerce-nacos-client/communication/thinking-in-ribbon
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"username": "Qinyi@imooc.com",
|
||||||
|
"password": "25d55ad283aa400af464c76d713c07ad"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
### 通过 OpenFeign 获取 Token
|
||||||
|
POST http://127.0.0.1:8000/ecommerce-nacos-client/communication/token-by-feign
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"username": "Qinyi@imooc.com",
|
||||||
|
"password": "25d55ad283aa400af464c76d713c07ad"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
### 通过原生 Feign Api 获取 Token
|
||||||
|
POST http://127.0.0.1:8000/ecommerce-nacos-client/communication/thinking-in-feign
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"username": "Qinyi@imooc.com",
|
||||||
|
"password": "25d55ad283aa400af464c76d713c07ad"
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
### 根据提供的 serviceId 获取实例信息
|
||||||
|
GET http://127.0.0.1:8000/ecommerce-nacos-client/hystrix/hystrix-command-annotation?serviceId=e-commerce-nacos-client
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
### 根据提供的 serviceId 获取实例信息
|
||||||
|
GET http://127.0.0.1:8000/ecommerce-nacos-client/hystrix/simple-hystrix-command?serviceId=e-commerce-nacos-client
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
### 根据提供的 serviceId 获取实例信息
|
||||||
|
GET http://127.0.0.1:8000/ecommerce-nacos-client/hystrix/hystrix-observable-command?serviceId=e-commerce-nacos-client
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
### 根据提供的 serviceId 获取实例信息
|
||||||
|
GET http://127.0.0.1:8000/ecommerce-nacos-client/hystrix/cache-hystrix-command?serviceId=e-commerce-nacos-client
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
### 根据提供的 serviceId 获取实例信息
|
||||||
|
GET http://127.0.0.1:8000/ecommerce-nacos-client/hystrix/cache-annotation-01?serviceId=e-commerce-nacos-client
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
### 根据提供的 serviceId 获取实例信息
|
||||||
|
GET http://127.0.0.1:8000/ecommerce-nacos-client/hystrix/cache-annotation-02?serviceId=e-commerce-nacos-client
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
### 根据提供的 serviceId 获取实例信息
|
||||||
|
GET http://127.0.0.1:8000/ecommerce-nacos-client/hystrix/cache-annotation-03?serviceId=e-commerce-nacos-client
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
### 根据提供的 serviceId 获取实例信息
|
||||||
|
GET http://127.0.0.1:8000/ecommerce-nacos-client/hystrix/request-merge
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
### 根据提供的 serviceId 获取实例信息
|
||||||
|
GET http://127.0.0.1:8000/ecommerce-nacos-client/hystrix/request-merge-annotation
|
||||||
|
Content-Type: application/json
|
@ -0,0 +1,13 @@
|
|||||||
|
### 查询服务实例信息
|
||||||
|
GET http://127.0.0.1:8000/ecommerce-nacos-client/nacos-client/service-instance
|
||||||
|
Accept: application/json
|
||||||
|
|
||||||
|
### 动态从 Nacos Server 中获取配置信息
|
||||||
|
GET http://127.0.0.1:8000/ecommerce-nacos-client/nacos-client/project-config
|
||||||
|
Accept: application/json
|
||||||
|
|
||||||
|
### 查看 Sleuth 跟踪信息
|
||||||
|
GET http://127.0.0.1:9001/imooc/ecommerce-nacos-client/sleuth/trace-info
|
||||||
|
Accept: application/json
|
||||||
|
e-commerce-user: eyJhbGciOiJSUzI1NiJ9.eyJlLWNvbW1lcmNlLXVzZXIiOiJ7XCJpZFwiOjE3LFwidXNlcm5hbWVcIjpcIkltb29jUWlueWlAaW1vb2MuY29tXCJ9IiwianRpIjoiMGIxNzQyYWItMWU3OC00OTZjLWIyNTAtMjNkZGQ1ZGEyZTU1IiwiZXhwIjoxNjI0MjA0ODAwfQ.QKGHzohSHdYDHzUVHpe9gNPUgzfkPwrSbB-WiMWYjLlt2tr9BufzZM8bSt-whb_bd0hKoC6rkYYO0WUVR67uSML-2yaTL1xMIn8GH9Flyig3rpO4vefL3Hp2TXIpwHHa7WlKsLzcUpNk9lxWs2B5k0ICdYCH_jD5Tx6N7CzfSUG9u4fOnVeM9UFE2nX_DURupUh_DKCc2oOoMeyCSR7Ma8-Ab4WQU3r-U0YivR8G1A0kmKOIoTeRhM3LcPuxUPh3rCbrjzMg--fexRGw0O38Qsby6pz-ku2IlTyFXY6_jNOG1BZR34-jBOnaIciP1TExw9bFumeuC2GcowTHJVH1Nw
|
||||||
|
token: imooc
|
Loading…
Reference in New Issue