SpringCloud之Feign(客户端负载均衡)

释放双眼,带上耳机,听听看~!

1、Feign是一个声明式的web服务客户端,使得编写web服务客户端变得非常容易;只需要创建一个接口,然后在上面添加注解即可;

2、引入Ribbon依赖:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
1<dependencies>
2       <dependency><!-- 自己定义的api -->
3           <groupId>com.atguigu.springcloud</groupId>
4           <artifactId>microservicecloud-api</artifactId>
5           <version>${project.version}</version>
6       </dependency>
7       <dependency>
8           <groupId>org.springframework.cloud</groupId>
9           <artifactId>spring-cloud-starter-feign</artifactId>
10      </dependency>
11      <!-- Ribbon相关 -->
12      <dependency>
13          <groupId>org.springframework.cloud</groupId>
14          <artifactId>spring-cloud-starter-eureka</artifactId>
15      </dependency>
16      <dependency>
17          <groupId>org.springframework.cloud</groupId>
18          <artifactId>spring-cloud-starter-ribbon</artifactId>
19      </dependency>
20      <dependency>
21          <groupId>org.springframework.cloud</groupId>
22          <artifactId>spring-cloud-starter-config</artifactId>
23      </dependency>
24      <dependency>
25          <groupId>org.springframework.boot</groupId>
26          <artifactId>spring-boot-starter-web</artifactId>
27      </dependency>
28      <!-- 修改后立即生效,热部署 -->
29      <dependency>
30          <groupId>org.springframework</groupId>
31          <artifactId>springloaded</artifactId>
32      </dependency>
33      <dependency>
34          <groupId>org.springframework.boot</groupId>
35          <artifactId>spring-boot-devtools</artifactId>
36      </dependency>
37  </dependencies>
38

2、配置application.yml文件:


1
2
3
4
5
6
7
8
9
10
11
1server:
2  port: 80
3feign:
4  hystrix:
5    enabled: true
6eureka:
7  client:
8    register-with-eureka: false
9    service-url:
10      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/  
11

3、启动类开启注解(@EnableFeignClients)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1import org.springframework.boot.SpringApplication;
2import org.springframework.boot.autoconfigure.SpringBootApplication;
3import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
4import org.springframework.cloud.netflix.feign.EnableFeignClients;
5import org.springframework.context.annotation.ComponentScan;
6
7@SpringBootApplication
8@EnableEurekaClient
9@EnableFeignClients(basePackages= {"com.atguigu.springcloud"})
10@ComponentScan("com.atguigu.springcloud")
11public class DeptConsumer80_Feign_App
12{
13  public static void main(String[] args)
14  {
15      SpringApplication.run(DeptConsumer80_Feign_App.class, args);
16  }
17}
18
19

4、编写配置类,注入:RestTemplate


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1import org.springframework.cloud.client.loadbalancer.LoadBalanced;
2import org.springframework.context.annotation.Bean;
3import org.springframework.context.annotation.Configuration;
4import org.springframework.web.client.RestTemplate;
5import com.netflix.loadbalancer.IRule;
6import com.netflix.loadbalancer.RetryRule;
7
8@Configuration
9public class ConfigBean //boot -->spring   applicationContext.xml --- @Configuration配置   ConfigBean = applicationContext.xml
10{
11  @Bean
12  @LoadBalanced//Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端       负载均衡的工具。
13  public RestTemplate getRestTemplate()
14  {
15      return new RestTemplate();
16  }
17 
18}
19

5、定义DeptClientService接口:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
1@FeignClient(value = "MICROSERVICECLOUD-DEPT",fallbackFactory=DeptClientServiceFallbackFactory.class)
2public interface DeptClientService
3{
4   @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
5   public Dept get(@PathVariable("id") long id);
6
7   @RequestMapping(value = "/dept/list", method = RequestMethod.GET)
8   public List<Dept> list();
9
10  @RequestMapping(value = "/dept/add", method = RequestMethod.POST)
11  public boolean add(Dept dept);
12}
13
14

6、编写DeptController_Consumer:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
1import java.util.List;
2import org.springframework.beans.factory.annotation.Autowired;
3import org.springframework.web.bind.annotation.PathVariable;
4import org.springframework.web.bind.annotation.RequestMapping;
5import org.springframework.web.bind.annotation.RestController;
6import com.atguigu.springcloud.entities.Dept;
7import com.atguigu.springcloud.service.DeptClientService;
8
9@RestController
10public class DeptController_Consumer
11{
12  @Autowired
13  private DeptClientService service;
14
15  @RequestMapping(value = "/consumer/dept/get/{id}")
16  public Dept get(@PathVariable("id") Long id)
17  {
18      return this.service.get(id);
19  }
20
21  @RequestMapping(value = "/consumer/dept/list")
22  public List<Dept> list()
23  {
24      return this.service.list();
25  }
26
27  @RequestMapping(value = "/consumer/dept/add")
28  public Object add(Dept dept)
29  {
30      return this.service.add(dept);
31  }
32}
33

 

给TA打赏
共{{data.count}}人
人已打赏
安全经验

Google Adsense 技巧提示100条

2021-10-11 16:36:11

安全经验

安全咨询服务

2022-1-12 14:11:49

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索