:から参照牙Zhipengコラム
1.リボン
1.1リボンの紹介
1.2サービスコンシューマを作成します。
プロジェクトの記事に基づいて、この記事では、ユーレカ・サーバープロジェクトを開始し、ポートは8761であり、プロジェクト開始サービス-HI、ポートは8762です。ポートサービス-HIのプロフィールは、8763に変更され、開始します。次に、あなたが見つける:サービス-HIは、小規模なクラスタに相当する、ユーレカ・サーバー2つのインスタンスを登録しました。<?= "1.0"エンコード= "UTF-8" XMLバージョン?> < プロジェクトのxmlns = "http://maven.apache.org/POM/4.0.0" のxmlns:XSI = "http://www.w3.org / 2001 / XMLスキーマ・インスタンス」 のxsi:のschemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 </ modelVersion > < groupIdを> com.zang </ groupIdを> < たartifactId >サービス・リボン</ たartifactId > <</ バージョン> < パッケージ> JAR </ パッケージ> < 名前>サービス・リボン</ 名前> < 説明>春ブーツ用デモプロジェクト</ 記述> < 親> < groupIdを> com.zang </ groupIdを> < たartifactId > SC-F-リボン</ たartifactId > < バージョン> 0.0.1-SNAPSHOT </ バージョン> </ 親> < 依存性> < 依存> < groupIdを> org.springframework.cloud </ groupIdを> < たartifactId >春・クラウド・スターター・ネットフリックス-ユーレカ・クライアント</ たartifactId > </ 依存関係> < 依存> < groupIdを> org.springframework.boot </ groupIdを> < たartifactId >春・ブート・スターター・ウェブ</ たartifactId > </ 依存> < 依存性> < のgroupId > org.springframework.cloud </ のgroupId > < たartifactId >ばねクラウドスタータNetflixのリボン</ たartifactId > </ 依存> </ 依存関係> </ プロジェクト>
// localhostを:8761 /ユーレカ/、サービス・リボン、8764ポートのためのプログラムのプログラム名でhttpプロジェクト設定ファイルの指定サービスのアドレス登録センターで。次のようにapplication.ymlプロフィール:
ユーレカ:
クライアント:
serviceURLの:
defaultZoneます。http:// localhost:8761 /ユーレカ/
サーバー:
ポート:8764
春:
アプリケーション:
名前:サービス-リボン
起動クラスプロジェクト、@EnableDiscoveryClientによってサービスセンターへの登録(使用する他のレジストリに適し@EnableEurekaClient持つ関数);およびプログラムIOC豆を注入する:restTemplate;及び@LoadBalancedアノテーションによって、このrestRemplateオープンロード・バランシングを示し機能。
輸入org.springframework.boot.SpringApplication。 輸入org.springframework.boot.autoconfigure.SpringBootApplication。 輸入org.springframework.cloud.client.discovery.EnableDiscoveryClient; 輸入org.springframework.cloud.client.loadbalancer.LoadBalanced; 輸入org.springframework.cloud.netflix.eureka.EnableEurekaClient; 輸入org.springframework.context.annotation.Bean; 輸入org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient パブリック クラスServiceRibbonApplication { 公共の 静的な 無効メイン(文字列[]引数){ SpringApplication.run(ServiceRibbonApplication。クラス、引数)。 } @Bean @LoadBalanced RestTemplate restTemplate(){ 戻り 新しい)(RestTemplateします。 } }
我々のプログラムの名前を持つ特定のURLアドレスの直接交換、それはリボンにサービス名に基づいています「/ HI」のインターフェース、前restTemplate IoCコンテナを注入することにより、サービス-hiがサービスを消費するために、テストクラスHelloServiceを書きます特定のサービス・インスタンスの選択、うち特定のサービス名のURLの交換要求サービスインスタンスに応じては、次の通り:
輸入org.springframework.beans.factory.annotation.Autowired; 輸入org.springframework.stereotype.Service。 輸入org.springframework.web.client.RestTemplate; @Service パブリック クラスHelloService { @Autowired RestTemplate restTemplate。 公共の文字列hiService(文字列名){ リターン restTemplate.getForObject( "のhttp:// SERVICE-HI / HI名=?" +名前、文字列。クラス); } }
次のように書き込み制御、呼制御部は、HelloServiceの方法を使用しました:
輸入com.zang.serviceribbon.service.HelloService。 輸入org.springframework.beans.factory.annotation.Autowired; 輸入org.springframework.web.bind.annotation.GetMapping。 輸入org.springframework.web.bind.annotation.RequestParam; 輸入org.springframework.web.bind.annotation.RestController; @RestController パブリック クラスHelloControler { @Autowired HelloService helloService。 (値@GetMapping = "/ HI" ) パブリック文字列HI(@RequestParam文字列名){ 戻りhelloService.hiService(名)。 } }
ブラウザでの訪問は、http何度:// localhostの:? 8764 / HI名=奘、ブラウザを交互:
このショーそのときに我々をrestTemplate.getForObjectを呼び出して(「のhttp:// SERVICE-HI / HI名=?」+名、String.class)メソッドは、インスタンスのポートのために行われ、ロードバランシング、別のサービスへのアクセスをされています。
このアーキテクチャでは1.3
- サービスレジストリ、ユーレカ・サーバ、ポート8761
- 2つのインスタンスのためのサービス-HIプロジェクトRANは、それぞれ、8762,8763ポートがサービスレジストリに登録されました
- サービスレジストリに登録さsercviceリボンポート8764、
- sercviceリボンコールサービス-HIリボンでバランスされて負荷以来restTemplate HIインタフェースによっては、ターンは、サービス-hiが呼び出すと:8762及びHI 8763 2ポート・インターフェースを、
2.装います
2.1について装います
装うは、簡単のHttpクライアントを作成せ、擬似的なHTTPクライアント宣言型です。装うを使用して、あなただけがインターフェイスや注釈を作成する必要があります。それは装うJAX-RS注釈やメモをプラグイン可能な注釈機能を使用することができました。プラグ可能なエンコーダおよびデコーダをふり。装うデフォルトでは、リボン、ユーレカと組み合わせて、負荷分散のデフォルトの実装を統合しました。
要するに:
- 装うは、アノテーションベースのインタフェースを使用しています
- 装うは、ロードバランシング機能と、リボンを統合しました
- 統合Hystrix、ヒューズの能力を持っています
装うサービスを作成する2.2
、エンジニアリングを過ごすユーレカ・サーバを起動し、ポート8761を継続し、サービス開始-HI二回、ポートは8762、8773です。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zang</groupId> <artifactId>service-feign</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>service-feign</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.zang</groupId> <artifactId>sc-f-feign</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <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.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
在工程的配置文件application.yml文件,指定程序名为service-feign,端口号为8765,服务注册地址为http://localhost:8761/eureka/ ,代码如下:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8765
spring:
application:
name: service-feign
在程序的启动类ServiceFeignApplication ,加上@EnableFeignClients注解开启Feign的功能:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient @EnableFeignClients public class ServiceFeignApplication { public static void main(String[] args) { SpringApplication.run( ServiceFeignApplication.class, args ); } }
定义一个feign接口,通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了service-hi服务的“/hi”接口,代码如下:
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(value = "service-hi") public interface SchedualServiceHi { @RequestMapping(value = "/hi",method = RequestMethod.GET) String sayHiFromClientOne(@RequestParam(value = "name") String name); }
在Web层的controller层,对外暴露一个”/hi”的API接口,通过上面定义的Feign客户端SchedualServiceHi 来消费服务。代码如下:
import com.zang.servicefeign.clients.SchedualServiceHi; 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; @RestController public class HiController { //编译器报错,无视。 因为这个Bean是在程序启动的时候注入的,编译器感知不到,所以报错。 @Autowired SchedualServiceHi schedualServiceHi; @GetMapping(value = "/hi") public String sayHi(@RequestParam String name) { return schedualServiceHi.sayHiFromClientOne( name ); } }
启动程序,多次访问http://localhost:8765/hi?name=zang,浏览器交替显示: