【SpringCloud NetFlix】 网关Zuul(二)集群中使用Zuul

【SpringCloud NetFlix】 网关Zuul(二)集群中使用Zuul_第1张图片

【SpringCloud NetFlix】 网关Zuul(二)集群中使用Zuul_第2张图片

建立集群项目
  • Eureka服务器
  • 服务提供者
  • 服务调用者
  • 网关
Eureka服务器
依赖
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-configartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eureka-serverartifactId>
        dependency>
    dependencies>
主启动类
@SpringBootApplication
@EnableEurekaServer
public class ServerApp {

    public static void main(String[] args) {
        new SpringApplicationBuilder(ServerApp.class).web(true).run(args);
    }

}
Application.yml
server:
  port: 8761
eureka:
  client: 
    register-with-eureka: false
    fetch-registry: false
服务提供者
依赖
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-configartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
        dependency>
实体类
public class Member {

    private Integer id;
    private String name;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}
rest服务接口
@RestController
public class MainController {

    @RequestMapping(value = "/member/{id}", method = RequestMethod.GET, 
            produces = MediaType.APPLICATION_JSON_VALUE)
    public Member getMember(@PathVariable Integer id) {
        Member p = new Member();
        p.setId(id);
        p.setName("angus");
        return p;
    }
}
主启动类
@SpringBootApplication
@EnableEurekaClient
public class MemberApp {

    public static void main(String[] args) {
        new SpringApplicationBuilder(MemberApp.class).web(true).run(args);
    }

}
Application.yml
spring:
  application:
    name: spring-zuul-member
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
服务调用者
依赖
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-configartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-ribbonartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-feignartifactId>
        dependency>
实体类:同上
调用提供者接口
@FeignClient("spring-zuul-member")
public interface MemberClient {

    @RequestMapping(value = "/member/{id}", method = RequestMethod.GET)
    public Member getMember(@PathVariable("id") Integer id);
}
消费类
@RestController
public class TestController {

    @Autowired
    private MemberClient memberClient;

    @RequestMapping(value = "/food-sale/{memberId}", method = RequestMethod.GET)
    public String foodSale(@PathVariable Integer memberId) {
        Member m = memberClient.getMember(memberId);
        System.out.println(m.getId() + "---" + m.getName());
        return "success";
    }

}
主启动类
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class SaleApp {

    public static void main(String[] args) {
        new SpringApplicationBuilder(SaleApp.class).web(true).run(args);
    }

}
Application.yml
server:
  port: 8081
spring:
  application:
    name: spring-zuul-sale
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
网关
依赖
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-configartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-zuulartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.httpcomponentsgroupId>
            <artifactId>httpclientartifactId>
            <version>4.5.2version>
        dependency>
主启动类
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class GatewayApp {

    public static void main(String[] args) {
        new SpringApplicationBuilder(GatewayApp.class).web(true).run(args);
    }

}
Application.yml
server:
  port: 9000
spring:
  application:
    name: spring-zuul-gateway
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
zuul:
  routes:
    sale:
      path: /sale/**
      serviceId: spring-zuul-sale
分别启动如下

Eureka服务器、服务提供者、服务调用者、网关

访问:http://localhost:9000/sale/food-sale/2

注意:Zuul在调用客户端访问服务的时候,底层默认采用的是HttpClient进行通信,如果要更换需进行如下配置:
【SpringCloud NetFlix】 网关Zuul(二)集群中使用Zuul_第3张图片
以上为疯狂SpringCloud微服务架构实战学习笔记
感谢杨恩雄老师:https://my.oschina.net/JavaLaw

你可能感兴趣的:(❀❀❀❀❀❀-框架篇)