git add .

This commit is contained in:
罗祥
2019-01-16 14:24:37 +08:00
parent 7c3e42909c
commit c19c349fd5
19 changed files with 206 additions and 38 deletions

View File

@ -27,6 +27,26 @@
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!--ribbon 依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<!--引入hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--hystrix 监控依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<!--健康检查依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

View File

@ -3,11 +3,24 @@ package com.heibaiying.producer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
public class ProducerApplication {
@LoadBalanced // 配置客户端负载均衡
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}

View File

@ -0,0 +1,24 @@
package com.heibaiying.producer.controller;
import com.heibaiying.producer.service.api.ICustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author : heibaiying
* @description : 用来测试turbine聚合监控功能的接口 调用了custom的服务
*/
@RestController
public class CustomController {
@Autowired
private ICustomService customService;
@GetMapping("consumers")
public String queryCustoms() {
return customService.queryCustoms();
}
}

View File

@ -3,6 +3,7 @@ package com.heibaiying.producer.controller;
import com.heibaiying.common.api.IProductService;
import com.heibaiying.common.bean.Product;
import com.heibaiying.producer.service.api.ICustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@ -17,6 +18,7 @@ public class ProducerController {
@Autowired
private IProductService productService;
@GetMapping("products")
public List<Product> productList() {
return productService.queryAllProducts();
@ -31,4 +33,5 @@ public class ProducerController {
public void save(@RequestBody Product product) {
productService.saveProduct(product);
}
}

View File

@ -0,0 +1,28 @@
package com.heibaiying.producer.service;
import com.heibaiying.producer.service.api.ICustomService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
* @author : heibaiying
*/
@Service
public class CustomService implements ICustomService {
@Autowired
RestTemplate restTemplate;
@Override
@HystrixCommand(fallbackMethod = "queryCustomsFail")
public String queryCustoms() {
return restTemplate.getForObject("http://consumer/consumers", String.class);
}
public String queryCustomsFail() {
return "获取用户列表失败";
}
}

View File

@ -0,0 +1,11 @@
package com.heibaiying.producer.service.api;
/**
* @author : heibaiying
* @description : 用户接口
*/
public interface ICustomService {
String queryCustoms();
}

View File

@ -9,3 +9,9 @@ eureka:
client:
serviceUrl:
defaultZone: http://localhost:8010/eureka/
management:
endpoints:
web:
exposure:
# 需要开启hystrix.stream端点的暴露 这样才能获取到监控信息 * 代表开启可监控端点
include: "*"