第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

第五節(jié) 微服務(wù)OTRS 補(bǔ)充客戶端負(fù)載均衡

標(biāo)簽:
Kubernetes

客户端客户端负载均衡使用

spring could在客户端负载均衡有两种选择一种是ribbon+restTemplate,另一种是feign。本主要讲解下基于ribbon+rest。当然feign(Feign默认集成了ribbon)也会简单介绍。

  1. ribbon+restTemplate实现方式
    首先需要在项目中添加pom文件中添加依赖

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
  1. 在工程的配置文件指定服务的注册中心地址为同上节eureka client配置相同 ,例如:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/server:
  port: 8764spring:
  application:
    name: service-ribbon
  1. 在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean: restTemplate;并通过@LoadBalanced注解表明restRemplate开启负载均衡的功能。
    例如在api-server项目启动类如下

  @SpringBootApplication@EnableEurekaClient@EnableCircuitBreaker@EnableResourceServer@Configuration@ComponentScan({"com.packtpub.mmj.api.service", "com.packtpub.mmj.common"})public class ApiApp {  //................................省略
    @LoadBalanced
    @Bean
    RestTemplate restTemplate() {        return new RestTemplate();
    }    public static void main(String[] args) {
        LOG.info("Register MDCHystrixConcurrencyStrategy");
        HystrixPlugins.getInstance().registerConcurrencyStrategy(new MDCHystrixConcurrencyStrategy());
        SpringApplication.run(ApiApp.class, args);
    }
}
  • 在使用restTemplate向其他服务发送请求就会通过eureka server 通过负载策略代理到相应的服务上。

  • 注意:ribbon中会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名。所以使用的url要用服务名称而不是IP:port这种形式

比如下例子:

@Autowired
    RestTemplate restTemplate;    public String hiService(String name) {        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
    }
  1. Feign是一个声明式的伪Http客户端,使用Feign,只需要创建一个接口并注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡。

  • 使用的第一步添加依赖

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
  • 第二步 工程配置application.yml,和上述ribbon相同 略

  • 第三步 启动类加上@EnableFeignClients注解开启Feign的功能,例如:

@SpringBootApplication@EnableEurekaClient@EnableDiscoveryClient@EnableFeignClientspublic class AppService{    public static void main(String[] args) {
        SpringApplication.run( AppService.class, args );
    }
}
  • 第四步 使用
    只需要定义一个接口,通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了restaurant-service服务的“/v1/restaurants”,(spring 会替我们实现有@ FeignClient的接口)代码如下:

//定义@FeignClient("restaurant-service")interface RestaurantClient {    @RequestMapping(method = RequestMethod.GET, value = "/v1/restaurants")    Collection<Restaurant> getRestaurants(@RequestParam("name") String name);
}//使用@Autowired
    private RestaurantClient restaurantClient;// 调用即可
  • 总结: 上述两种方式均为客户端服务在调用,其他服务的请求会先通过eureka server查找已注册的服务根据相应的负载策略,转到具体的服务



作者:勃列日涅夫
链接:https://www.jianshu.com/p/ce2a316ec248


點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消