【前面的话】书接上文,本文的某些知识依赖个人第一篇SpringCLoud的文章:SpringCloud之Eureka,若是没有看过能够先移步去看一下。另外在微服务架构中,业务都会被拆分红一个个独立的服务,服务与服务的通信是基于http restful的。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另外一种是feign。上一篇文章已经讲过ribbon+rest这种方式了,这一片博文主要讲feign的应用。java
Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只须要建立一个接口并注解。它具备可插拔的注解特性,可以使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。git
简而言之:github
新建一个feign子工程lovin-feign-client,用于后面的操做。下面是主要的pom依赖:~~~pom
<artifactId>lovin-feign-client</artifactId>
<version>0.0.1</version>
<name>lovinfeignclient</name>
<description>feignclient测试</description>复制代码
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
</dependencies>复制代码
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
~~~
- 这里为了安全,我这里仍是添加**spring-boot-starter-security**
~~~yaml
server:
port: 8806 # 服务端口号
spring:
application:
name: lovinfeignclient # 服务名称
security:
basic:
enabled: true
user:
name: lovin
password: ${REGISTRY_SERVER_PASSWORD:lovin}
eureka:
client:
serviceUrl:
defaultZone: http://lovin:lovin@localhost:8881/eureka/ # 注册到的eureka服务地址
feign:
hystrix:
enabled: true
~~~
- 配置**spring-boot-starter-security**,这里为了方便我这里放开全部请求
~~~java
package com.eelve.lovin.config;复制代码
import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;spring
/**安全
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()
.and().csrf().disable();
}
}
~~~
- 在主类上添加**@EnableFeignClients**和**@EnableHystrix** ,固然也须要注册到注册中心:
~~~java
package com.eelve.lovin;复制代码
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.hystrix.EnableHystrix;import org.springframework.cloud.openfeign.EnableFeignClients;restful
/**网络
import com.eelve.lovin.hystrix.FeignRemoteServiceImpl;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;架构
/**app
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String hello();
}
~~~
- 添加熔断器调用方法:新建**FeignRemoteServiceImpl**实现**FeignRemoteService**接口:
~~~java
package com.eelve.lovin.hystrix;复制代码
import com.eelve.lovin.service.FeignRemoteService;import org.springframework.stereotype.Component;
/**
import com.eelve.lovin.service.FeignRemoteService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;
/**
@Autowired
FeignRemoteService feignRemoteService;复制代码
@GetMapping(value = "/getHello")
public String getHello() {
return feignRemoteService.hello();
}
}
~~~
# 叁、启动测试
- 依次启动eureka的服务端和两个客户端,以及新建的lovin-feign-client

咱们能够看到服务已经所有启动成功
- 而后访问http://localhost:8806/getHello

咱们能够看到已经能够经过feign调到咱们创建的eureka客户端了
- 再次请求接口观察返回

咱们能够看到咱们调到了经过feign调用ribbon负载的另一个接口了,到这里咱们就已经弄好了一个简单的ribbon负载。复制代码
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;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;
/**
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
~~~
- 访问http://localhost:8806/hystrix

这里咱们经过首页能够看到:
~~~
默认的集群监控:经过URL http://turbine-hostname:port/turbine.stream开启,实现对默认集群的监控。
指定的集群监控:经过URL http://turbine-hostname:port/turbine.stream?cluster=[clusterName]开启,实现对clusterName的监控。
单体应用监控:经过URL http://hystrix-app:port/hystrix.stream开启,实现对某个具体的服务监控
~~~
- 添加监控模式查看详情,这里选择第三个单体应用复制代码
这样咱们就完成了熔断器的监控,固然具体含义有待下一步深究。
---