Feign
创建一个SpringBoot基础工程,取名feigen-counsumer。并在pom文件中引入必要的依赖内容
<!-- 引入父工程--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <projece.reporting.outputEncoding>UTF-8</projece.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.SR2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- eureka的依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.1.6.RELEASE</version> </dependency> <!-- feign的依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.2.1.RELEASE</version> </dependency> </dependencies>
在引导类上添加@EnableFeignClients注解开启SpringCloud Feignd的支持
//@SpringBootApplication //@EnableDiscoveryClient //@EnableCircuitBreaker //熔断 @SpringCloudApplication //组合注解 熔断+负载+boot启动 @EnableFeignClients //启用feign组件 public class ConsumerApplication { //远程服务间的调用 /* 使用feigen解决 @Bean @LoadBalanced //开启负载均衡组件 public RestTemplate restTemplate(){ return new RestTemplate(); }*/ public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class,args); } }
定义HelloService接口,通过@FeignClient注解指定服务名绑定服务
@FeignClient("service-provider") public interface HelloService { @RequestMapping("/hello") String Hello(); }
创建一个ConsumerController来实现对Feign客户端的调用,使用@Autowired直接注入上面的helloService实例
@RestController public class ConsumerControler { @Autowired HelloService helloService; @RequestMapping(value = "/feign-consumer",method = RequestMethod.GET) public String helloConsumer(){ return helloService.Hello(); } }
修改默认配置:端口号,微服务名称,注册给eureka
server: port: 9001 spring: application: name: feign-consumer eureka: client: service-url: defaultZone: http://localhost:10086/eureka register-with-eureka: true #true注册给eureka
测试:
发送几次get请求Http:localhost:9001/feign-consumer,正确返回“hello World”
Zuul
创建一个SpringBoot基础工程,取名SpringCloud-Zuul。并在pom文件中引入必要的依赖内容
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> <version>2.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies>
在引导类上添加@EnableZuulProxy注解开启SpringCloud Zuul的支持
@SpringCloudApplication @EnableZuulProxy //启动zuul组件 public class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class,args); } }
创建LoginFilter继承自ZuulFilter ,添加拦截器的业务代码
@Component public class LoginFilter extends ZuulFilter { /** * 过滤器的类型 pre route post error * @return */ @Override public String filterType() { return "pre"; } /** * 执行顺序 返回值越小,优先级越高 * @return */ @Override public int filterOrder() { return 10; } /** * 是否执行run方法, true * @return */ @Override public boolean shouldFilter() { return true; } /** * 编写过滤器的业务逻辑 * @return * @throws ZuulException */ @Override public Object run() throws ZuulException { //初始化context上下文对象 RequestContext context = RequestContext.getCurrentContext(); //获取request对象 HttpServletRequest request = context.getRequest(); //获取参数 String token = request.getParameter("token"); //拦截 if (StringUtils.isBlank(token)){ context.setSendZuulResponse(false); //提示信息 context.setResponseStatusCode(HttpStatus.SC_UNAUTHORIZED); //设置相应的提示 context.setResponseBody("requst error! "); } //返回值为null,就代表该过滤器什么都不作 return null; } }
修改默认配置:端口号,微服务名称,注册给eureka
server: port: 10010 spring: application: name: dynamic-zuul zuul: routes: service-provider: /service-provider/** #路由名称,可以随便写 第三种配置 # path: /service-provider/** 第一种配置 # url: http://localhost:8889 serviceId: service-provider # 第二种配置 host: connect-timeout-millis: 3000 socket-timeout-millis: 3000 eureka: client: service-url: defaultZone: http://localhost:10086/eureka
测试:通过网关访问--URL不含token,包含token,打开的界面不一致
个人总结
Eureka
注册中心:eureka-server
1.引入启动器
2配置spring.application.name=dynamic.eureka
3.在引导类上添加@EnableEurekaServer
客户端:server-provider,server-consumer
1.引入启动器
2 配置spring.application.name,eureka.service-url.defaultZone=http://localhost:10086/eureka
3.@EnableDiscoveryClient(启动eureka客户端)
Feign
1.引入openFeign启动器
2.在引导类上加上@EnableFeignClients
3.自定义接口,通过@FeignClient注解指定服务名绑定服务
4.更改默认配置
Zuul
1.引入Zuul启动器
2.启动类上添加@EnableZuulProxy注解
3.创建过滤器,继承ZuulFilter基类,重写四个方法
4.配置路由 zuul.router. service-provider: /service-provider/**