在我配置好了pom.xml和启动类注解以后发现ping:一直都是空的 即使根据网上的方法访问了以后刷新也还是空的 但是用下面的代码是可以监控到数据的
@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
//一旦调用服务方法失败并抛出了错误信息后,会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法
@HystrixCommand(fallbackMethod = "processHystrix_Get")
public Dept get(@PathVariable("id") Long id)
{
Dept dept = this.service.get(id);
if (null == dept) {
throw new RuntimeException("该ID:" + id + "没有没有对应的信息");
}
return dept;
}
public Dept processHystrix_Get(@PathVariable("id") Long id)
{
return new Dept().setDeptno(id).setDname("该ID:" + id + "没有没有对应的信息,null--@HystrixCommand")
.setDb_source("no this database in MySQL");
}
直到我用了FallbackFactory之后去除了该方法上的@HystrixCommand注解 发现Dashboard监控不到数据,于是对代码进行了如下修改
package com.kelestar.springcloud.controller;
import com.kelestar.springcloud.entities.Dept;
import com.kelestar.springcloud.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class DeptController {
@Autowired
private DeptService service;
@Autowired
private DiscoveryClient client;
@HystrixCommand
@PostMapping(value = "/dept/add")
public boolean add(@RequestBody Dept dept){
System.out.println(dept.toString());
return service.add(dept);
}
@HystrixCommand
@GetMapping(value = "/dept/get/{id}")
public Dept get(@PathVariable("id") Long id){
return service.get(id);
}
@HystrixCommand
@GetMapping(value = "dept/list")
public List<Dept> list(){
return service.list();
}
@HystrixCommand
@RequestMapping(value = "/dept/discovery", method = RequestMethod.GET)
public Object discovery()
{
List<String> list = client.getServices();
System.out.println("**********" + list);
List<ServiceInstance> srvList = client.getInstances("SERVICECLOUD-DEPT");
for (ServiceInstance element : srvList) {
System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"
+ element.getUri());
}
return this.client;
}
}
若想监控某个请求只需要在方法上添加@HystrixCommand注解即可 不添加该注解的方法在Dashboard上是监控不到的
以下是成功的页面