1. 程式人生 > >Spring cloud Eureka服務註冊及發現(三)發現使用服務

Spring cloud Eureka服務註冊及發現(三)發現使用服務

Feign是一個宣告式的Web Service客戶端,它使得編寫Web Serivce客戶端變得更加簡單。我們只需要使用Feign來建立一個介面並用註解來配置它既可完成。具體Feign的解釋請看:Spring Cloud Feign詮釋

下面,通過一個例子來展現Feign如何方便的宣告對上述computer-service服務的定義和呼叫。

  • 建立一個Spring Boot工程,配置pom.xml,具體如下:
  <!--  1.引入springCloud parent包的繼承-->
  <parent>
    <groupId>org.springframework.cloud</groupId
>
<artifactId>spring-cloud-starter-parent</artifactId> <version>Brixton.RELEASE</version> <relativePath /> </parent> ...... <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId
>
spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies>
  • 配置 application.yml,設定配置伺服器地址和eureka配置資訊
spring:
  profiles:
    #預設使用下面哪個配置項。啟動專案的時候 可使用 -Dspring.profiles.active=dev 更換使用的配置項
    active: test
   #服務配置資訊
  application:
    name: eurekaFeign
  cloud:
    #配置伺服器的設定資訊
    config:
      #採用的檔案型別 即:生產環境,測試環境。。
      profile: ${spring.profiles.active}
      #配置伺服器的訪問地址
      uri: http://${config.server.hostname}:${config.server.port}
server:
  port: ${port}

#一下資訊 儘量配置到git檔案上,然後通過配置伺服器獲取
eureka:
  client:
    #表示是否註冊自身到eureka伺服器
    registerWithEureka: true
    #是否從eureka伺服器獲取註冊資訊
    fetchRegistry: true
    registry-fetch-interval-seconds: 30
    #開啟客戶端存活狀態監測
    healthcheck:
      enabled: true
    serviceUrl:
      #設定Eureka伺服器地址
      defaultZone: http://${euruka.server.hostname}:${euruka.server.port}/eureka/

#將不同的配置以---分割
---
spring:
  profiles: test
port: 7080
config:
  server:
    hostname: 127.0.0.1
    port: 8888
---
spring:
  profiles: dev
port: 8080
config:
  server:
    hostname: 127.0.0.1
    port: 8888
  • 應用主類中新增@EnableFeignClients註解, 開啟Feign功能:
@SpringBootApplication
//啟用eureka服務的客戶端註解
@EnableDiscoveryClient
//啟用Feign模擬WebService客戶端
@EnableFeignClients
public class EurekaFeign {

    public static void main(String[] args) {
        SpringApplication.run(EurekaFeign.class, args);
    }
}
  • 定義使用computeService服務的介面
//使用Eureka伺服器上註冊標識為computeService的服務
@FeignClient("computeService")
public interface ComputeClient {

    @RequestMapping(method = RequestMethod.GET, value = "/add")
    Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);
}
使用@FeignClient(“compute-service”)註解來繫結該介面對應compute-service服務
通過Spring MVC的註解來配置compute-service服務下的具體實現。
  • 在web層中呼叫上面定義的ComputeClient,具體如下:
@RestController
public class ConsumerController {

    @Autowired
    ComputeClient computeClient;

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public Integer add() {
        return computeClient.add(10, 20);
    }
}
  • 啟動服務程式。

然後,檢視computeService服務控制檯,也輸出了被呼叫的日誌資訊:
這裡寫圖片描述

檢視Eureka控制檯介面 eurekaFeign也註冊到了服務中。
這裡寫圖片描述

我們通過Feign以介面和註解配置的方式,輕鬆實現了對compute-service服務的繫結,這樣我們就可以在本地應用中像本地服務一下的呼叫它。
當部署多臺computeService服務的時候,eurekaFeign不需要做任何修改,就可以訪問computeService中的任意一臺,實現負載均衡。