OpenFeign客户端调用
1.简介:
OpenFeign声明式的webService客户端. Feign可以与Eureka和Ribbon组合以支持负载均衡。使用方法是定义一个接口并在上面添加注解。
2.代码
这里的EurekaMain7001和EurekaMain7002是两个Eureka server. PaymentMain8001和PaymentMain8002注册到Eureka。 通过FeignOrderMain80对PaymentMain8001和PaymentMain8002进行调用。
创建FeignOrderMain80工程:
pom:
<dependencies> <!-- open feign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!--eureka client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>com.shishi.springcloud</groupId> <artifactId>cloud-api-common</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.shishi.springcloud</groupId> <artifactId>cloud-provider-payment8001</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies>
yml:
server: port: 80 spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://localhost:3306/db2021?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root eureka: client: register-with-eureka: false service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
#这个yml是不需要写datasource的,但是老是报错数据库连接源没配置, 查了一下是说引入了Mybatis的包,但是加了@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}) 也没用,不引入数据库连接的依赖也没用。所以暂时就这样了。
service层:
@Component @FeignClient(value = "CLOUD-PAYMENT-SERVICE") public interface PaymentFeignService { @GetMapping(value="/payment/get/{id}") public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id); }
controller层:
@RestController public class PaymentFeignController { @Resource private PaymentFeignService paymentFeignService; @GetMapping("/consumer/payment/get/{id}") public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) { return paymentFeignService.getPaymentById(id); } }
主程序:
@SpringBootApplication @EnableFeignClients public class FeignOrderMain80 { public static void main(String[] args) { SpringApplication.run(FeignOrderMain80.class, args); } }
3.测试: