作者:jiangzz
电话:15652034180
微信:jiangzz_wx
微信公众账号:jiangzz_wy
装うWebサービスクライアントは、宣言です。これは、簡単にWebサービスクライアントを書くことができます。春の雲は、Spring MVCの注釈のサポートを追加し、デフォルトで使用されている同じHttpMessageConverters春のWebを使用しています。春の雲は、リボンとユーレカは、装うを使用しながら、HTTPクライアントをロードバランシングを提供統合されています。
はじめに
- 依存性の導入
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- FeignSpringBootApplication
@SpringBootApplication
@EnableFeignClients
public class FeignSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(FeignSpringBootApplication.class,args);
}
}
- application.properties
eureka.client.register-with-eureka=false
eureka.client.healthcheck.enabled=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://jiangzz:123456@localhost:8761/eureka/
- IUserServiceFeignClient
@FeignClient(name = "USER-SERVICE")
public interface IUserServiceFeignClient {
@RequestMapping(value="/manager/user/{id}",method = {RequestMethod.GET})
public User queyUserById(@PathVariable(value = "id") Integer id);
}
- UserControllerで
@RestController
public class UserController {
@Autowired
private IUserServiceFeignClient userServiceFeignClient;
@RequestMapping(value = "/user/{id}",method={RequestMethod.GET})
public User queryUserById(@PathVariable("id") Integer id){
return userServiceFeignClient.queyUserById(id);
}
}
IUserServiceFeignClient name=USER-SERVICE
属性は、底がインターフェイスメソッドの多くの設定パラメータとなりますユーレカからネーミングサービスを見つける装うために参照するには、単にリボンを使用するよりもエレガント達成するために、このリモートサーバに要求を送信します。パッケージの底面には、実際に装うリボンアセンブリです。
Feigenヒューズ
装うはデフォルト吹き戦略でオンになっていない設定ファイルで指定されたユーザーが必要です
eureka.client.register-with-eureka=false
eureka.client.healthcheck.enabled=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://jiangzz:123456@localhost:8761/eureka/
feign.hystrix.enabled=true
IUserServiceFeignClientを変更し、フェイルバックプロパティを追加
@FeignClient(name = "USER-SERVICE",fallback = UserServiceFailBack.class)
public interface IUserServiceFeignClient {
@RequestMapping(value="/manager/user/{id}",method = {RequestMethod.GET})
public User queyUserById(@PathVariable(value = "id") Integer id);
}
追加UserServiceFailBack
サービス低下を達成するために
@Component
public class UserServiceFailBack implements IUserServiceFeignClient {
@Override
public User queyUserById(Integer id) {
return new User("测试用户",new Date(),true,1500.0);
}
}
ユーザーは、装うが、サービスがシャットダウンされ、参照してみてくださいHTTPをクリックすることができます:// localhostを:8080 /ユーザー/ 1ビューのアクセス結果を
{"id":null,"name":"测试用户","birthDay":"2019-05-30 07:38:21","sex":true,"salary":1500.0}
タイムアウトを設定します
対応するサービス要求時間の構成設定をApplication.proeprties
eureka.client.register-with-eureka=false
eureka.client.healthcheck.enabled=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://jiangzz:123456@localhost:8761/eureka/
feign.client.config.USER-SERVICE.connect-timeout=500
feign.client.config.USER-SERVICE.read-timeout=500
feign.hystrix.enabled=true
システムコールのタイムアウトした場合と、それが自動的に融合します
Hystrixダッシュボード
コンフィギュレーションのpom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- application.properties
eureka.client.register-with-eureka=false
eureka.client.healthcheck.enabled=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://jiangzz:123456@localhost:8761/eureka/
feign.client.config.USER-SERVICE.connect-timeout=500
feign.client.config.USER-SERVICE.read-timeout=500
feign.hystrix.enabled=true
management.endpoints.web.exposure.include=*
- FeignSpringBootApplication
@SpringBootApplication
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
public class FeignSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(FeignSpringBootApplication.class,args);
}
}
設定HyStrixプロパティ
装うと統合した後、ユーザーがComandPropertiesのふりを設定することはできませんが、コンフィギュレーション・ビーンの形で構成することができます
@SpringBootApplication
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
public class FeignSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(FeignSpringBootApplication.class,args);
}
@Bean
public SetterFactory setterFactory(){
SetterFactory setterFactory =new SetterFactory() {
@Override
public HystrixCommand.Setter create(Target<?> target, Method method) {
String groupKey = target.name();
String commandKey = Feign.configKey(target.type(), method);
HystrixCommandProperties.Setter setter = HystrixCommandProperties.Setter()
//设置统计指标5秒为一个时间窗口
.withMetricsRollingStatisticalWindowInMilliseconds(5000)
//操过80%失败率
.withCircuitBreakerErrorThresholdPercentage(50)
//操作5个开启短路器
.withCircuitBreakerRequestVolumeThreshold(5)
//设置线程隔离
.withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE)
//设置断路器的开启时间为5秒
.withCircuitBreakerSleepWindowInMilliseconds(5000);
return HystrixCommand.Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey))
.andCommandPropertiesDefaults(setter);
}
};
return setterFactory;
}
}
効果を確認するために、回路ブレーカと連携!