其实就是对ribbon的进一步封装,为了开发者更方便的调用。功能和ribbon是一样的。
fetchRegistry 这个属性在Eureka的默认配置是true
怎么是使用openfeign
呢?
pom文件
<?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>cloud</artifactId>
<groupId>com.home</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>open80</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>
mybatis-spring-boot-starter
</artifactId>
</dependency>
<dependency>
<artifactId>common</artifactId>
<groupId>com.home</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--<dependency>-->
<!--<groupId>com.alibaba</groupId>-->
<!--<artifactId>druid-spring-boot-starter</artifactId>-->
<!--<version>1.1.9</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.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
yml文件 注意我们不注册进eureka了。
server:
port: 80
spring:
application:
name: open
# datasource.type=com.alibaba.druid.pool.DruidDataSource
# spring.datasource.url = jdbc:mysql://127.0.0.1:3306/demo
# spring.datasource.username = root
# spring.datasource.password = 123456
# spring.datasource.driverClassName = com.mysql.jdbc.Driver
# datasource:
# type:
eureka:
client:
register-with-eureka: false
# fetch-registry: false
service-url:
# defaultZone: http://localhost:7001/eureka
defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka
主启动类 加入了一个Enable 的注解。ribbon还是按照之前的方法使用就行了。
package home;
import my.bal;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
@EnableFeignClients
@RibbonClient(name = "PAY-SERVICE",configuration = bal.class) //也就是用这个注解引入这个bean 单独使用。
public class Open80 {
public static void main(String[] args) {
SpringApplication.run(Open80.class, args);
}
}
openfei方式调用需要的接口 接口的写法就是你要调用的controller的前边copy过来就行了。 在这个接口上要加入两个注解。就OK啦。
package home.service;
import home.entity.MyResult;
import home.entity.Pay;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@Component
@FeignClient(value = "PAY-SERVICE")
public interface FiegnService {
@PostMapping(value = "/pay/create")
public MyResult cre(@RequestBody Pay pay);
@GetMapping(value = "pay/get/{id}")
public MyResult get(@PathVariable(value = "id") int id);
}
controller的写法。 其实就是像以前service的写法一样就行了。为了就是用这种你熟悉的方式。因为集成了ribbon所以默认就有负载均衡。
package home.controller;
import com.netflix.discovery.converters.Auto;
import home.entity.MyResult;
import home.entity.Pay;
import home.lb.Lb;
import home.service.FiegnService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@Slf4j
public class OpenCon {
@Autowired
FiegnService fiegnService;
@GetMapping(value = "/order/create")
public MyResult create(Pay pay) {//pay/create
log.info(pay + "");
return fiegnService.cre(pay);
}
@GetMapping(value = "/order/get/{id}")
public MyResult get(@PathVariable(value = "id") int id) {
return fiegnService.get(id);
}
}
openfeign的请求默认连接和readtimeout
都是1秒
所以我们如果有服务比较慢,可以设置超时时间
在客户端80 的yml中设置最后三行,ribbon的配置是比较特殊的。一定要仔细。
server:
port: 80
spring:
application:
name: open
# datasource.type=com.alibaba.druid.pool.DruidDataSource
# spring.datasource.url = jdbc:mysql://127.0.0.1:3306/demo
# spring.datasource.username = root
# spring.datasource.password = 123456
# spring.datasource.driverClassName = com.mysql.jdbc.Driver
# datasource:
# type:
eureka:
client:
register-with-eureka: false
# fetch-registry: false
service-url:
# defaultZone: http://localhost:7001/eureka
defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka
ribbon:
ConnectTimeout: 60000
ReadTimeout: 60000
以上是设置超时时间
我们还要设置日志,方便调试。
第一设置日志级别:一共四种,可以看看。
package home.config;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeiCfg {
@Bean
public Logger.Level level(){
return Logger.Level.FULL;
}
}
第二在yml中设置打印
ribbon:
ConnectTimeout: 60000
ReadTimeout: 60000
logging:
level:
home.service.FiegnService: debug
就OK了