<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          SpringCloud微服務(wù)框架Fegin負(fù)載均衡

          共 8222字,需瀏覽 17分鐘

           ·

          2021-04-03 20:58

          Springcloud框架的Ribbon負(fù)載均衡

          添加相關(guān)的pom依賴

          <dependencies>
                  <dependency>
                      <groupId>com.yang</groupId>
                      <artifactId>microservicecloud-api</artifactId>
                      <version>1.0-SNAPSHOT</version>
                  </dependency>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-web</artifactId>
                  </dependency>
                  <!-- 修改后立即生效,熱部署 -->
                  <dependency>
                      <groupId>org.springframework</groupId>
                      <artifactId>springloaded</artifactId>
                  </dependency>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-devtools</artifactId>
                  </dependency>


                  <!-- Ribbon相關(guān) -->
                  <dependency>
                      <groupId>org.springframework.cloud</groupId>
                      <artifactId>spring-cloud-starter-eureka</artifactId>
                  </dependency>
                  <dependency>
                      <groupId>org.springframework.cloud</groupId>
                      <artifactId>spring-cloud-starter-config</artifactId>
                  </dependency>

                  <dependency>
                      <groupId>org.springframework.cloud</groupId>
                      <artifactId>spring-cloud-starter-feign</artifactId>
                  </dependency>

              </dependencies>

          創(chuàng)建配置類

          package com.yang.springcloud.config;


          import com.netflix.loadbalancer.IRule;
          import com.netflix.loadbalancer.RetryRule;
          import org.springframework.cloud.client.loadbalancer.LoadBalanced;
          import org.springframework.context.annotation.Bean;
          import org.springframework.context.annotation.Configuration;
          import org.springframework.web.client.RestTemplate;

          @Configuration
          public class ConfigBean
          {
              @Bean
              @LoadBalanced
              public RestTemplate getRestTemplate()
              
          {
                  return new RestTemplate();
              }
              @Bean
              public IRule myRule()
              
          {
                  //return new RoundRobinRule();
                  //return new RandomRule();//達(dá)到的目的,用我們重新選擇的隨機(jī)算法替代默認(rèn)的輪詢。
                  return new RetryRule();
              }
          }

          創(chuàng)建Controller層

          package com.yang.springcloud.controller;

          import java.util.List;

          import com.yang.entity.Dept;
          import com.yang.service.DeptClientService;
          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.web.bind.annotation.PathVariable;
          import org.springframework.web.bind.annotation.RequestMapping;
          import org.springframework.web.bind.annotation.RestController;

          @RestController
          public class DeptController_Feign
          {
              @Autowired
              private DeptClientService service = null;

              @RequestMapping(value = "/consumer/dept/get/{id}")
              public Dept get(@PathVariable("id") Long id)
              
          {
                  return this.service.get(id);
              }

              @RequestMapping(value = "/consumer/dept/list")
              public List<Dept> list()
              
          {
                  return this.service.list();
              }

              @RequestMapping(value = "/consumer/dept/add")
              public Object add(Dept dept)
              
          {
                  return this.service.add(dept);
              }
          }

          創(chuàng)建啟動(dòng)類

          package com.yang.springcloud;

          import org.springframework.boot.SpringApplication;


          import org.springframework.boot.autoconfigure.SpringBootApplication;
          import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
          import org.springframework.cloud.netflix.feign.EnableFeignClients;
          import org.springframework.context.annotation.ComponentScan;


          @SpringBootApplication
          @EnableEurekaClient
          @EnableFeignClients(basePackages= {"com.yang"})
          @ComponentScan("com.yang")
          public class DeptConsumer80_Feign_App {

              public static void main(String[] args)
              
          {
                  SpringApplication.run(DeptConsumer80_Feign_App.classargs);
              }
          }

          創(chuàng)建application.yml配置類

          server:
            port: 8080

          eureka:
            client:
              register-with-eureka: false
              service-url:
                defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

          在microservicecloud-api 定義service接口

          package com.yang.service;

          import java.util.List;

          import com.yang.entity.Dept;
          import org.springframework.cloud.netflix.feign.FeignClient;
          import org.springframework.web.bind.annotation.PathVariable;
          import org.springframework.web.bind.annotation.RequestMapping;
          import org.springframework.web.bind.annotation.RequestMethod;



          @FeignClient(value = "MICROSERVICECLOUD-DEPT")
          public interface DeptClientService
          {
              @RequestMapping(value = "/dept/get/{id}",method = RequestMethod.GET)
              public Dept get(@PathVariable("id") long id);

              @RequestMapping(value = "/dept/list",method = RequestMethod.GET)
              public List<Dept> list();

              @RequestMapping(value = "/dept/add",method = RequestMethod.POST)
              public boolean add(Dept dept);
          }


             

          如果你覺得這篇內(nèi)容對(duì)你挺有啟發(fā),我想邀請(qǐng)你幫我三個(gè)小忙:

          • 點(diǎn)個(gè)【在看】,或者分享轉(zhuǎn)發(fā),讓更多的人也能看到這篇內(nèi)容

          • 關(guān)注公眾號(hào)【園碼生活】,不定期分享原創(chuàng)&精品技術(shù)文章。

          歡迎評(píng)論區(qū)留下你的精彩評(píng)論~          
                   

          覺得文章不錯(cuò)可以分享到朋友圈讓更多的小伙伴看到哦~

          客官!在看一下唄          


          瀏覽 65
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  免费在线观看黄 | 一级精品A片 | 精品一线| 国产做爰视频免费播放 | 国产视频久久 豆花 |