SpringCloud分布式系统之Feign负载均衡使用

首先从公共API模块开始,因为此接口会多次或多个元素调用,所以放在公共API最好

公共API:1、修改pom.xml文件,在下面添加以下包

	<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-feign -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-feign</artifactId>
        <version>1.4.6.RELEASE</version>
    </dependency>

公共API:2、在service层添加一个接口

@FeignClient(value="MICROSERVICECLOUD-DEPT")		//这个是在eureka已注册的微服务名称
public interface ApiContentClientService {
    @RequestMapping("/api_content/select")		//调用微服务的接口
    GavinApiContent selectApiContent(@RequestParam String id);
}

公共API已经配置完成,可以开始调用它!
首先创建microservicecloud-consumer-feign模块

microservicecloud-consumer-feign模块:1、修改pom.xml文件,在下面添加以下包

	<dependencies>
		<!--引入公共API模块-->
        <dependency>
            <groupId>com.kinglove.project</groupId>
            <artifactId>microservicecloud-api</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-ribbon -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
            <version>1.4.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-feign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
    </dependencies>

microservicecloud-consumer-feign模块:2、YAML就按照80客户端模块来改就行了!

microservicecloud-consumer-feign模块:3、配置Controller,调用刚刚写的公共API接口

@RestController
@RequestMapping("/api_content")
public class ApiContentController {

    @Autowired
    ApiContentClientService apiContentClientService;	//公共API接口

    @RequestMapping(value="select",produces = "application/json;charset=utf-8")
    GavinApiContent selectApiContent(@RequestParam String id){
        return apiContentClientService.selectApiContent(id);
    }
}

microservicecloud-consumer-feign模块:4、编写启动类,ApplicationFeign

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.kinglove.project"})
@MapperScan({"com.kinglove.project.mapper"})
public class ApplcationFeign {
    public static void main(String[] args) {
        SpringApplication.run(ApplcationFeign.class,args);
    }
}

在这里提示一下:microservicecloud-consumer-feign模块的包路径最好跟公共API模块包路径一样,这样Spring扫描包的时候才会扫到,否则会出现错误

比如:

  • 公共API模块包路径:com.kinglove.project
  • 公共API模块包路径:com.kinglove.project

启动,就能看到,它默认的是Ribbon的默认算法轮询,也可以修改其均衡方式,参考https://blog.csdn.net/qq_34355079/article/details/103382260

因为Feign包括了Ribbon负载均衡,所以功能会更强大

你可能感兴趣的:(SpringCloud分布式系统之Feign负载均衡使用)