美文网首页
springcloud之zuul

springcloud之zuul

作者: GG_lyf | 来源:发表于2021-01-05 13:28 被阅读0次

前言

  之前的eureka只是注册中心,如果想把端口集体暴露,并且还是经过了一层伪装,遮盖怎么办呢?答案就是,经过一层或多层路由,这个路由和前端vue的路由意思一样,vue的路由是通过router找到内容再修改页面的,这个路由是通过定义好的名字取找某个微服务,进而找到他下面的某个或某些接口,进行返回的。


开搞

1.在现有的springboot项目中创建一个zuul微服务,并且用的是maven创建的

2.添加依赖

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>  <!--由于要注册进注册中心,那么就要用eureka-->
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>

3.修改application.yml

server:
  port: 9001 #zuul微服务的端口,随便起,只要不和自己的端口名冲突就行
spring:
  application:
    name: zuul #微服务的名,随便起,只要自己记得住
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:9000/eureka  #要注册进的注册中心的地址
  instance:
    prefer-ip-address: true
zuul:
  routes:
    user: #可以写微服务的名,只起到分辨的作用
      path: /users/**   #访问路径名
      serviceId: user  #微服务名

4.修改启动类

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

5.访问localhost:9001/users/user/findAllusers是路由的前缀,用于找到哪个微服务;user是微服务的父接口名,可以找到是哪个类;findAll是子接口名,可以找到是哪个类下的哪个方法。

image.png

相关文章

网友评论

      本文标题:springcloud之zuul

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