springcloud集成网关加config
springcloud集成网关加config
1.集成网关创建新模块
2.导入依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.55</version> </dependency>
3.配置yml
eureka: client: serviceUrl: defaultZone: http://peer1:1003/eureka/,http://peer2:1002/eureka/ #注册中心地址 #注册中心地址 server: port: 5000 zuul: ignored-services: "*" #禁止使用服务名字进行访问 routes: order-server: "/order/**" student-server: "/student/**" user-service: "/user/**" prefix: "/servers"
4.主配置类打开网关注解
@SpringBootApplication @EnableCircuitBreaker//开启熔断器 /** * @EnableZuulServer开启网关 */ @EnableZuulProxy public class ZuullApplication5000 { public static void main(String[] args) { SpringApplication.run(ZuullApplication5000.class); } }
5.实现ZuulFilter定义过滤器
@Component public class LoginCheckFilter extends ZuulFilter { public static final String PRE_TYPE = "pre"; //定义优先级数字 越大优先级越高 public static final int FILTER_ORDER = 1; /** * * Filtered prefix * */ @Override public String filterType() { return PRE_TYPE; } /** * The bigger the number, the higher the priority * */ @Override public int filterOrder() { return FILTER_ORDER; } /** * Execute the run method according to the return value type * @return */ @Override public boolean shouldFilter() { /** *Get access path * Determine the request path */ HttpServletRequest request = RequestContext.getCurrentContext().getRequest(); if (request.getRequestURI().contains("login")){ return false; } return true; } @Override public Object run() throws ZuulException { HttpServletRequest request = RequestContext.getCurrentContext().getRequest(); String token = request.getHeader("access-token"); /** * Determines whether the string is empty */ if (!StringUtils.hasLength(token)){ /** *Get the response object */ HttpServletResponse response = RequestContext.getCurrentContext().getResponse(); /** *Response object content Settings */ response.setContentType("test/json,charset=utf-8"); /** * Set the response status encoding */ response.setStatus(HttpStatus.SC_UNAUTHORIZED); /** *Set error message */ Map<String,Object> map = new HashMap<>(); map.put("success", false); map.put("message", "请先登录在进行访问"); /** * The conversion data type is json */ String json = JSON.toJSONString(map); try { response.getWriter().write(json); } catch (IOException e) { e.printStackTrace(); } /** * Stop the release */ RequestContext.getCurrentContext().setSendZuulResponse(false); } return null; }
集成配置中心:配置中心原理图
1.码云创建自己的账号添加配置文件
2.创建config模块
3.导入依赖
<dependencies> <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.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
4.yml配置
eureka: client: serviceUrl: defaultZone: http://peer1:1003/eureka/,http://peer2:1002/eureka/ #注册中心地址 instance: prefer-ip-address: true #使用ip地址注册 instance-id: config-server #指定服务的id server: port: 6002 spring: application: name: config-server cloud: config: server: git: uri: https://gitee.com/wang_yi_mo/springcloud-config.git username: 13699638505 password: 0427java
5.配置类开启配置中心
@SpringBootApplication @EnableDiscoveryClient @EnableConfigServer public class EurekaServerApplication6002 { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication6002.class); } }
6.启动模块
7.测试:http://localhost:6002/application-student-test.yml
8.修改student-server微服务
9.删除student-server层的yml配置
spring: cloud: config: uri: http://localhost:6002 name: application-student #后面会找到那个配置文件自动加入你的环境#-test.yml profile: test #测试环境 label: master
10.开启服务浏览器直接访问