路由网关---zuul
我们知道我们要进入一个服务本身,很明显我们没有特别好的办法,直接输入IP地址+端口号,我们知道这样的做法很糟糕的,这样的做法大有问题,首先暴露了我们实体机器的IP地址,别人一看你的IP地址就知道服务部署在哪里,让别人很方便的进行攻击操作。
某个路径,你输入该路径,它匹配到了,它就去替你访问这个服务,它会有个请求转发的过程,像Nginx一样,服务机器的具体实例,它不会直接去访问IP,它会去Eureka注册中心拿到服务的实例ID,即服务的名字。我再次使用客户端的负载均衡ribbon访问其中服务实例中的一台。
路由项目实例
1 新建 eureka
1.1 pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
1.2 application.properties
-
server.port=8088 eureka.instance.hostname=127.0.0.1 #不要向注册中心注册自己 eureka.client.register-with-eureka=false #禁止检索服务 eureka.client.fetch-registry=false eureka.client.service-url.defaultZone=http://127.0.0.1:8088/eureka spring.application.name=eurekserver eureka.server.enable-self-preservation=false
1.3启动类
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
2新建 eureka client
2.1 pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.6.RELEASE</version> </dependency>
2.2 application.properties
server.port=8111
#设置应用的名称
spring.application.name=eurekaClient1
#服务注册的Eureka Server地址
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8088/eureka
##http://shumeigang:123456@127.0.0.1:8081/eureka
eureka.instance.prefer-ip-address=true
#设置注册ip
#表示是否将自己注册在EurekaServer上,默认为true。由于当前应用就是EurekaServer,所以置为false
eureka.client.register-with-eureka=true
#表示表示是否从EurekaServer获取注册信息,默认为true。单节点不需要同步其他的EurekaServer节点的数据
eureka.client.fetch-registry=true
2.3 Controller
package com.example.demo; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @GetMapping("/User/Name") public String UserName(){ return "eurekaClient1:小明"; } }
2.4启动类
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
3 创新Zuuldemo
3.1 pom.xml
<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-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
3.2 application.properties
server.port=8020
#设置应用的名称
spring.application.name=Zuuldemo2
#eurekaClient1 接口项目名称
zuul.routes.eurekaClient1.path=/api/**
#接口地址
zuul.routes.eurekaClient1.url=http://127.0.0.1:8111/User/Name
zuul:
semaphore:
max-semaphores: 200
#服务注册的Eureka Server地址
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8088/eureka
##http://shumeigang:123456@127.0.0.1:8081/eureka
eureka.instance.prefer-ip-address=true
#设置注册ip
#表示是否将自己注册在EurekaServer上,默认为true。由于当前应用就是EurekaServer,所以置为false
eureka.client.register-with-eureka=true
#表示表示是否从EurekaServer获取注册信息,默认为true。单节点不需要同步其他的EurekaServer节点的数据
eureka.client.fetch-registry=true
3.3启动类
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4运行
第一步 运行eureka
第二步 运行 eureka Client
第三步 运行 Zuul
运行结果
运行eureka结果
运行 eureka Client
运行 Zuul http://127.0.0.1:8020/api