Spring Cloud Zuul
新建Spring Boot工程,命名为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> <groupId>com.dzpykj</groupId> <artifactId>zuul</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>zuul</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
2.将application.properties重命名为application.yml,并且添加配置
server: port: 8767 spring: application: name: zuul eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ zuul: routes: service1: path: /service1/** serviceId: ribbon service2: path: /service2/** serviceId: feign
3.启动类添加注解@EnableZuulProxy开启Zuul
package com.dzpykj; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication @EnableEurekaClient @EnableZuulProxy public class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); } }
4.改造前几篇随笔中的代码
4.1 将ribbon工程com.dzpykj.controller.HelloController的代码改造
package com.dzpykj.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.dzpykj.hystrixService.HelloService; @RestController public class HelloController { // @Autowired // RestTemplate restTemplate; @Autowired HelloService helloService; // @RequestMapping("/hello/{name}") // public String hello(@PathVariable String name) { // return restTemplate.getForObject("http://eurekaclient/hi?name="+name, String.class); // } @RequestMapping("/hello/{name}") public String hi(@PathVariable String name) { return helloService.hello(name); } }
4.2 将ribbon工程com.dzpykj.hystrixService.HelloService的代码改造
package com.dzpykj.hystrixService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @Service public class HelloService { @Value("${server.port}") String port; @Autowired RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "helloErrorFallBack") public String hello(String name) { return restTemplate.getForObject("http://eurekaclient/hi?name="+name+"(ribbon)", String.class); } public String helloErrorFallBack(String name) { return "Sorry "+name+",when you are visting ribbon hystrix project,port:"+port+",you meet an error"; } }
4.3 将feign工程com.dzpykj.controller.HelloController的代码改造
package com.dzpykj.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.dzpykj.feignInterface.HelloInterface; @RestController public class HelloController { @Autowired HelloInterface helloInterface; @RequestMapping("/hello/{name}") public String hello(@PathVariable String name) { return helloInterface.hello(name+"(feign)"); } }
5.依次启动Eureka服务集群、Eureka客户端集群、Ribbon工程
5.1 按照Spring Cloud Eureka Server集群Demo级搭建的步骤启动Eureka服务peer1,peer2集群
5.2 按照Spring Cloud Eureka服务Demo级搭建最后面的步骤,分别启动8763,8764两个Eureka客户端形成集群
5.3 启动Ribbon工程
5.4 启动Feign工程
5.5 启动Zuul工程
6.访问http://127.0.0.1:8767/service1/hello/chaixy;http://127.0.0.1:8767/service2/hello/chaixy
7.这篇随笔代码较琐碎,GitHub源码在此处 https://github.com/PinBo1991/springcloud
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端