一:feign是springCloud跨服务调用的组件,feign底层也是一个基于http的封装
我现在创建两个服务,一个消费者,一个生产者:
pom:
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.boot
spring-boot-starter-actuator
创建一个生产者:
server:
port: 8020
spring:
application:
name: producer
eureka:
client:
healthcheck:
enabled: true #健康检查
serviceUrl.defaultZone: http://localhost:8010/eureka/
instance:
instance-id: ${spring.cloud.client.ip-address}:${server.port}
prefer-ip-address: true
lease-expiration-duration-in-seconds: 10 # 发呆时间,即服务续约到期时间(缺省为90s)
lease-renewal-interval-in-seconds: 5 # 心跳时间,即服务续约间隔时间(缺省为30s)
metadata-map:
cluster: service
feign:
hystrix:
enabled: true
创建一个http接口:
package com.example.producer.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Auther: gongyiyang
* @Date: 2018/10/22 12:47
* @Description:
*/
@RestController
@RequestMapping("/producer")
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "你好,这是producer";
}
}
创建一个消费者:
server:
port: 8021
spring:
application:
name: consumer
eureka:
client:
healthcheck:
enabled: true
serviceUrl.defaultZone: http://localhost:8010/eureka/
instance:
#实例显示格式,默认按照ip,名称,端口
instance-id: ${spring.cloud.client.ip-address}:${server.port}
prefer-ip-address: true
# 发呆时间,即服务续约到期时间(缺省为90s)
lease-expiration-duration-in-seconds: 10
# 心跳时间,即服务续约间隔时间(缺省为30s)
lease-renewal-interval-in-seconds: 5
metadata-map:
cluster: service
feign:
hystrix:
enabled: true
接口开发:
package com.example.consumer.controller;
import com.example.consumer.servise.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author PC_gongyiyang
* @Auther: gongyiyang
* @Date: 2018/10/22 15:47
* @Description:
*/
@RestController
@RequestMapping("/consumer")
public class FeignHelloController {
@Autowired
private HelloService helloService;
@GetMapping("/hello")
public String hello() {
return helloService.hello();
}
}
调用生产者接口:
package com.example.consumer.servise;
import com.example.consumer.servise.fallback.HelloServiceBack;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @author PC_gongyiyang
* @Auther: gongyiyang
* @Date: 2018/10/22 15:44
* @Description:
*/
@FeignClient(name = "producer",fallback = HelloServiceBack.class)
public interface HelloService {
@GetMapping("/producer/hello")
String hello();
}
设置一个异常回调:
package com.example.consumer.servise.fallback;
import com.example.consumer.servise.HelloService;
import org.springframework.stereotype.Component;
/**
* @author PC_gongyiyang
* @Auther: gongyiyang
* @Date: 2018/10/22 16:00
* @Description:
*/
@Component
public class HelloServiceBack implements HelloService {
@Override
public String hello() {
return "这是consumer的fallback";
}
}
调用结果:
简单的配置服务间的调用就完成了!
二:hystrix熔断器:
在应用启动时配置熔断器地址:
package com.example.consumer;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
/**
* @author PC_gongyiyang
* @Auther: gongyiyang
* @Date: 2018/10/22 14:02
* @Description:
*/
@EnableHystrix
@EnableHystrixDashboard
@EnableFeignClients
@SpringCloudApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@SuppressWarnings("Duplicates")
@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;
}
}
访问熔断器地址;
可以看到熔断器正在工作:
三:actuator应用监控:
为actuator创建一个应用:
pom:
org.springframework.cloud
spring-cloud-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-turbine
org.springframework.cloud
spring-cloud-starter-netflix-hystrix-dashboard
配置信息:
server:
port: 8030
spring:
application:
name: turbine
eureka:
client:
healthcheck:
enabled: true
serviceUrl.defaultZone: http://localhost:8010/eureka/
instance:
instance-id: ${spring.cloud.client.ip-address}:${server.port}
prefer-ip-address: true
lease-expiration-duration-in-seconds: 10 # 发呆时间,即服务续约到期时间(缺省为90s)
lease-renewal-interval-in-seconds: 5 # 心跳时间,即服务续约间隔时间(缺省为30s)
turbine:
app-config: consumer,producer,zuul #需要监控的服务名
aggregator:
cluster-config: service,zuul #需要监控的服务集群名,default
cluster-name-expression: metadata['cluster'] #new String("default")
combine-host-port: true
instanceUrlSuffix:
service: hystrix.stream ##key为clusterConfig的集群名字,默认为default
zuul: hystrix.stream ##value为集群的hystrix监控url后缀,springboot2.0默认为actuator/hystrix.stream
启动日志可以看到有哪些熔断地址:
访问以下监控页面:
看一下应用监控详情:
-------------------------------------------------------------------
最后分享一个买东西省钱又可以分享赚钱的APP: