使用zuul配置网关,拦截请求,解决跨域问题,过滤请求信息
下面是原有代码加上zuul
服务工程(主要用于注册eureka)
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import org.springframework.context.annotation.ComponentScan; /** * Created by 79782 on 2018/6/11. */ @ComponentScan(basePackages = { "com.yf.controller" }) @SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { //new SpringApplicationBuilder(EurekaApplication.class).web(true).run(args); //或者pom引spring-boot-starter-web,然后用下面这个启动 SpringApplication.run(EurekaApplication.class, args); } }
spring.application.name=eureka-server server.port=8899 #强制不注册到注册服务器 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false #注册中心地址 eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/ #驱逐下线的服务,间隔,5秒,默认是60,建议开发和测试环境配置 #org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean.evictionIntervalTimerInMs eureka.server.evictionIntervalTimerInMs=5000
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.test</groupId> <artifactId>erueka</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> </project>
2.服务:
package com.yf.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.List; /** * Created by 79782 on 2018/6/11. */ @RestController public class FirstServer { @Value("${server.port}") private String serverPort; @RequestMapping("/getMenberAll") public List<String> getMenberAll(){ List<String> listUser = new ArrayList<String>(); listUser.add("zhangsan"); listUser.add("lisi"); listUser.add("wangwu"); listUser.add("serverPort"+serverPort); return listUser; } @RequestMapping("/getMemberService") public String getOederService(){ return "this is member"; } }
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import org.springframework.context.annotation.ComponentScan; /** * Created by 79782 on 2018/6/11. */ @ComponentScan(basePackages = { "com.yf.controller" }) @SpringBootApplication @EnableEurekaClient public class EurekaApplication { public static void main(String[] args) { //new SpringApplicationBuilder(EurekaApplication.class).web(true).run(args); //或者pom引spring-boot-starter-web,然后用下面这个启动 SpringApplication.run(EurekaApplication.class, args); } }
eureka: client: serviceUrl: defaultZone: http://localhost:8899/eureka/ server: port: 8763 spring: application: name: service-member
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.yf</groupId> <artifactId>eruekaService</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> </project>
3.消费服务
package com.yf.controller; import com.yf.service.OrderMemberService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.annotation.Order; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * Created by 79782 on 2018/6/11. */ @RestController public class OrderMemberController { @Autowired private OrderMemberService orderMemberService; @RequestMapping("/orderGetService") public List<String> orderGetService(){ System.out.println("order use memeber...."); return orderMemberService.getOrderUserAll(); } @RequestMapping("/getOederService") public String getOederService(){ return "this is order"; } }
package com.yf.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import java.util.List; /** * Created by 79782 on 2018/6/11. */ @Service public class OrderMemberService { @Autowired private RestTemplate restTemplate; public List<String> getOrderUserAll(){ return restTemplate.getForObject("http://service-member/getMenberAll",List.class); } }
package com.yf; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.web.client.RestTemplate; /** * Created by 79782 on 2018/6/11. */ @ComponentScan(basePackages = { "com.yf" }) @SpringBootApplication @EnableEurekaClient public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } @Bean @LoadBalanced //支持负载均衡 RestTemplate restTemplate(){ return new RestTemplate(); } }
eureka: client: serviceUrl: defaultZone: http://localhost:8899/eureka/ server: port: 8764 spring: application: name: service-order
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.yf</groupId> <artifactId>erueka-client</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <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> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> </dependencies> </project>
4.配置zuul
package com.yf.controller; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import java.io.IOException; /** * Created by 79782 on 2018/6/11. */ @Component public class MyFilter extends ZuulFilter { @Override public String filterType() { return "pre"; } @Override public int filterOrder() { return 0; } @Override public boolean shouldFilter() { return true; } @Override public Object run() { RequestContext ctx= RequestContext.getCurrentContext(); HttpServletRequest request=ctx.getRequest(); Object accessToken=request.getParameter("token"); if(accessToken!=null){ return null; } ctx.setSendZuulResponse(false); ctx.setResponseStatusCode(401); try { ctx.getResponse().getWriter().write("token is empty"); } catch (IOException e) { e.printStackTrace(); } return null; } }
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.context.annotation.ComponentScan; /** * Created by 79782 on 2018/6/11. */ @ComponentScan(basePackages = { "com.yf.controller" }) @SpringBootApplication @EnableEurekaClient @EnableZuulProxy public class EurekaApplication { public static void main(String[] args) { //new SpringApplicationBuilder(EurekaApplication.class).web(true).run(args); //或者pom引spring-boot-starter-web,然后用下面这个启动 SpringApplication.run(EurekaApplication.class, args); } }
eureka: client: serviceUrl: defaultZone: http://localhost:8899/eureka/ server: port: 8769 spring: application: name: server-zuul zuul: routes: api-a: path: /api-member/** service-id: service-member api-b: path: /api-order/** service-id: service-order
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.yf</groupId> <artifactId>service-zuul</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <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> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> </dependencies> </project>