Spring?cloud?實(shí)現(xiàn)房源查詢功能的實(shí)例代碼
前言
這個(gè)項(xiàng)目的功能很簡(jiǎn)單只涉及了查詢功能,這個(gè)項(xiàng)目的的目的是熟悉SpringCloud框架,明白服務(wù)與服務(wù)之間的調(diào)用是通過(guò)http請(qǐng)求完成的使用微服務(wù)的架構(gòu)而Feign可以使其像調(diào)用本地方法一樣,學(xué)會(huì)在其他模塊調(diào)用另一模塊的服務(wù)和內(nèi)容,完成負(fù)載均衡,學(xué)會(huì)將不同端口注冊(cè)到eureka ,了解網(wǎng)關(guān)并會(huì)配置網(wǎng)關(guān)和使用斷路器。
核心組件
服務(wù)注冊(cè)中心
Spring Cloud Netflix Eureka
服務(wù)調(diào)用方式
REST API、Feign、 Ribbon
服務(wù)網(wǎng)關(guān)
Spring Cloud Netflix Zuul
斷路器
Spring Cloud Netflix Hystrix
數(shù)據(jù)庫(kù)設(shè)計(jì)
由于本項(xiàng)目只是為了完整的實(shí)現(xiàn)SpringCloud項(xiàng)目并明白框架功能故只設(shè)計(jì)查詢功能,所以只設(shè)計(jì)兩張表
表1 house
表2
1 項(xiàng)目搭建
本項(xiàng)目是一個(gè)多模塊項(xiàng)目,創(chuàng)建一個(gè) Spring Initializr 項(xiàng)目 不自動(dòng)添加依賴項(xiàng),完成創(chuàng)建后刪除自帶的src目錄,并在根目錄下創(chuàng)建新的maven模塊。
1.1添加依賴
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <packaging>pom</packaging> <modules> <module>house-server</module> <module>eureak-server</module> <module>house-zuul</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.12.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xatu</groupId> <artifactId>spring-cloud-house-test1</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-cloud-house-test1</name> <description>spring-cloud-house-test1</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <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> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR5</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 開(kāi)發(fā)房源查詢模塊:
2.1 house-list 模塊
2.1.1模塊總覽:
端口號(hào) 8081 url接口 /house
參數(shù)名稱 :null
參數(shù)類型 : null
說(shuō)明 : 找到上線房源 打印list
2 .1.2寫(xiě)配置文件
創(chuàng)建application.properties
?server.port=8081 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/house_test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true spring.datasource.username=root spring.datasource.password=123456 logging.pattern.console=%clr(%d{${LOG_DATEFORMAT_PATTERN:HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx} mybatis.configuration.map-underscore-to-camel-case=true spring.application.name=house-list eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
2.1.3 依賴項(xiàng)配置
<?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"> <parent> <artifactId>house-server</artifactId> <groupId>com.xatu</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>houser-list</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <!-- eureka-client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.1.3 啟動(dòng)類
/** * 描述: 項(xiàng)目啟動(dòng)類 */ @SpringBootApplication @EnableEurekaClient public class HouseListApplication { public static void main(String[] args) { SpringApplication.run(HouseListApplication.class, args); }
2.1.4 controller
/** * 描述: CourseListController課程列表Controller */ @RestController//將返結(jié)果是json對(duì)象 public class HouseListController { @Autowired HouseListService houseListService; @GetMapping("/house") public List<House> houseList() { return houseListService.getCourseList(); } }
2.1.5 創(chuàng)建entity實(shí)體類,實(shí)現(xiàn)set /get 方法并用自動(dòng)生成重寫(xiě)toString方法。
/** * 描述: House實(shí)體類 */ public class House implements Serializable { Integer id; Integer houseId; String houseName; Integer valid; @Override public String toString() { return "Course{" + "id=" + id + ", courseId=" + houseId + ", courseName='" + houseName + '\'' + ", valid=" + valid + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getHouseId() { return houseId; } public void setHouseId(Integer houseId) { this.houseId = houseId; } public String getHouseName() { return houseName; } public void setHouseName(String houseName) { this.houseName = houseName; } public Integer getValid() { return valid; } public void setValid(Integer valid) { this.valid = valid; } }
2.1.6 service
/** * 描述: 房源列表服務(wù) */ public interface HouseListService { List<House> getCourseList(); }
impl 實(shí)現(xiàn)類
@Service public class HouseListServiceImpl implements HouseListService { @Autowired HouseMapper houseMapper; @Override public List<House> getCourseList() { return houseMapper.findValidCourses(); } }
mapper
/** * 描述: 房源的Mapper類 */ @Mapper @Repository public interface HouseMapper { @Select("SELECT * FROM house WHERE valid = 1") List<House> findValidCourses(); }
這里實(shí)現(xiàn)的是查詢已上線的房源。
2.2 房?jī)r(jià)模塊
2.2.1 總覽
端口號(hào) 8083
url接口 /price
參數(shù)名稱 :houseId
參數(shù)類型 : int
說(shuō)明 : 獲取到對(duì)應(yīng)房源id的價(jià)格
url接口 /HouseInPrice
參數(shù)名稱 :null
參數(shù)類型 : null
說(shuō)明 : 作用與/list/house類似只是為了測(cè)試引入功能
url接口 /houseAndPrice
參數(shù)名稱 :null
參數(shù)類型 : null
說(shuō)明 : 獲取到房子id 價(jià)格 id 名字
2.2.2 配置文件與依賴
server.port=8082 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/house_test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true spring.datasource.username=root spring.datasource.password=123456 logging.pattern.console=%clr(%d{${LOG_DATEFORMAT_PATTERN:HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx} #tuo feng ming ming zhuan huan mybatis.configuration.map-underscore-to-camel-case=true spring.application.name=house-price eureka.client.service-url.defaultZone=http://localhost:8000/eureka/ house-list.ribbon.NFLoadBanlancerRuleClassName=com.netflix.loadbalancer.RoundRobinRule feign.hystrix.enabled=true
<?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"> <parent> <artifactId>house-server</artifactId> <groupId>com.xatu</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>house-price</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- feign--> <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>com.xatu</groupId> <artifactId>houser-list</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.2.3 啟動(dòng)類
/** * 描述: 項(xiàng)目啟動(dòng)類 */ @SpringBootApplication @EnableEurekaClient @EnableFeignClients @EnableCircuitBreaker public class HousePriceApplication { public static void main(String[] args) { SpringApplication.run(HousePriceApplication.class, args); } }
2.2.4 client
/** * 描述: 房源列表的Feign客戶端 */ //當(dāng)遠(yuǎn)端服務(wù)出現(xiàn)問(wèn)題會(huì)進(jìn)入這個(gè)類 @FeignClient(value = "house-list" ,fallback = HouseListClientHystrix.class) //有多個(gè)實(shí)例時(shí),帶有注解的人就是注入的 @Primary public interface HouseListClient { @GetMapping("/house") public List<House> houseList(); }
如果不用feign 不同服務(wù)是不能進(jìn)行調(diào)用的,我們?cè)谛枰褂谜戏?wù)的模塊中引入依賴 ,我加了注釋了,再在啟動(dòng)類中加入注解,并創(chuàng)建一個(gè)類來(lái)作為我們引入模塊的feign客戶端,直接從我們想要引入的服務(wù)的controller中復(fù)制即可,引入后會(huì)有提示,要引入對(duì)另一個(gè)模塊的依賴,就能數(shù)顯服務(wù)的整合了。
/** * 描述: 斷路器實(shí)現(xiàn)類 */ @Component public class HouseListClientHystrix implements HouseListClient { @Override public List<House> houseList() { List<House> defaultCourses = new ArrayList<>(); House House = new House(); House.setId(1); House.setHouseId(1); House.setHouseName("默認(rèn)房源"); House.setValid(1); defaultCourses.add(House); return defaultCourses; } }
發(fā)生錯(cuò)誤時(shí)調(diào)用的類,給服務(wù)的返回是默認(rèn)的返回值 @Component 注解使它成為一個(gè)組件
2.2.5 controller
/** * 描述: 房?jī)r(jià)格控制器 */ @RestController public class HousePriceController { @Autowired HousePriceService housePriceService; @Autowired HouseListClient houseListClient; @GetMapping("/price") public Integer getCoursePrice(Integer houseId) { HousePrice housePrice = housePriceService.getHousePrice(houseId); return housePrice.getPrice(); } @GetMapping("/HouseInPrice") public List<House> getHouseListInPrice(Integer houseId) { List<House> houses = houseListClient.houseList(); return houses; } @GetMapping("/houseAndPrice") public List<HouseAndPrice> getCoursesAndPrice() { List<HouseAndPrice> houseAndPrice = housePriceService.getHousesAndPrice(); return houseAndPrice; } }
2.2.6 service
/** * 描述: 房?jī)r(jià)格服務(wù) */ public interface HousePriceService { HousePrice getHousePrice(Integer houseId); List<HouseAndPrice> getHousesAndPrice(); List<HousePrice> getHousePriceList(); }
impl
/** * 描述: 課程價(jià)格的服務(wù)實(shí)現(xiàn)類 */ @Service public class HousePriceServiceImpl implements HousePriceService { @Autowired HousePriceMapper housePriceMapper; @Autowired HouseListClient houseListClient; @Override public HousePrice getHousePrice(Integer houseId) { return housePriceMapper.findCoursePrice(houseId); } @Override public List<HouseAndPrice> getHousesAndPrice() { List<HouseAndPrice> houseAndPricesList = new ArrayList<>(); List<House> houses = houseListClient.houseList(); for(int i = 0;i < houses.size();i++){ House house = houses.get(i); if(house != null){ HousePrice housePrice = getHousePrice(house.getHouseId()); HouseAndPrice houseAndPrice = new HouseAndPrice(); houseAndPrice.setPrice(housePrice.getPrice()); houseAndPrice.setName(house.getHouseName()); houseAndPrice.setId(house.getId()); houseAndPrice.setHouseId(house.getHouseId()); houseAndPricesList.add(houseAndPrice); } } return houseAndPricesList; } @Override public List<HousePrice> getHousePriceList() { return housePriceMapper.getAll(); } }
第二個(gè)方法就體現(xiàn)除了服務(wù)的整合,我們并不用去進(jìn)行多表查詢,只需要調(diào)用其他服務(wù)就能快捷完成。
2.2.7 實(shí)體類
/** * 融合類 * */ public class HouseAndPrice { Integer id; Integer houseId; String name; Integer price; @Override public String toString() { return "HouseAndPrice{" + "id=" + id + ", houseId=" + houseId + ", name='" + name + '\'' + ", price=" + price + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getHouseId() { return houseId; } public void setHouseId(Integer houseId) { this.houseId = houseId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } }
public class HousePrice implements Serializable { Integer id; Integer houseId; Integer price; @Override public String toString() { return "HousePrice{" + "id=" + id + ", houseId=" + houseId + ", price=" + price + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getHouseId() { return houseId; } public void setHouseId(Integer houseId) { this.houseId = houseId; } public Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } }
2.2.8 mapper
/** * 描述: 房?jī)r(jià)格Mapper類 */ @Mapper @Repository public interface HousePriceMapper { @Select("SELECT * FROM house_price WHERE house_id = #{houseId}") HousePrice findCoursePrice(Integer courseId); @Select("SELECT * FROM house_price" ) List<HousePrice> getAll(); }
3 Eureka 開(kāi)發(fā)
Eureka server服務(wù)注冊(cè)與管理的中心 生產(chǎn)者,提供者
調(diào)用方:
提供者把自己的信息注冊(cè)到上面,server就能掌握最新的動(dòng)態(tài),調(diào)用方去調(diào)用服務(wù)是,會(huì)先去獲取最新的地址,獲取地址之后再去進(jìn)行實(shí)際的調(diào)用。
Eureka 開(kāi)發(fā)多了獲取地址這一步是對(duì)ip服務(wù)進(jìn)行解耦 。
Eureka client改寫(xiě)
我們把強(qiáng)兩個(gè)模塊進(jìn)行Eureka Client改寫(xiě)也很簡(jiǎn)單,只用在啟動(dòng)類上加注解
@EnableEurekaClient
在配置文件上進(jìn)行配置
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
我們eureka端口號(hào)多少8000那項(xiàng)就是多少
3.1模塊總覽
3.2 配置文件與依賴
spring.application.name=eureka-server server.port=8000 eureka.instance.hostname=localhost #????? eureka.client.fetch-registry=false #????????? eureka.client.register-with-eureka=false eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
第一個(gè)注釋是獲取注冊(cè)表。不需要同步其他節(jié)點(diǎn)數(shù)據(jù)
第二個(gè)注釋是是否發(fā)自己也注冊(cè)上去
<?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"> <parent> <artifactId>spring-cloud-house-test1</artifactId> <groupId>com.xatu</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>eureak-server</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <name>course-eureka-server</name> <description>Spring Cloud Eureka</description> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3.3 啟動(dòng)類
/** * 描述: Eureka服務(wù)端 */ //eurekaserver啟動(dòng) @EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
這樣就證明我們的服務(wù)已經(jīng)注冊(cè)上去了
4 負(fù)載均衡
4.1負(fù)載均衡策略
RandomRule表示隨機(jī)策略
RoundRobinRule 表示輪詢策略
Response TimeWeightedRule加權(quán),根據(jù)每一個(gè)Server的平均響應(yīng)時(shí)間動(dòng)態(tài)加權(quán)
4.2 為price模塊配置負(fù)載均衡
其實(shí)就是在配置文件中加一行
house-list.ribbon.NFLoadBanlancerRuleClassName=com.netflix.loadbalancer.RoundRobinRuley意思就是對(duì)house-list服務(wù)調(diào)用的時(shí)候采取的負(fù)載均衡的策略。
5 斷路器
起到一個(gè)兜底的作用,有些時(shí)候一個(gè)服務(wù)出現(xiàn)問(wèn)題,我們不希望整個(gè)系統(tǒng)都崩潰掉,那我們可以采取降級(jí)的手段,雖然不能返回正常的數(shù)據(jù)卻可以保證系統(tǒng)正常運(yùn)行。運(yùn)用斷路器我們不僅要配置配置文件,加依賴,還要書(shū)寫(xiě)一個(gè)短路類(參見(jiàn)price模塊下client 下的斷路器實(shí)現(xiàn)類)
6 網(wǎng)關(guān)
6.1 模塊總覽
6.2 配置文件與依賴
<?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"> <parent> <artifactId>spring-cloud-house-test1</artifactId> <groupId>com.xatu</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>house-zuul</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- 網(wǎng)關(guān)依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
spring.application.name=house-gateway server.port=9000 logging.pattern.console=%clr(%d{${LOG_DATEFORMAT_PATTERN:HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx} mybatis.configuration.map-underscore-to-camel-case=true eureka.client.service-url.defaultZone=http://localhost:8000/eureka/ #??????? zuul.prefix=/xatu zuul.routes.house-list.path=/list/** zuul.routes.house-list.service-id=house-list zuul.routes.house-price.path=/price/** zuul.routes.house-price.service-id=house-price
6.3 啟動(dòng)類
/** * 描述: 網(wǎng)關(guān)啟動(dòng)類 */ //網(wǎng)關(guān)注解 @EnableZuulProxy @SpringCloudApplication public class ZuulGatewayApplication { public static void main(String[] args) { SpringApplication.run(ZuulGatewayApplication.class, args); } }
6.4 過(guò)濾器
pre 過(guò)濾器在路由請(qǐng)求之前運(yùn)行
route 過(guò)濾器可以處理請(qǐng)求的實(shí)際路由
post 路由請(qǐng)求后運(yùn)行過(guò)濾器
error如果在處理請(qǐng)求的過(guò)程中發(fā)生錯(cuò)誤,則過(guò)濾器將運(yùn)行
這里寫(xiě)一個(gè)前置一個(gè)后置
/** * 描述: 前置過(guò)濾器 */ @Component public class PreRequestFilter extends ZuulFilter { //指定過(guò)濾器類別 @Override public String filterType() { return FilterConstants.PRE_TYPE; } //順序 @Override public int filterOrder() { return 5; } //是不是走過(guò)濾器可以添加判斷 @Override public boolean shouldFilter() { return true; } //通過(guò)過(guò)濾器將要實(shí)現(xiàn)的邏輯----打印出請(qǐng)求url @Override public Object run() throws ZuulException { RequestContext currentContext = RequestContext.getCurrentContext(); //獲取當(dāng)前請(qǐng)求uri System.out.println("URI:" + currentContext.getRequest().getRequestURI()); return null; } }
/** * 描述: 后置過(guò)濾器 */ @Component public class PostRequestFilter extends ZuulFilter { @Override public String filterType() { return FilterConstants.POST_TYPE; } @Override public int filterOrder() { return FilterConstants.SEND_RESPONSE_FILTER_ORDER - 1; } @Override public boolean shouldFilter() { return true; } //打印返回狀態(tài)碼 @Override public Object run() throws ZuulException { RequestContext currentContext = RequestContext.getCurrentContext(); int status = currentContext.getResponse().getStatus(); System.out.println("response status:" + status); return null; } }
瀏覽器輸入http://127.0.0.1:9000/xatu/house-list/house
可以看書(shū)過(guò)濾器也實(shí)現(xiàn)了
總結(jié)
1 通過(guò)這個(gè)項(xiàng)目我們了解了SpringCloud的幾個(gè)重要組件
2 數(shù)據(jù)流向
3 完成兩個(gè)模塊開(kāi)發(fā)后進(jìn)行了服務(wù)注冊(cè)與發(fā)現(xiàn)Eureka
4 為了使服務(wù)間調(diào)用更加簡(jiǎn)便使用了Feign組件
5 使用到了負(fù)載均衡
6 使用到了熔斷器
7 使用網(wǎng)關(guān),并進(jìn)行了過(guò)濾器的編寫(xiě)
到此這篇關(guān)于Spring cloud 實(shí)現(xiàn)房源查詢功能的文章就介紹到這了,更多相關(guān)Spring cloud 房源查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java不用算數(shù)運(yùn)算符來(lái)實(shí)現(xiàn)求和方法
我們都知道,Java的運(yùn)算符除了具有優(yōu)先級(jí)之外,還有一個(gè)結(jié)合性的特點(diǎn)。當(dāng)一個(gè)表達(dá)式中出現(xiàn)多種運(yùn)算符時(shí),執(zhí)行的先后順序不僅要遵守運(yùn)算符優(yōu)先級(jí)別的規(guī)定,還要受運(yùn)算符結(jié)合性的約束,以便確定是自左向右進(jìn)行運(yùn)算還是自右向左進(jìn)行運(yùn)算,但是如果不用運(yùn)算符怎么求和呢2022-04-04SpringBoot集成RocketMQ發(fā)送事務(wù)消息的原理解析
RocketMQ 的事務(wù)消息提供類似 X/Open XA 的分布事務(wù)功能,通過(guò)事務(wù)消息能達(dá)到分布式事務(wù)的最終一致,這篇文章主要介紹了SpringBoot集成RocketMQ發(fā)送事務(wù)消息,需要的朋友可以參考下2022-06-06Mybatis?ResultMap和分頁(yè)操作示例詳解
這篇文章主要為大家介紹了Mybatis?ResultMap和分頁(yè)操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10java 中的static關(guān)鍵字和final關(guān)鍵字的不同之處
java 中的static關(guān)鍵字和final關(guān)鍵字的不同之處,需要的朋友可以參考一下2013-03-03Java實(shí)現(xiàn)整數(shù)的逆序輸出的三種方法
這篇文章主要介紹了Java實(shí)現(xiàn)整數(shù)的逆序輸出的三種方法,第一種是無(wú)限制整數(shù)的逆序輸出,第二種是非負(fù)整數(shù)的逆序輸出,第三種是非特殊情況的逆序輸出,每種方法給大家講解的非常詳細(xì)需要的朋友可以參考下2022-11-11VSCode+Gradle搭建Java開(kāi)發(fā)環(huán)境實(shí)現(xiàn)
這篇文章主要介紹了VSCode+Gradle搭建Java開(kāi)發(fā)環(huán)境實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07關(guān)于ScheduledThreadPoolExecutor不執(zhí)行的原因分析
這篇文章主要介紹了關(guān)于ScheduledThreadPoolExecutor不執(zhí)行的原因分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08