美文网首页小卜java
Spring Cloud基础之Intelligent Routi

Spring Cloud基础之Intelligent Routi

作者: 汤太咸啊 | 来源:发表于2021-12-12 21:18 被阅读0次

这一篇依赖前面的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!

访问http://localhost:8080/hello

输出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!

相关文章

网友评论

    本文标题:Spring Cloud基础之Intelligent Routi

    本文链接:https://www.haomeiwen.com/subject/sdldfrtx.html