SpringCloud--路由网关:Zuul
1.1 Zuul整体架构
1.2 创建Zuul的服务端
1.2.1 创建Gateway项目,配置pom.xml:
pom.xml如下
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-consul-discovery
org.springframework.cloud
spring-cloud-starter-netflix-zuul
1.2.2 配置Zuul服务端
server:
max-http-header-size: 10000000
port: 8501
spring:
application:
name: service-gateway
cloud:
consul:
discovery:
preferIpAddress: true # the health check get ' no such host 'error
health-check-path: /actuator/health # 健康健康路径,也可以自己写
health-check-interval: 10s # 检测轮训时间 1m 代码1分钟
hostname: ${spring.application.name}
service-name: ${spring.application.name}
#instance-id: consul-client00 实例ID,唯一值
instance-id: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
host: 192.168.56.10
port: 8500
zuul:
host:
connect-timeout-millis: 10000
socket-timeout-millis: 60000
routes:
helloService:
path: /proxy/**
serviceId: service-provider
sensitiveHeaders: "*"
ratelimit:
key-prefix: ratelimit-api
# 启动限流服务
enabled: true
behind-proxy: true
default-policy:
# 请求数量
limit: 100
# 请求总时间
quota: 20
# 统计窗口刷新时间
refresh-interval: 60
# 限流类型
type: url
ignored-services: '*'
add-proxy-headers: true
retryable: true
sensitive-headers:
1.2.3 设置启动类:@EnableZuulProxy
@EnableZuulProxy
@EnableDiscoveryClient
@SpringBootApplication
public class SpringBootConsulApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootConsulApplication.class, args);
}
}
1.3 创建Zuul消费端
1.3.1 创建SpringCloud项目(项目结构同消费端)
1.3.2 配置Zuul消费端
server.port=8504
spring.application.name=service-consumer
spring.cloud.consul.host=192.168.56.10
spring.cloud.consul.port=8500
#设置不需要注册到 consul 中
spring.cloud.consul.discovery.register=false
1.3.3 使用RestTemplate访问服务端接口
/**
* 调用远程接口
*
* @return
*/
@RequestMapping("/call")
public User call(@RequestParam(value = "id") int id) {
ServiceInstance serviceInstance = loadBalancer.choose("service-gateway");
System.out.println("服务地址:" + serviceInstance.getUri());
System.out.println("服务名称:" + serviceInstance.getServiceId());
User callServiceResult = new RestTemplate().getForObject(serviceInstance.getUri().toString() + + "/proxy/hello/index?id=" + id,
User.class);
System.out.println(callServiceResult);
return callServiceResult;
}
1.3.4 使用Feign访问服务端接口
1.3.4.1 设置启动类
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class SpringBootConsulApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootConsulApplication.class, args);
}
}
1.3.4.2 Feign服务类
@FeignClient(name= "service-gateway")
public interface HelloRemoteService {
@RequestMapping(value = "/Proxy/hello/index")
public User hello(@RequestParam(value = "id") int id);
}
1.3.4.3 测试调用类
@Autowired
private HelloRemoteService userService;
/**
* 使用Feign,调用远程接口
*
* @return
*/
@RequestMapping("/FeignCall")
public User FeignCall(@RequestParam(value = "id") int id) {
User callServiceResult = userService.hello(id);
System.out.println(callServiceResult);
return callServiceResult;
}