赞
踩
Feign提供声明式的远程调用,借用动态代理实现远程调用,使编写Web服务客户端变得更容易。 Spring Cloud增加了对Spring MVC注释的支持,并使用了Spring Web中默认使用的相同HttpMessageConverters。 Spring Cloud集成了Ribbon和Eureka,在使用Feign时提供负载均衡的http客户端。
server.port=8088
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
spring.security.user.name=mask
spring.security.user.password=111111
@SpringBootApplication
@EnableEurekaServer
public class EurekaApp{
public static void main(String[] args) {
SpringApplication.run(EurekaApp.class,args);
}
}
spring.application.name=USER-SERVICE
eureka.instance.instance-id=001
eureka.instance.prefer-ip-address=true
eureka.instance.lease-expiration-duration-in-seconds=20
eureka.instance.lease-renewal-interval-in-seconds=10
eureka.client.register-with-eureka=true
eureka.client.healthcheck.enabled=true
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://mask:111111@localhost:8088/eureka/
@SpringBootApplication
@EnableEurekaClient
@RestController
@RequestMapping("main")
public class EurekaApp {
public static void main(String[] args) {
SpringApplication.run(EurekaApp.class,args);
}
@RequestMapping(value = "index", method = {RequestMethod.GET})
public String index() {
return "服务A";
}
}
server.port=8081
spring.application.name=USER-SERVICE
eureka.instance.instance-id=002
eureka.instance.prefer-ip-address=true
eureka.instance.lease-expiration-duration-in-seconds=20
eureka.instance.lease-renewal-interval-in-seconds=10
eureka.client.register-with-eureka=true
eureka.client.healthcheck.enabled=true
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://mask:111111@localhost:8088/eureka/
@SpringBootApplication
@EnableEurekaClient
@RestController
@RequestMapping("main")
public class EurekaApp {
public static void main(String[] args) {
SpringApplication.run(EurekaApp.class,args);
}
@RequestMapping(value = "index", method = {RequestMethod.GET})
public String index() {
return "服务B";
}
}
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
创建接口 UserServiceController
@FeignClient(name = "USER-SERVICE") //name为服务名
public interface UserServiceController {
@GetMapping("main/index") //该方法映射的路径
String index();
}
server.port=8089
ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://mask:111111@localhost:8088/eureka/
@SpringBootApplication @RestController @EnableFeignClients public class EurekaSpringBootConsumer { public static void main(String[] args) { SpringApplication.run(EurekaSpringBootConsumer.class,args); } @Autowired private UserServiceController userServiceController; @GetMapping("/rpc/test") public String index() { String index = userServiceController.index(); System.out.println(index); return "OK"; } }
访问http://localhost:8089/rpc/test
底层会根据接口方法的配置信息参数发送请求给远程的服务器这种实现比单纯的使用Ribbon更加的优雅。实际上Feign底层就是对Ribbon组件的封装。
注:在Fegin里不能传递自定义类型至GET查询参数
默认Feign没有开启熔断策略,需要用户在配置文件中指定
feign.hystrix.enabled=true
@FeignClient(name = "USER-SERVICE", fallback = UserServiceFailBack.class) //name为服务名
public interface UserServiceController {
@GetMapping("main/index") //该方法映射的路径
String index();
}
@Component
public class UserServiceFailBack implements UserServiceController{
@Override
public String index() {
return "熔断";
}
}
@RestController
@RequestMapping("main")
public class TestController {
@RequestMapping(value = "index", method = {RequestMethod.GET})
public String index() {
int i = 1/0;
return "服务B";
}
}
启动服务多次访问http://localhost:8089/rpc/test如下图可看出服务B抛出异常后执行熔断后代码
feign.client.config.USER-SERVICE.connect-timeout=500
feign.client.config.USER-SERVICE.read-timeout=500
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
management.endpoints.web.exposure.include=*
@EnableHystrixDashboard
@EnableCircuitBreaker
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。