Feignの簡単な紹介
5213 ワード
1、Feign概要
Feignは宣言的なWebサービスクライアントであり、Feignを使用すると、Webサービスクライアントの書き込みがより容易になります.Feign注釈とJAX-RS注釈を含む挿抜可能な注釈サポート、Feignはまた、挿抜可能エンコーダとデコーダ、Spring CloudはSpring MVC注釈のサポートを追加し、HttpMessageConvertersはSpring Webでデフォルトで使用される同じ方法を使用しています.Spring CloudにはRibbonとEurekaが統合されており、Feignを使用すると負荷バランスのとれたhttpクライアントが提供されます.
2、Feignの簡単な使用
Feignを使用する.ビルダーパッケージSDK
FeignはFeignを提供する.ビルダー()クライアントの構築方法で、他のサービスに依存せずにリモートのURLに簡単にアクセスできます.この方式の使い方は,リモートのURLを純粋なインタフェースSDKにカプセル化し,他のユーザやシステムが使用できるようにすることである.1.mvn依存を追加し、Feign依存のみを使用すればよい
2.FeignClientインタフェース呼び出しRESTFULインタフェースの定義
3.パッケージSDK
SpringCloudおよびEurekaサービス管理の使用
この方法では、SpringCloudコンポーネントの依存および注釈を追加する必要があり、Springのコンテナ管理にも依存する必要があります.1.依存の追加
2.定義UserFeignClientインタフェースがSDKと異なるのは、インタフェースにFeignClientおよびComponentの注記が必要であることである.@Componentの意味は、コンポーネントをSpringCloudに登録し、Springコンテナの管理を受け、依存注入を使用してインタフェースのエンティティを得ることです.@FeignClient注記では「name」と「url」を指定できます.プログラムがEurekaクライアントの場合、nameをサービス登録センターの名前に指定すると、サービス登録センターに行ってアクセスするhostアドレスを検索します.
3.UserFeignClientにアクセスするコントロールを定義する
Feignのこの2つの使い方は、どちらもいいです.
3、FeignとHystrixの関係:
Feignのデフォルトは統合Hystrixです.Hystrixを解除するには、feignは2つの方法を提供します.
(1)、アプリケーションによる.ymlにこれを追加します.
feign.hystrix.Enabled:false##はfeignのhystrixサポートをいっそ無効にします.これで全部Hystrix禁止
(2)、feignは単一Clientに対する禁止を提供し、クラスを構成することによって実現され、以下のコードを見る.
対応するコンフィギュレーション2類は以下の通りである.
説明:Feign.builderのデフォルトはHystrixFeignBuilderです.Builderに変更する必要がある場合は、Hystrixを禁止します.
Feignは宣言的なWebサービスクライアントであり、Feignを使用すると、Webサービスクライアントの書き込みがより容易になります.Feign注釈とJAX-RS注釈を含む挿抜可能な注釈サポート、Feignはまた、挿抜可能エンコーダとデコーダ、Spring CloudはSpring MVC注釈のサポートを追加し、HttpMessageConvertersはSpring Webでデフォルトで使用される同じ方法を使用しています.Spring CloudにはRibbonとEurekaが統合されており、Feignを使用すると負荷バランスのとれたhttpクライアントが提供されます.
2、Feignの簡単な使用
Feignを使用する.ビルダーパッケージSDK
FeignはFeignを提供する.ビルダー()クライアントの構築方法で、他のサービスに依存せずにリモートのURLに簡単にアクセスできます.この方式の使い方は,リモートのURLを純粋なインタフェースSDKにカプセル化し,他のユーザやシステムが使用できるようにすることである.1.mvn依存を追加し、Feign依存のみを使用すればよい
io.github.openfeign
feign-core
9.5.0
2.FeignClientインタフェース呼び出しRESTFULインタフェースの定義
public interface FeignClient {
@RequestLine("GET /user/login")
public String login(@Param("username") String username, @Param("password") String password);
}
3.パッケージSDK
public class SDK{
public static void login(String username,String password){
FeignClient feignClient = Feign.builder()
.target(FeignClient.class, "http://localhost");
feignClient.login(username,password);
}
/** */
public static void main(String[] args) {
login("lsz","lsz");
}
}
SpringCloudおよびEurekaサービス管理の使用
この方法では、SpringCloudコンポーネントの依存および注釈を追加する必要があり、Springのコンテナ管理にも依存する必要があります.1.依存の追加
org.springframework.cloud
spring-cloud-starter-feign
org.springframework.cloud
spring-cloud-starter-zuul
org.springframework.boot
spring-boot-starter-test
test
dependency>
org.springframework
spring-web
4.3.10.RELEASE
org.springframework.cloud
spring-cloud-dependencies
Dalston.SR2
pom
import
2.定義UserFeignClientインタフェースがSDKと異なるのは、インタフェースにFeignClientおよびComponentの注記が必要であることである.@Componentの意味は、コンポーネントをSpringCloudに登録し、Springコンテナの管理を受け、依存注入を使用してインタフェースのエンティティを得ることです.@FeignClient注記では「name」と「url」を指定できます.プログラムがEurekaクライアントの場合、nameをサービス登録センターの名前に指定すると、サービス登録センターに行ってアクセスするhostアドレスを検索します.
@Component //springboot
@FeignClient(name="user-server",url = "http://localhost:8011")
public interface UserFeignClient {
@RequestMapping("/{id}")
public User findByIdFeign(@RequestParam("id") Long id);
}
3.UserFeignClientにアクセスするコントロールを定義する
@Component
public class UserServiceImpl implements UserService {
@Autowired
private UserFeignClient userFeignClient;
@Override
public User findByIdFeign(String id) {
User object = this.userFeignClient.findByIdFeign("1111");
return object;
}
}
Feignのこの2つの使い方は、どちらもいいです.
3、FeignとHystrixの関係:
Feignのデフォルトは統合Hystrixです.Hystrixを解除するには、feignは2つの方法を提供します.
(1)、アプリケーションによる.ymlにこれを追加します.
feign.hystrix.Enabled:false##はfeignのhystrixサポートをいっそ無効にします.これで全部Hystrix禁止
(2)、feignは単一Clientに対する禁止を提供し、クラスを構成することによって実現され、以下のコードを見る.
package com.itmuch.cloud.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.itmuch.config.Configuration2;
@FeignClient(name = "xxxx", url = "http://localhost:8761/", configuration = Configuration2.class, fallback = FeignClient2Fallback.class)
public interface FeignClient2 {
@RequestMapping(value = "/eureka/apps/{serviceName}")
public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName") String serviceName);
}
対応するコンフィギュレーション2類は以下の通りである.
package com.itmuch.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import feign.Feign;
import feign.auth.BasicAuthRequestInterceptor;
@Configuration
public class Configuration2 {
@Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("user", "password123");
}
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder();
}
}
説明:Feign.builderのデフォルトはHystrixFeignBuilderです.Builderに変更する必要がある場合は、Hystrixを禁止します.