1.添加Feign服务调用及负载均衡

本阶段项目完成总架构图中的第三阶段,添加Feign的内部服务调用及负载均衡。
elm.png

1.1.创建食品服务集群

在父工程下,创建 Maven Module 子工程(工程名:food_server_10201;Packaging:jar)。此工程的内容与food_server_10200工程一致,除了端口号。所以修改food_server_10201工程的application.yml文件:

  1. server:
  2. port: 10201
  3. spring:
  4. application:
  5. name: food-server
  6. #业务配置
  7. datasource:
  8. username: root
  9. password: 123
  10. url: jdbc:mysql://localhost:3306/elm?characterEncoding=utf-8
  11. driver-class-name: com.mysql.jdbc.Driver
  12. #业务配置
  13. logging:
  14. level:
  15. org.springframework: debug
  16. com.neusoft.mapper: debug
  17. #业务配置
  18. mybatis:
  19. mapper-locations: classpath:mapper/*.xml
  20. type-aliases-package: com.neusoft.po
  21. #eureka配置
  22. eureka:
  23. client:
  24. service-url:
  25. #将自己注册给Eureka Server集群
  26. defaultZone: http://eurekaServer13000:13000/eureka,http://eurekaServer13001:13001/eureka
  27. instance:
  28. prefer-ip-address: true #使用ip地址向Eureka注册
  29. instance-id: ${spring.cloud.client.ip-address}:${server.port} #自定义微服务实例ID

工程启动后,在Eureka工作台中检查食品服务集群是否注册成功。

1.2.添加基于Feign的服务调用

在商家微服务(business_server_10300)工程中使用Feign进行服务调用及负载均衡。

1.2.1.导入Feign依赖

在business_server_10300工程中添加Feign的依赖

  1. <!--SpringCloud整合的openFeign -->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-openfeign</artifactId>
  5. </dependency>

1.2.2.主启动类添加Feign注解

在主启动类上添加开启Feign支持的注解

  1. @SpringBootApplication
  2. //开启Spring Cloud Feign的支持功能
  3. @EnableFeignClients
  4. public class MyApplication {
  5. //RestTemplate不需要了
  6. /*
  7. public RestTemplate getRestTemplate() {
  8. return new RestTemplate();
  9. }
  10. */
  11. //... ...
  12. }

1.2.3.配置调用接口

创建在Feign中调用微服务的接口

package com.neusoft.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import com.neusoft.po.CommonResult;

//指定调用的食品微服务名
@FeignClient(name="food-server")
public interface FoodFeignClient {

    @GetMapping("/FoodController/listFoodByBusinessId/{businessId}")
    public CommonResult listFoodByBusinessId(@PathVariable("businessId") Integer businessId);
}

2.2.4.使用Feign接口进行调用

在控制器BusinessController中注入Feign接口,并使用它进行服务调用

@CrossOrigin("*")
@RestController
@RequestMapping("/BusinessController")
public class BusinessController {

    @Autowired
    private BusinessService businessService;
    @Autowired
    private FoodFeignClient foodFeignClient;

    @GetMapping("/listBusinessByOrderTypeId/{orderTypeId}")
    public CommonResult<List> listBusinessByOrderTypeId(@PathVariable("orderTypeId") Integer orderTypeId)
            throws Exception {
        List<Business> list = businessService.listBusinessByOrderTypeId(orderTypeId);
        return new CommonResult(200, "success", list);
    }

    @GetMapping("/getBusinessById/{businessId}")
    public CommonResult<Business> getBusinessById(@PathVariable("businessId") Integer businessId) throws Exception {
        Business business = businessService.getBusinessById(businessId);
        //在商家微服务中调用食品微服务
        CommonResult<List> result = foodFeignClient.listFoodByBusinessId(businessId);
        System.out.println(result.getMessage());
        if (result.getCode() == 200) {
            business.setFoodList(result.getResult());
        }
        return new CommonResult(200, "success", business);
    }
}

2.2.5.测试

启动服务,测试服务调用,同时也可以测试负载均衡(Feign也封装了Ribbon的负载均衡功能)。