eureka和feign的使用


  1 eureka和feign的簡介(copy來的)

  eureka:Eureka是Netflix開發的服務發現組件,本身是一個基於REST的服務。Spring Cloud將它集成在其子項目spring-cloud-netflix中,以實現Spring Cloud的服務發現功能。

  feign:Feign是一個聲明式的Web服務客戶端,使用Feign可使得Web服務客戶端的寫入更加方便。 它具有可插拔注釋支持,包括Feign注解和JAX-RS注解、Feign還支持可插拔     編碼器和解碼器、Spring Cloud增加了對Spring MVC注釋的支持,並HttpMessageConverters在Spring Web中使用了默認使用的相同方式。Spring Cloud集成了Ribbon        和Eureka,在使用Feign時提供負載平衡的http客戶端。

2 eureka的服務端

  2.1 pom.xml中導入相應的依賴

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 
</dependency>

  2.2 在配置文件中編碼相關配置

#指定端口
server:
  port: 7001
#創建eureka實例
eureka:
  instance:
    hostname: localhost
  client:
    #不允許服務端向自己注冊
    register-with-eureka: false
    fetch-registry: false
    #客戶端注冊時需要使用的地址
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

  2.3 在主類上添加相關注解

3 服務提供者(feign)

  3.1 pom.xml中導入相關依賴

       <!--eureka的客戶端依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
          <!--feign的依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>        

  3.2 在配置文件中編碼相關配置

#配置端口
server:
  port: 7003
#注冊到eureka客戶端的名字
spring:
  application:
    name: feign-client
#eureka相關實例
eureka:
  instance:
    lease-expiration-duration-in-seconds: 30
    lease-renewal-interval-in-seconds: 10
    #在eureka中顯示ip地址
    prefer-ip-address: true
    ip-address: 127.0.0.1
  client:
    registerWithEureka: true
    fetchRegistry: true
    #eureka服務端的地址
    serviceUrl:
      defaultZone: http://localhost:7001/eureka/

  3.3 在主類上添加相關依賴

  

  3.4 需要被服務消費者調用的方法(例)

  

@RestController
public class UserController {
    @GetMapping("/user")
    public String user(@PathVariable("id") Integer id){
        return "測試成功啦啦啦啦啦啦"+id;
    }
}

4 服務消費者(feign)

  4.1 在pom.xml 中添加相關依賴

       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>    

  4.2 在配置文件中編碼相關配置

server:
  port: 7002
ribbon:
  ReadTimeout: 60000
  ConnectTimeout: 60000
eureka:
  instance:
    lease-expiration-duration-in-seconds: 30
    lease-renewal-interval-in-seconds: 10
    prefer-ip-address: true
    ip-address: 127.0.0.1
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://localhost:7001/eureka/
spring:
  application:
    name: eureka_client

  4.3 在主類上添加相關注解

  4.4 服務消費者的feign端(即為一個接口,通過此接口與另一個服務建立連接)

@Component
@FeignClient(value = "FEIGN-CLIENT",name = "FEIGN-CLIENT")
public interface Feign {

    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public String user(@PathVariable("id") Integer id);

}

  4.5 服務消費端調用此接口的方法

@RestController
public class UserController {
    @Autowired
    private Feign feign;

    @GetMapping("/user/{id}")
    public String user(@PathVariable("id") Integer id){
        return feign.user(id);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM