Ribbon学習応用

3775 ワード

何も言っていないような気がしますが、ソースコードはまだ見ていません.基本的な浅い原理はみんな知っていると思います.直接応用しましょう.
1サービスプログラム呼び出し側にribbon依存を追加
 
            org.springframework.cloud
            spring-cloud-starter-netflix-ribbon
        

2 RestTemplateを使用してプログラムの呼び出しを行い、戻り構成クラスを作成する
@Configuration
public class BeanConfigration {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return  new RestTemplate();
    }

}

3、RestTemplateでプログラムの呼び出しを行う
@RestController
@RequestMapping
public class TestController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("ckientOneName")
    public String getClinetOneProjectName(){
        for(int i = 0; i <10; i++){
            String str=restTemplate.getForObject("http://client03/test/projectName",String.class);
            System.out.println(str);
        }

        return "ok";
    }

}

4実行結果
client03:8912
client03:8911
client03:8912
client03:8911
client03:8912
client03:8911
client03:8912
client03:8911
client03:8912
client03:8911

5、飢餓ロードを開く.この主な目的は、サービスが多すぎて、ネットワークが悪い場合、最初のロードがタイムアウトする問題を解決することである.
ribbon:
  eager-load:
    enabled: true    #      
    clients: client03 #          

コードが表示されると、オンになります.
c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client client03 initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=client03,current list of Servers=[10.3.135.143:8911, 10.3.135.143:8912],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone;	Instance count:2;	Active connections count: 0;	Circuit breaker tripped count: 0;	Active connections per server: 0.0;]
},Server stats: [[Server:10.3.135.143:8912;	Zone:defaultZone;	Total Requests:0;	Successive connection failure:0;	Total blackout seconds:0;	Last connection made:Thu Jan 01 08:00:00 CST 1970;	First connection made: Thu Jan 01 08:00:00 CST 1970;	Active Connections:0;	total failure count in last (1000) msecs:0;	average resp time:0.0;	90 percentile resp time:0.0;	95 percentile resp time:0.0;	min resp time:0.0;	max resp time:0.0;	stddev resp time:0.0]
, [Server:10.3.135.143:8911;	Zone:defaultZone;	Total Requests:0;	Successive connection failure:0;	Total blackout seconds:0;	Last connection made:Thu Jan 01 08:00:00 CST 1970;	First connection made: Thu Jan 01 08:00:00 CST 1970;	Active Connections:0;	total failure count in last (1000) msecs:0;	average resp time:0.0;	90 percentile resp time:0.0;	95 percentile resp time:0.0;	min resp time:0.0;	max resp time:0.0;	stddev resp time:0.0]
]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@1511d157

6、注意第三歩では、私たちが取った方法はgetForObjectであり、パラメータを伝達していない.実は方法の名前から分かるように、私たちが採用したgetの方式で要求を行うので、パラメータを伝達する時に2つの形式があり、1つはつなぎ合わせで、1つはrestfu風格のurlの後で直接パラメータを伝達する.同様に、サービスに多くのパラメータが必要な場合、post方式で要求することができます.postForObjectで、何も言うことはありません.自分で見るとできます.
7、ロード・バランシング・ポリシーを選択し、デフォルトのポーリング・メカニズムをランダム・メカニズムに変更し、プロファイルに構成を追加する.
client03: #        
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
client03:8911
client03:8912
client03:8911
client03:8911
client03:8911
client03:8911
client03:8911
client03:8912
client03:8911
client03:8912

8、コードで構成することもでき、以下のように構成クラスを作成することを考えている.
@Configuration
public class RibbonRuleStrategy{

    @Bean
    public Rule rule(){
    
        return new Rule();
    }

}

起動クラスに次の注記を追加します.
@RibbonClient(name="    ",configuration="      ")
public class RibbonClientConfig{
}