新建项目:springBoot-eureka-client-zuul
项目结构:

pom文件:
<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.csq.study</groupId>
<artifactId>springBoot-eureka-client-zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.14.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring.cloud.version>Edgware.SR5</spring.cloud.version>
<quartz.version>2.0.0.RELEASE</quartz.version>
</properties>
<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>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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-server</artifactId>
</dependency>
<!-- Eureka服务-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-zuul -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
</dependencies>
</project>
controller层:
package com.csq.study.springboot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/demo")
public class CityController {
@RequestMapping(value="/hello", method=RequestMethod.GET)
public String listCity() {
return "hello world zuul";
}
}
启动类:
package com.csq.study.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
/*
* @SpringBootApplication注解声明Spring Boot应用
* 作用等同于@Configuration, @EnableAutoConfiguration, @ComponentScan,
* 简化Spring配置
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
配置文件:
#端口号
server.port=8086
#应用名称
spring.application.name=springBoot-eureka-client-zuul
#注册服务器的URL
eureka.client.service-url.defaultZone=http://localhost:8762/eureka/
#测试
zuul.routes.hi.path: /hh/**
zuul.routes.hi.serviceId: springBoot-eureka-client
##传统路由配置:不依赖服务发现。
## 所有以myapi开头的url路由至http://127.0.0.1:8088/下
#zuul.routes.myApi.path=/myapi/**
#zuul.routes.myApi.url=http://127.0.0.1:8088
#forward模式 直接转发至zuul提供的rest服务
#zuul.routes.myDemo.path=/myDemo/**
#zuul.routes.myDemo.url=forward:/demo
zuul.routes.city.path=/city/**
zuul.routes.city.serviceId=springBoot-city
zuul.routes.data.path=/data/**
zuul.routes.data.serviceId=springBoot-data
#请求服务时的超时时间
#feign.client.config.feignName.connect-timeout=5000
#读数据时的超时时间
#feign.client.config.feignName.read-timeout=5000
#feign.hystrix.enabled: false
分别启动注册中心、redis、springBoot-data、springBoot-city 、springBoot-report-feign-gateway、和本项目,然后访问:
http://localhost:8080/weather/cityId/101010400 出现如下截图代表成功

容易出现错误:
status 500 reading DataClient#getDataByCityId(String); content:
{"timestamp":1561010095189,"status":500,"error":"Internal Server Error","exception":"java.lang.RuntimeException","message":"没有相应天气信息","path":"/weather/cityId/101280601"}
报错原因:如果报这种错误那是因为redis没有数据 ,需要先在redis中存一份
解决办法:启动redis缓存服务,在这里访问:http://localhost:8080/weather/cityId/101010400,,把这个id的数据放Redis中一份就可以了。
博客介绍了新建springBoot-eureka-client-zuul项目,包括项目结构、pom文件、controller层、启动类和配置文件等内容。启动相关服务后访问特定链接验证项目是否成功,还指出可能出现的500错误,原因是redis无数据,解决办法是将数据存入Redis。
856

被折叠的 条评论
为什么被折叠?



