Integer id){
log.info("------ 根据文档编号:{},获取文档详情 ------",id);
EdocEntryVO edocEntry = entryService.getEdocEntryById(id);
if (edocEntry == null){
//抛出异常,触发 熔断 备选方法
throw new RuntimeException("id为:"+id+"的用户不存在,触发服务熔断");
}
return edocEntry;
}
// 根据 id 查询文档详情 方法 服务熔断后 的备选方案
public EdocEntryVO fallbackStoryDetail(@RequestParam Integer id){
//此方法,只用服务熔断时,才会被调用
EdocEntryVO edocEntryVO = new EdocEntryVO();
edocEntryVO.setId(999);
edocEntryVO.setCid(0);
edocEntryVO.setTitle("根据 id 查询文档详情 方法 服务熔断后 的备选方案");
edocEntryVO.setSummary("当根据id 查询不到具体用户信息时,就会触发");
edocEntryVO.setUploadUser("hystrix");
edocEntryVO.setCreateDate(new Date());
return edocEntryVO;
}
}
6.2.3 主启动类上的注解
@EnableHystrix //开启熔断服务 旧的开启服务熔断注解: @EnableCircuitBreaker
6.2.4 测试
6.2.4.1 没有服务降级,也没有服务熔断 时
直接返回错误;
6.2.4.2 有服务降级,没有服务熔断 时
触发服务降级;
6.2.4.3 有服务降级,也有服务熔断 时
触发服务熔断;
7、显示eureka中服务的基本信息
7.1 依赖
<!-- 暴露info端点给外部访问, 需要添加暴露端点的启动依赖: -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
7.2 配置
# 服务信息 eureka 中服务配置的基本信息
info:
app.name: web 消费端
company.name: com.kgc.kh96 # 公司域名
# 其他信息可以自行查询
7.3 测试
点击服务路径:
点击后的测试信息:
8、eureka注册中心进行加密
8.1Eureka加密的重要性
如果我们不对Eureka进行加密就会导致一些恶意的微服务注册到Eureka注册中心,为了防止这种事情发生我们就要对Eureka进行加密处理。
8.2 实现步骤
8.2.1 配置依赖 maven
? Eureka 自带了一个 Web 的管理页面,方便我们查询注册到上面的实例信息,但是有一个问题:如果在实际使用中,注册中心地址有公网 IP 的话,必然能直接访问到,这样是不安全的。所以我们需要对 Eureka 进行改造,加上权限认证来保证安全性。
? 改造我们的 eureka-server,通过集成 Spring-Security 来进行安全认证。
? 在 pom.xml 中添加 Spring-Security 的依赖包,代码如下所示。
<!-- eureka依赖配置 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- Spring-Security 的依赖包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
8.2.2 springboot Eureka注册中心 配置文件
8.2.2.1yml文件:
spring:
security:
user:
name: Your username
password: Yourpassword
8.2.2.1properties格式:
spring.security.user.name: Your username
spring.security.user.name: Your password
8.2.3 注册中心security配置
package com.dazuizui.eureka1.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
/* @Override
protected void configure(HttpSecurity http) throws Exception {
// 关闭csrf
http.csrf().disable();
// 支持httpBasic
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}*/
@Override
protected void configure(HttpSecurity http) throws Exception {
//过滤eureka路径
http.csrf().ignoringAntMatchers("/eureka/**");
super.configure(http);
}
}
8.2.3 注册中心的配置文件
8.2.3.1 y