spring cloud 中zuul的简单使用

spring cloud 中zuul的简单使用

eureka

在这里插入图片描述

在这里插入图片描述

创建4个微服务项目

zuul eureka demo_a demo_b

zuul:
启动类添加 : @EnableZuulProxy注解

@SpringBootApplication
@EnableZuulProxy
public class ZuulDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZuulDemoApplication.class, args);
    }
}

配置文件 :application.properties

spring.application.name=api-gateway
server.port=5555

# routes to serviceId 这里边是通过serviceid来绑定地址,当在路径后添加/api-a/   则是访问service-A对应的服务。
zuul.routes.api-a.path=/api-a/**
zuul.routes.api-a.serviceId=service-A

zuul.routes.api-b.path=/api-b/**
zuul.routes.api-b.serviceId=service-B

# routes to url  这里是绑定具体的ip地址
zuul.routes.api-a-url.path=/api-a-url/**
zuul.routes.api-a-url.url=http://localhost:2222/

# routes to url  这里是绑定具体的ip地址
zuul.routes.api-b-url.path=/api-b-url/**
zuul.routes.api-b-url.url=http://localhost:3333/

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

pom文件

     <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-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>

实例 :访问 (zuul的服务) http://127.0.0.1:5555/api-b/b/hello 实际上会访问 http://127.0.0.1:3333/b/hello (我的demo_b项目的端口是3333)

eureka:

启动类 添加 @EnableEurekaServer

@EnableEurekaServer
@SpringBootApplication
public class EurekaDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaDemoApplication.class, args);
    }

}

配置文件 application.properties

server.port=1111
#eureka.instance.hostname=localhost

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

pom

  <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-server</artifactId>
        </dependency>

项目a:

启动文件:

@SpringBootApplication
@EnableDiscoveryClient
public class AaaaApplication {
    public static void main(String[] args) {
        SpringApplication.run(AaaaApplication.class, args);
    }

}

配置文件:application.properties

spring.application.name=service-A

server.port=2222

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

pom

    <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-server</artifactId>
        </dependency>

测试controller

@Controller
@RequestMapping("/a")
public class Aacontroller {
    @RequestMapping("/hello")
    @ResponseBody
    public String test(){
        return "hello:A";
    }
}

demo_b : bbbb

和上面的a项目一样
controller不一样

@Controller
@RequestMapping("/b")
public class BbController {
        @RequestMapping("/hello")
        @ResponseBody
        public String test(){

            return "hello:B";
        }


}

以上OK了

最终效果是 访问

http://127.0.0.1:5555/api-a/a/hello 出现 hello:A (/a/hello)

http://127.0.0.1:5555/api-b/b/hello 出现 hello:B (b/hello)

end…

代码下载 :
链接: https://pan.baidu.com/s/1-N7r_HS2gfWkdxlzU4Cf3g
提取码: 869x

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值