第九章 SpringCloud之Zuul路由
2024-09-04 02:14:54
############Zuul简单使用################
1、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>eureka-client-feign-hystrix-zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-client-feign-hystrix-zuul</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties> <dependencies>
<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-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
2、添加注解
package com.test.eurekaclientfeign; import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean; @SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrixDashboard
@EnableHystrix
@EnableZuulProxy
public class EurekaClientFeignHystrixZuulApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientFeignHystrixZuulApplication.class, args);
} @Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
3、application.yml配置文件
spring:
application:
name: eureka-client-feign-hystrix-zuul #应用名
logging: #logging日志配置
level:
root: INFO
org.hibernate: INFO
server:
port: 8669 #服务端口 eureka:
instance:
hostname: localhost
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:8661/eureka
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
这样就完成了,只是这种方式访问,需要添加serviceID访问
例如:
#不使用Zuul访问
http://192.168.137.1:8664/user/1 #使用Zuul访问,其中eureka-client-user为serviceId
http://192.168.137.1:8669/eureka-client-user/user/4
###############自己配置Zuul##################
在application.yml添加如下的配置
spring:
application:
name: eureka-client-feign-hystrix-zuul #应用名
logging: #logging日志配置
level:
root: INFO
org.hibernate: INFO
server:
port: 8669 #服务端口 eureka:
instance:
hostname: localhost
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:8661/eureka
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
zuul:
routes:
eureka-client-user: /myuser/** #将serviceID = eureka-client-user 简化为 /myuser
最新文章
- Java程序设计之正则表达式
- class命名
- Java printf
- Dubbo集成Spring与Zookeeper实例
- Selenium2学习-021-WebUI自动化实战实例-019-设置浏览器窗口位置大小
- 【Stage3D学习笔记续】山寨Starling(十一):Touch事件体系
- 我也来学着写写WINDOWS服务-解析xml抓取数据并插入数据库
- ACM生活总结
- Github实例教程-创建库、创建主页
- 以Tomcat+Mysql为例,实现Docker多容器连接
- Apache Storm
- elasticsearch视频
- rabbitMQ的安装和创建用户
- Hihocoder 1329 平衡树&#183;Splay(平衡树)
- Subversion版本控制系统的安装和操作.
- 字符串最长子串匹配-dp矩阵[转载]
- 解决IE下载 apk/ipa 变成zip:Android 手机应用程序文件下载服务器 配置解决方法
- ELMAH--Using HTTP Modules and Handlers to Create Pluggable ASP.NET Components 77 out of 90 rated th
- C语言:结构体和联合体(共用体)
- postgresql----Gist索引