基于上一节项目,先把2个two后缀的项目删除了,让在创建一个子项目business-client-feign项目,子项目创建过程就不说了,之前章节有说过。
先在pom文件中导入包,这里spring-cloud-starter-openfeign就是feign要使用的jar包,其它跟business-client项目一样
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-cloudartifactId> <groupId>com.ydgroupId> <version>1.0-SNAPSHOTversion> parent> <modelVersion>4.0.0modelVersion> <artifactId>business-client-feignartifactId> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-netflix-hystrixartifactId> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-openfeignartifactId> dependency> dependencies>project>
然后配置application.yml文件这里几乎跟business-client一样就改了端口和服务名
server: port: 8904spring: application: name: business-client-feigneureka: client: service-url: defaultZone: http://127.0.0.1:8901/eureka # 获取服务地址列表间隔时间,默认30秒 registry-fetch-interval-seconds: 5hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 2000 circuitBreaker: errorThresholdPercentage: 50 # 触发熔断错误比例阈值,默认值50% sleepWindowInMilliseconds: 10000 # 熔断后休眠时长,默认值5秒 requestVolumeThreshold: 10 # 熔断触发最小请求次数,默认值是20
在启动类上添加feign注解@EnableFeignClients
package com.yd;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.openfeign.EnableFeignClients;/** * @Author: zengyz * @Date: 2020/11/16 10:01 */@SpringBootApplication/*DiscoveryClient使用 */@EnableDiscoveryClient@EnableCircuitBreaker@EnableFeignClientspublic class BusinessClientFeignApplication { public static void main(String[] args) { SpringApplication.run(BusinessClientFeignApplication.class); }}
这时创建一个client的接口类,主要作用是封装接口调用,这样就不用想business-client项目一样去拼接接口地址了
package com.yd.controller.client;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;/** * @Author: zengyz * @Date: 2020/11/16 10:00 */@FeignClient("business-service")public interface UserClient { @GetMapping("/user/test") public String queryList();}
然后在创建一个控制类,在类中注入创建的接口类,这样就能对外提供接口,内部调用接口了
package com.yd.controller;import com.yd.controller.client.UserClient;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @Author: zengyz * @Date: 2020/11/16 9:59 */@RestController@RequestMapping("/cf")public class UserEntityFeignController { @Autowired private UserClient userClient; @GetMapping("/test") public String test(){ return userClient.queryList(); }}
使用feign时就不需要再启动类添加restTemplate实例化了,也可实现均衡负载,因为Feign内置ribbon。

项目business-service的application.yml配置文件中把端口设置成动态的

这样如果business-service需要启动多个时只有配置启动类设置不同的端口就可以了,下面是idea的设置过程,先打开图1界面,然后掉价edit configurations,打开图2 Run/Debug configurations 窗口这里就可以看到business-service的启动类配置选择,点击复制图标就可以生成一个启动类配置设置在端口就可以了,如图3



本文基于上一节项目,介绍Idea搭建Spring Cloud项目的步骤。先删除two后缀项目,创建子项目business - client - feign,在pom文件导入包、配置application.yml,在启动类添加feign注解,创建接口类和控制类。还提到使用feign的优势及设置business - service端口动态的方法。
2059

被折叠的 条评论
为什么被折叠?



