温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringCloud Commons公共抽象方法怎么用

发布时间:2022-04-22 10:19:52 来源:亿速云 阅读:169 作者:iii 栏目:开发技术

今天小编给大家分享一下SpringCloud 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是一个标记界面。

@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不再能通过自动配置来创建,必须通过配置类创建,具体实例如下所示:

@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的列表,如下代码所示:

@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公共抽象方法怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI