这一篇依赖前面的SpringCloud的服务发现的服务,因此首先需要在Discovery Service服务发现那一篇中的创建Discovery Server服务
1.创建gateway-service服务
//导入包,实际是通过Spring Initializr导入的,Eureka Discovery,Zuul,Actuactor
spring-cloud-config-server
spring-cloud-starter-netflix-eureka-server
spring-boot-starter-actuator
//配置启动类注解EnableZuulProxy,EnableDiscoveryClient
@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class GatewayServiceApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServiceApplication.class, args);
}
}
2.gateway-service配置文件
spring.application.name=gateway-service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
3.创建hello-service服务
//导入包,实际是通过Spring Initializr导入的,Eureka Discovery,Spring Web
spring-cloud-starter-netflix-eureka-server
spring-boot-starter-web
//配置启动类注解EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class HelloServiceApplication {
public static void main(String[] args) {
SpringApplication.run(HelloServiceApplication.class, args);
}
@RequestMapping
public String hello(){
return "Hello!";
}
}
4.hello-service配置文件
spring.application.name=hello
server.port=1111
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
5.创建goodbye-service服务
//导入包,实际是通过Spring Initializr导入的,Eureka Discovery,Spring Web
spring-cloud-starter-netflix-eureka-server
spring-boot-starter-web
//配置启动类注解EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class GoodbyeServiceApplication {
public static void main(String[] args) {
SpringApplication.run(GoodbyeServiceApplication.class, args);
}
@RequestMapping
public String goodbye(){
return "Hello!";
}
}
6.goodbye-service配置文件
spring.application.name=goodbye
server.port=2222
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
7.路由,分别转发到两个不同的service中
访问http://localhost:8080/goodbye
输出Googbye!
输出Hello!
8.在gateway- service中增加ZuulFilter
public class AddRequestHeaderFilter extends ZuulFilter {
@Override
public String filterType() {
//包括pre,post,route,error
return "pre";
}
@Override
public int filterOrder() {
//设置filter的优先级顺序,多个filter时按照顺序排列
return 0;
}
@Override
public boolean shouldFilter() {
//true表示执行该filter,false表示不执行该filter
return true;
}
//运行filter时的处理
@Override
public Object run() throws ZuulException {
RequestContext cts = RequestContext.getCurrentContext();
cts.addZuulRequestHeader("x-location","China");
return null;
}
}
@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
@Configuration
public class GatewayServiceApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServiceApplication.class, args);
}
@Bean
public AddRequestHeaderFilter addRequestHeaderFilter(){
return new AddRequestHeaderFilter();
}
}
9.goodbye service中启动类增加接参数处理
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class GoodbyeServiceApplication {
public static void main(String[] args) {
SpringApplication.run(GoodbyeServiceApplication.class, args);
}
@RequestMapping
public String goodbye(@RequestHeader("x-location")String location){
return "Goodbye from "+ location +"!";
}
}
9.即可以通过http://localhost:8080/goodbye
输出:
Goodbye from China!
网友评论