Feign优化调用
在服务调用模块上(user-server)
1. 添加依赖
<!--springcloud整合的openFeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2. 在feign包下创建一个接口
方法名和服务提供模块(order-server)暴露的API接口一样
package com.ddz.feigns;
import com.ddz.entity.Order;
import com.ddz.feigns.impl.OrderFeignClientFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;
@FeignClient(name = "order-server", fallback = OrderFeignClientFallback.class)
public interface OrderFeignClient {
@GetMapping(value = "/findOrderByUser/{uid}")
public List<Order> findOrderByUser(@PathVariable Integer uid);
}
3. 在启动类添加注解
4. 修改 UserServiceImpl 实现类
package com.ddz.service.impl;
import com.ddz.entity.Order;
import com.ddz.feign.OrderFeignClient;
import com.ddz.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Resource
private OrderFeignClient orderFeignClient;
/* 直接调用feign中的方法简化调用 */
@Override
public List<Order> findUserOrder(Integer uid) {
return orderFeignClient.findOrderByUser(uid);
}
}
Feign实现服务熔断
SpringCloud Fegin默认已为Feign整合了hystrix,所以添加Feign依赖后就不用在添加hystrix
1. 在 application.yml 配置文件中添加一个节点
2. 实现 feign 包下的接口
package com.ddz.feigns.impl;
import com.ddz.entity.Order;
import com.ddz.feigns.OrderFeignClient;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class OrderFeignClientFallback implements OrderFeignClient {
/**
* 返回假数据方法
*/
@Override
public List<Order> findOrderByUser(Integer uid) {
List<Order> list = new ArrayList<>();
list.add(new Order(1, "Feign全局熔断", 0, 0.0));
return list;
}
}
3. 修改接口注解
搭建Hystrix DashBoard监控
1. 新建 dashboard-server 模块
2. 添加依赖
<!-- hystrix -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- dashboard监测仪表盘 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
3. 创建 application.yml 配置文件
server:
port: 7004 #端口号
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/ #,http://localhost:7002/eureka/ #添加到的eureka注册中心的地址
instance:
prefer-ip-address: true #在eureka地址页面中显示微服务的ip地址和端口
instance-id: ${spring.cloud.client.ip-address}:${server.port} #设置发送心跳时间间隔
lease-renewal-interval-in-seconds: 5 #设置没有发送心跳 微服务在eureka中保存的最大时间
lease-expiration-duration-in-seconds: 10
spring:
application:
name: dashboard-server # 服务名称
4. 新建启动类并添加注解
package com.ddz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableHystrixDashboard//监测仪表盘
public class MyDashboardServer {
public static void main(String[] args) {
SpringApplication.run(MyDashboardServer.class, args);
}
// spring boot 2.0 以上版本需要指定 文件路径
// @Bean
// public ServletRegistrationBean getServlet() {
// HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
// ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
// registrationBean.setLoadOnStartup(1);
// registrationBean.addUrlMappings("/hystrix.stream");
// registrationBean.setName("HystrixMetricsStreamServlet");
// return registrationBean;
// }
}
spring boot 2.0 以上版本
5. 在需要监控的模块下添加依赖
<!-- hystrix -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- dashboard监测仪表盘 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
6. 在 application.yml 下添加节点
management:
endpoints:
web:
exposure:
include: hystrix.stream
7. 在启动类上添加注解
@EnableCircuitBreaker
8. 在需要监测的方法上添加注解
@HystrixCommand
9. 在浏览器访问
localhost:端口号/hystrix.stream
断路器聚合监控 Turbine
1. 在 dashboard 服务下添加依赖
<!-- turbine监测多个 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
2. 在 application.yml 下添加配置节点
turbine:
app-config: user-server
cluster-name-expression: "'default'"
3. 在启动类上添加注解
**@EnableTurbine **
4. 重启 dashboard 服务,在首页访问
localhost:Dashboard的端口/turbine.stream
监测多个方法
Sentinel替换Hystrix熔断
1. 下载jar包
链接:https://github.com/alibaba/Sentinel/releases/download/1.6.3/sentinel-dashboard-1.6.3.jar
2. 在下载好的目录下进入DOS页面输入指令启动服务
java -Dserver.port=10000 -Dcsp.sentinel.dashboard.server=localhost:10000 -
Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.6.3.jar
3. 在网页访问http://localhost:10000/
4. 在模块中接入控制台
I. 添加依赖
<!-- sentinel依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- spring-alibaba 大版本 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>1.5.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>