SpringCloud使用(后半部)

1.Feign

简介:

Feign也叫伪装: Feign可以把Rest的请求进行隐藏,伪装成类似SpringMVC的Controller一样。你不用再自己拼接url,拼接参数等等 操作,一切都交给Feign去做。

1.1Feign的客户端

@FeignClient("user-service")
public interface UserClient {
@GetMapping("/user/{id}")
User queryById(@PathVariable("id") Long id);
}

注意:

  • @FeignClient ,声明这是一个Feign客户端,同时通过 value 属性指定服务名称
  • 这是一个接口,Feign会通过动态代理,帮我们生成实现类。

然后在注入使用这个接口:

@RestController
@RequestMapping("/cf")
public class ConsumerFeignController {
@Autowired
private UserClient userClient;
@GetMapping("/{id}")
public User queryById(@PathVariable Long id){
return userClient.queryById(id);
}
}

开启Feign功能(在启动类):

@SpringCloudApplication
@EnableFeignClients//开启Feign功能
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}

}

1.2Hystrix支持

和默认集成Ribbon一样,Feign也默认集成了Hystrix

默认情况下他是关闭的,需要在配置文件中配置:

feign:
hystrix:
enabled: true 

接下来实现一下:

首先定义一个类,实现Feign客户端,作为fallback的处理类

@Component
public class UserClientFallback implements UserClient {
@Override
public User queryById(Long id) {
User user = new User();
user.setId(id);
user.setName("用户异常");
return user;
}
}

然后在UserFeignClient中,指定刚才编写的实现类:

@FeignClient(value = "user-service", fallback = UserClientFallback.class)
public interface UserClient {
@GetMapping("/user/{id}")
User queryById(@PathVariable("id") Long id);
}

2.SpringCloudGateway

使用SpringCloudGateway:

编写启动类:

@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}

编写配置:

server:
port: 10010
spring:
application:
name: api-gateway
cloud:
gateway:
routes:
# 路由id,可以随意写
- id: user-service-route
# 代理的服务地址
uri: http://127.0.0.1:9091
# 路由断言,可以配置映射路径
predicates:
- Path=/a/**
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8001/eureka
instance:
prefer-ip-address: true

也就是说,我将路径中包含有 /a/** 开头的请求,都代理到http://127.0.0.1:9091

假如我访问http://localhost:10010/a/8, 就是访问http://localhost:9091/a/8,当然这个路径是不存在的啊,

把他http://localhost:10010/user/8就好,就是http://localhost:9091/user/8,也访问我的服务了,能够返回一个User。

但是这样是写死了路径,可以吧http://127.0.0.1:9091换成服务名这样写更好一点,这就是从Eureka获取服务的地址信息了。

server:
port: 10010
spring:
application:
name: api-gateway
cloud:
gateway:
routes:
# 路由id,可以随意写
- id: user-service-route
# 代理的服务地址;lb表示从eureka中获取具体服务
uri: lb://user-service
# 路由断言,可以配置映射路径
predicates:
- Path=/user/**
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8001/eureka
instance:
prefer-ip-address: true

 

路由前缀:

在gateway中可以通过配置路由的过滤器PrefixPath,实现映射路径中地址的添加;

server:
port: 10010
spring:
application:
name: api-gateway
cloud:
gateway:
routes:
# 路由id,可以随意写
- id: user-service-route
# 代理的服务地址;lb表示从eureka中获取具体服务
uri: lb://user-service
# 路由断言,可以配置映射路径
predicates:
- Path=/**
filters:
# 添加请求路径的前缀
- PrefixPath=/user
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8001/eureka
instance:
prefer-ip-address: true

这就是把前缀加到你写的路径前面

如http://localhost:10010/8 --》http://localhost:9091/user/8

当然有添加前缀,也有去除前缀

server:
port: 10010
spring:
application:
name: api-gateway
cloud:
gateway:
routes:
# 路由id,可以随意写
- id: user-service-route
# 代理的服务地址;lb表示从eureka中获取具体服务
uri: lb://user-service
# 路由断言,可以配置映射路径
predicates:
- Path=/api/user/**
filters:
# 表示过滤1个路径,2表示两个路径,以此类推
- StripPrefix=1
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8001/eureka
instance:
prefer-ip-address: true

GateWay跨域配置:

说到跨域配置,顺便提一嘴跨域:

地址不一样是跨域,端口号不一样也算跨域,域名不一样也算跨域。

在访问Spring Cloud Gateway网关服务器的时候,出现跨域问题的话;可以在网关服务器中通过配置解决,允许哪 些服务是可以跨域请求的;具体配置如下:

spring:
cloud:
gateway:
globalcors:
corsConfigurations:
'[/**]':
#allowedOrigins: * # 这种写法或者下面的都可以,*表示全部
allowedOrigins:
- "http://docs.spring.io"
allowedMethods:
- GET

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值