SpringCloud之Feign

前面的話】書接上文,本文的某些知識依賴個人第一篇SpringCLoud的文章:SpringCloud之Eureka,若是沒有看過能夠先移步去看一下。另外在微服務架構中,業務都會被拆分紅一個個獨立的服務,服務與服務的通信是基於http restful的。Spring cloud有兩種服務調用方式,一種是ribbon+restTemplate,另外一種是feign。上一篇文章已經講過ribbon+rest這種方式了,這一片博文主要講feign的應用。java

壹、Feign的簡介

Feign是一個聲明式的僞Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只須要建立一個接口並註解。它具備可插拔的註解特性,可以使用Feign 註解和JAX-RS註解。Feign支持可插拔的編碼器和解碼器。Feign默認集成了Ribbon,並和Eureka結合,默認實現了負載均衡的效果。git

簡而言之:github

  • Feign 採用的是基於接口的註解
  • Feign 整合了ribbon

貳、準備工做

新建一個feign子工程lovin-feign-client,用於後面的操做。下面是主要的pom依賴:~~~pom lovincloud com.eelve.lovincloud 1.0-SNAPSHOT 4.0.0 web

<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

/**安全

  • @ClassName SecurityConfig
  • @Description TDO
  • @Author zhao.zhilue
  • @Date 2019/8/16 14:13
  • @Version 1.0
    **/
    @Configuration
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@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

/**網絡

  • @ClassName LovinFeignClientApplication
  • @Description TDO
  • @Author zhao.zhilue
  • @Date 2019/8/15 17:17
  • @Version 1.0
    **/
    @SpringBootApplication
    @EnableFeignClients
    @EnableDiscoveryClient
    @EnableHystrix
    public class LovinFeignClientApplication {
    public static void main(String[] args) {
    SpringApplication.run(LovinFeignClientApplication.class,args);
    }
    }
    ~~~
    • 添加一個遠程調用的服務端FeignRemoteService,而且配置feign調用信息:
      ~~~java
      package com.eelve.lovin.service;

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

  • @ClassName FeignRemoteService
  • @Description TDO
  • @Author zhao.zhilue
  • @Date 2019/8/15 17:18
  • @Version 1.0
    **/
    @FeignClient(value = "lovineurkaclient",fallback = FeignRemoteServiceImpl.class)
    public interface FeignRemoteService {
@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;

/**

  • @ClassName FeignRemoteServiceImpl
  • @Description TDO
  • @Author zhao.zhilue
  • @Date 2019/8/15 17:31
  • @Version 1.0
    **/
    @Component
    public class FeignRemoteServiceImpl implements FeignRemoteService {
    @Override
    public String hello() {
    return "hystrix起做用了";
    }
    }
    ~~~
    • 最後新建FeignController,來消費服務:
      ~~~java
      package com.eelve.lovin.controller;

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;

/**

  • @ClassName FeignController
  • @Description TDO
  • @Author zhao.zhilue
  • @Date 2019/8/15 17:21
  • @Version 1.0
    **/
    @RestController
    public class FeignController {
@Autowired
    FeignRemoteService feignRemoteService;複製代碼
@GetMapping(value = "/getHello")
    public String getHello() {
        return feignRemoteService.hello();
    }
}
~~~
# 叄、啓動測試
- 依次啓動eureka的服務端和兩個客戶端,以及新建的lovin-feign-client
![咱們能夠看到服務已經所有啓動成功](https://i.loli.net/2019/08/23/oJn64HIfmOiVgEP.png)
咱們能夠看到服務已經所有啓動成功
- 而後訪問http://localhost:8806/getHello
![咱們能夠看到已經能夠經過feign調到咱們創建的eureka客戶端了](https://user-gold-cdn.xitu.io/2019/9/4/16cfaf6c1ae610eb?w=1334&h=989&f=png&s=32240)
咱們能夠看到已經能夠經過feign調到咱們創建的eureka客戶端了
- 再次請求接口觀察返回
![咱們能夠看到咱們調到了經過feign調用ribbon負載的另一個接口](https://user-gold-cdn.xitu.io/2019/9/4/16cfaf6c53bbbed1?w=1333&h=833&f=png&s=28951)
咱們能夠看到咱們調到了經過feign調用ribbon負載的另一個接口了,到這裏咱們就已經弄好了一個簡單的ribbon負載。複製代碼

肆、添加Hystrix Dashboard斷路器監控

  • 添加須要的pom依賴
    ~~~pom

    org.springframework.boot
    spring-boot-starter-actuator


    org.springframework.cloud
    spring-cloud-starter-netflix-hystrix-dashboard
    2.1.2.RELEASE

    ~~~
  • 在主類上添加@EnableHystrixDashboard,開啓斷路器監控,而且配置HystrixMetricsStreamServlet
    ~~~java
    package com.eelve.lovin;

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;

/**

  • @ClassName LovinFeignClientApplication
  • @Description TDO
  • @Author zhao.zhilue
  • @Date 2019/8/15 17:17
  • @Version 1.0
    **/
    @SpringBootApplication
    @EnableFeignClients
    @EnableDiscoveryClient
    @EnableHystrix
    @EnableHystrixDashboard
    public class LovinFeignClientApplication {
    public static void main(String[] args) {
    SpringApplication.run(LovinFeignClientApplication.class,args);
    }
@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
![首頁](https://user-gold-cdn.xitu.io/2019/9/4/16cfaf6d1f024c44?w=1912&h=992&f=png&s=186857)
這裏咱們經過首頁能夠看到:
~~~
默認的集羣監控:經過URL http://turbine-hostname:port/turbine.stream開啓,實現對默認集羣的監控。
指定的集羣監控:經過URL http://turbine-hostname:port/turbine.stream?cluster=[clusterName]開啓,實現對clusterName的監控。
單體應用監控:經過URL http://hystrix-app:port/hystrix.stream開啓,實現對某個具體的服務監控
~~~
- 添加監控模式查看詳情,這裏選擇第三個單體應用複製代碼

添加監控接口監控詳情這樣咱們就完成了熔斷器的監控,固然具體含義有待下一步深究。

伍、網絡架構

  • 咱們能夠看到咱們調用的服務再也不是像再上一篇文章中的直接訪問對應的服務,而是經過feign的Ribbon的負載均衡的去調用的,並且這裏說明一點,Ribbon的默認機制是輪詢。
    目前的網絡架構

---

相關文章
相關標籤/搜索