好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

超详细讲解SpringCloud Commons公共抽象的用法

本期主角——Spring Cloud Commons:公共抽象

Spring Cloud Commons公共抽象

Spring Cloud将服务发现、负载均衡和断路器等通用模型封装在一个公共抽象中,可以被所有的Spring Cloud客户端使用,不依赖于具体的实现(例如服务发现就有Eureka和Consul等不同的实现),这些公共抽象位于Spring Cloud Commons项目中。

@EnableDiscoveryClient

Commons提供@EnableDiscoveryClient注释。这通过META-INF/spring.factories查找DiscoveryClient接口的实现。Discovery Client的实现将在org.springframework.cloud.client.discovery.EnableDiscoveryClient键下的spring.factories中添加一个配置类。DiscoveryClient实现的示例是Spring Cloud Netflix Eureka,Spring Cloud Consul发现和Spring Cloud Zookeeper发现。

默认情况下,DiscoveryClient的实现将使用远程发现服务器自动注册本地Spring Boot服务器。可以通过在@EnableDiscoveryClient中设置autoRegister=false来禁用此功能。

服务注册ServiceRegistry

Commons现在提供了一个ServiceRegistry接口,它提供了诸如register(Registration)和deregister(Registration)之类的方法,允许您提供定制的注册服务。Registration是一个标记界面。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

@Configuration

@EnableDiscoveryClient (autoRegister= false )

public class MyConfiguration {

     private ServiceRegistry registry;

 

     public MyConfiguration(ServiceRegistry registry) {

         this .registry = registry;

     }

     // called via some external process, such as an event or a custom actuator endpoint

     public void register() {

         Registration registration = constructRegistration();

         this .registry.register(registration);

     }

}

每个ServiceRegistry实现都有自己的Registry实现。

RestTemplate的负载均衡

创建RestTemplate实例的时候,使用@LoadBalanced注解可以将RestTemplate自动配置为使用负载均衡的状态。@LoadBalanced将使用Ribbon为RestTemplate执行负载均衡策略。

创建负载均衡的RestTemplate不再能通过自动配置来创建,必须通过配置类创建,具体实例如下所示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

@Configuration

public class MyConfiguration {

@LoadBalanced

@Bean

RestTemplate restTemplate(){

return new RestTemplate():

}

}

public class MyApplication {

@Autowired

private RestTemplate restTemplate ;

public string getMyApplicationName() {

//使用restTemplate访问my-application微服务的/name接口

string name = restTemplate.getFor0bject( "http://my-application/name" ,string. class ) ;

return name;

}

}

URI需要使用服务名来指定需要访问应用服务,Ribbon客户端将通过服务名从服务发现应用处获取具体的服务地址来创建一个完整的网络地址,以实现网络调用。

RestTemplate的失败重试

负载均衡的RestTemplate可以添加失败重试机制。默认情况下,失败重试机制是关闭的,启用方式是将Spring Retry添加到应用程序的类路径中。还可以设置

spring.cloud.loadbalancer.retry.enabled=false禁止类路径中Spring retry的重试逻辑。

如果想要添加一个或者多个RetryListener到重试请求中,可以创建一个类型为LoadBalancedRetryListenerFactory的Bean,用来返回将要用于重试机制的RetryListener的列表,如下代码所示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

@Configuration

public class RryListenerConfiguration {

@Bean

LoadBalancedRetryListenerFactory retryListenerFactory( {

return new LoadBalancedRetryListenerFactoryO {

@override

public RetryListener[] createRetryListeners (String service)

return new RetryListener[] { new RetryListener ( {

@Override

//重试开始前的工作

public <T,E extends Throwable> boolean open(RetryContext context,RetryCallback<T,E>callback){

return true ;

}

//重试结束后的工作@Override

public <T, E extends Throwable> void close(RetryContext context,RetryCallback<T,E>callback,Throwable throwable){

}

//重试出错后的工作@Override

publicT,E extends Throwable> void onError(RetryContext context,RetryCal1back<T,E>callback,Throwable throwable){

}

}};

}};

}}

其中,自定义配置类中定义了生成LoadBalancedRetryListenerFactory实例的@Bean方法,该工厂类的createRetryListeners方法会生成一个RetryListener实例,用于进行网络请求的重试。

到此这篇关于超详细讲解SpringCloud Commons 公共抽象的用法的文章就介绍到这了,更多相关SpringCloud 公共抽象内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://jianshen.blog.csdn.net/article/details/124174031

查看更多关于超详细讲解SpringCloud Commons公共抽象的用法的详细内容...

  阅读:10次