欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?cloud?實現(xiàn)房源查詢功能的實例代碼

 更新時間:2022年09月28日 08:35:34   作者:天海奈奈  
這篇文章主要介紹了Spring?cloud?實現(xiàn)房源查詢功能,本項目是一個多模塊項目,創(chuàng)建一個 Spring Initializr 項目 不自動添加依賴項,完成創(chuàng)建后刪除自帶的src目錄,并在根目錄下創(chuàng)建新的maven模塊,需要的朋友可以參考下

前言

這個項目的功能很簡單只涉及了查詢功能,這個項目的的目的是熟悉SpringCloud框架,明白服務(wù)與服務(wù)之間的調(diào)用是通過http請求完成的使用微服務(wù)的架構(gòu)而Feign可以使其像調(diào)用本地方法一樣,學會在其他模塊調(diào)用另一模塊的服務(wù)和內(nèi)容,完成負載均衡,學會將不同端口注冊到eureka ,了解網(wǎng)關(guān)并會配置網(wǎng)關(guān)和使用斷路器。

核心組件

服務(wù)注冊中心

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ù)庫設(shè)計

由于本項目只是為了完整的實現(xiàn)SpringCloud項目并明白框架功能故只設(shè)計查詢功能,所以只設(shè)計兩張表

表1 house

表2 

1 項目搭建

本項目是一個多模塊項目,創(chuàng)建一個 Spring Initializr 項目 不自動添加依賴項,完成創(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  開發(fā)房源查詢模塊:

2.1 house-list 模塊 

2.1.1模塊總覽:

端口號 8081 url接口 /house  

參數(shù)名稱 :null

參數(shù)類型 : null 

說明 : 找到上線房源 打印list

2 .1.2寫配置文件 

創(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 依賴項配置

<?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 啟動類

/**
 * 描述:     項目啟動類
 */
@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對象
public class HouseListController {
 
    @Autowired
    HouseListService houseListService;
 
    @GetMapping("/house")
    public List<House> houseList() {
        return houseListService.getCourseList();
    }
}

2.1.5 創(chuàng)建entity實體類,實現(xiàn)set /get 方法并用自動生成重寫toString方法。

/**
 * 描述:     House實體類
 */
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 實現(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();
}

這里實現(xiàn)的是查詢已上線的房源。

2.2 房價模塊

2.2.1 總覽

端口號 8083

 url接口 /price

參數(shù)名稱 :houseId

參數(shù)類型 : int

說明 : 獲取到對應(yīng)房源id的價格

 url接口 /HouseInPrice

參數(shù)名稱 :null

參數(shù)類型 : null

說明 : 作用與/list/house類似只是為了測試引入功能

 url接口 /houseAndPrice

參數(shù)名稱 :null

參數(shù)類型 : null

說明 : 獲取到房子id 價格 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 啟動類

/**
 * 描述:     項目啟動類
 */
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker
public class HousePriceApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(HousePriceApplication.class, args);
    }
}

2.2.4 client

/**
 * 描述:     房源列表的Feign客戶端
 */
//當遠端服務(wù)出現(xiàn)問題會進入這個類
@FeignClient(value = "house-list" ,fallback = HouseListClientHystrix.class)
//有多個實例時,帶有注解的人就是注入的
@Primary
public interface HouseListClient {
 
    @GetMapping("/house")
    public List<House> houseList();
}

如果不用feign 不同服務(wù)是不能進行調(diào)用的,我們在需要使用整合服務(wù)的模塊中引入依賴 ,我加了注釋了,再在啟動類中加入注解,并創(chuàng)建一個類來作為我們引入模塊的feign客戶端,直接從我們想要引入的服務(wù)的controller中復制即可,引入后會有提示,要引入對另一個模塊的依賴,就能數(shù)顯服務(wù)的整合了。

/**
 * 描述:     斷路器實現(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("默認房源");
        House.setValid(1);
        defaultCourses.add(House);
        return defaultCourses;
    }
}

發(fā)生錯誤時調(diào)用的類,給服務(wù)的返回是默認的返回值 @Component 注解使它成為一個組件

2.2.5 controller 

/**
 * 描述:     房價格控制器
 */
@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

/**
 * 描述:     房價格服務(wù)
 */
public interface HousePriceService {
 
    HousePrice getHousePrice(Integer houseId);
 
    List<HouseAndPrice> getHousesAndPrice();
    List<HousePrice> getHousePriceList();
}

impl 

/**
 * 描述:     課程價格的服務(wù)實現(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();
    }
 
 
}

第二個方法就體現(xiàn)除了服務(wù)的整合,我們并不用去進行多表查詢,只需要調(diào)用其他服務(wù)就能快捷完成。

2.2.7 實體類

/**
 * 融合類
 *
 */
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

/**
 * 描述:     房價格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 開發(fā)

Eureka server服務(wù)注冊與管理的中心 生產(chǎn)者,提供者

調(diào)用方:

提供者把自己的信息注冊到上面,server就能掌握最新的動態(tài),調(diào)用方去調(diào)用服務(wù)是,會先去獲取最新的地址,獲取地址之后再去進行實際的調(diào)用。

Eureka 開發(fā)多了獲取地址這一步是對ip服務(wù)進行解耦 。

Eureka client改寫

我們把強兩個模塊進行Eureka Client改寫也很簡單,只用在啟動類上加注解

@EnableEurekaClient

在配置文件上進行配置

eureka.client.service-url.defaultZone=http://localhost:8000/eureka/ 

我們eureka端口號多少8000那項就是多少 

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/

 第一個注釋是獲取注冊表。不需要同步其他節(jié)點數(shù)據(jù)

第二個注釋是是否發(fā)自己也注冊上去

<?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 啟動類

/**
 * 描述:     Eureka服務(wù)端
 */
//eurekaserver啟動
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

這樣就證明我們的服務(wù)已經(jīng)注冊上去了

4 負載均衡

4.1負載均衡策略

RandomRule表示隨機策略
RoundRobinRule 表示輪詢策略
Response TimeWeightedRule加權(quán),根據(jù)每一個Server的平均響應(yīng)時間動態(tài)加權(quán)

4.2 為price模塊配置負載均衡

其實就是在配置文件中加一行

house-list.ribbon.NFLoadBanlancerRuleClassName=com.netflix.loadbalancer.RoundRobinRuley意思就是對house-list服務(wù)調(diào)用的時候采取的負載均衡的策略。

5 斷路器

起到一個兜底的作用,有些時候一個服務(wù)出現(xiàn)問題,我們不希望整個系統(tǒng)都崩潰掉,那我們可以采取降級的手段,雖然不能返回正常的數(shù)據(jù)卻可以保證系統(tǒng)正常運行。運用斷路器我們不僅要配置配置文件,加依賴,還要書寫一個短路類(參見price模塊下client 下的斷路器實現(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 啟動類

/**
 * 描述:     網(wǎng)關(guān)啟動類
 */
//網(wǎng)關(guān)注解
@EnableZuulProxy
@SpringCloudApplication
public class ZuulGatewayApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ZuulGatewayApplication.class, args);
    }
}

 6.4 過濾器

pre 過濾器在路由請求之前運行
route 過濾器可以處理請求的實際路由
post 路由請求后運行過濾器
error如果在處理請求的過程中發(fā)生錯誤,則過濾器將運行

這里寫一個前置一個后置

/**
 * 描述:     前置過濾器
 */
@Component
public class PreRequestFilter extends ZuulFilter {
 
    //指定過濾器類別
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }
    //順序
    @Override
    public int filterOrder() {
        return 5;
    }
    //是不是走過濾器可以添加判斷
    @Override
    public boolean shouldFilter() {
        return true;
    }
    //通過過濾器將要實現(xiàn)的邏輯----打印出請求url
    @Override
    public Object run() throws ZuulException {
        RequestContext currentContext = RequestContext.getCurrentContext();
        //獲取當前請求uri
        System.out.println("URI:" + currentContext.getRequest().getRequestURI());
        return null;
    }
}
/**
 * 描述:     后置過濾器
 */
@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

可以看書過濾器也實現(xiàn)了

總結(jié)

1 通過這個項目我們了解了SpringCloud的幾個重要組件

2 數(shù)據(jù)流向

3 完成兩個模塊開發(fā)后進行了服務(wù)注冊與發(fā)現(xiàn)Eureka

4 為了使服務(wù)間調(diào)用更加簡便使用了Feign組件

5 使用到了負載均衡

6 使用到了熔斷器

7 使用網(wǎng)關(guān),并進行了過濾器的編寫

到此這篇關(guān)于Spring cloud 實現(xiàn)房源查詢功能的文章就介紹到這了,更多相關(guān)Spring cloud 房源查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java不用算數(shù)運算符來實現(xiàn)求和方法

    Java不用算數(shù)運算符來實現(xiàn)求和方法

    我們都知道,Java的運算符除了具有優(yōu)先級之外,還有一個結(jié)合性的特點。當一個表達式中出現(xiàn)多種運算符時,執(zhí)行的先后順序不僅要遵守運算符優(yōu)先級別的規(guī)定,還要受運算符結(jié)合性的約束,以便確定是自左向右進行運算還是自右向左進行運算,但是如果不用運算符怎么求和呢
    2022-04-04
  • mybatis?plus更新字段為null處理方法

    mybatis?plus更新字段為null處理方法

    這篇文章主要為大家介紹了將mybatis?plus更新字段為null的處理方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2022-02-02
  • SpringBoot集成RocketMQ發(fā)送事務(wù)消息的原理解析

    SpringBoot集成RocketMQ發(fā)送事務(wù)消息的原理解析

    RocketMQ 的事務(wù)消息提供類似 X/Open XA 的分布事務(wù)功能,通過事務(wù)消息能達到分布式事務(wù)的最終一致,這篇文章主要介紹了SpringBoot集成RocketMQ發(fā)送事務(wù)消息,需要的朋友可以參考下
    2022-06-06
  • Mybatis?ResultMap和分頁操作示例詳解

    Mybatis?ResultMap和分頁操作示例詳解

    這篇文章主要為大家介紹了Mybatis?ResultMap和分頁操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-10-10
  • 貪心算法原理及在Java中的使用

    貪心算法原理及在Java中的使用

    我們可能在好多地方都會聽到貪心算法這一概念,并且它的算法思想也比較簡單就是說算法只保證局部最優(yōu),進而達到全局最優(yōu)。但我們實際編程的過程中用的并不是很多,究其原因可能是貪心算法使用的條件比較苛刻,所要解決的問題必須滿足貪心選擇性質(zhì)
    2021-05-05
  • java 中的static關(guān)鍵字和final關(guān)鍵字的不同之處

    java 中的static關(guān)鍵字和final關(guān)鍵字的不同之處

    java 中的static關(guān)鍵字和final關(guān)鍵字的不同之處,需要的朋友可以參考一下
    2013-03-03
  • Java實現(xiàn)整數(shù)的逆序輸出的三種方法

    Java實現(xiàn)整數(shù)的逆序輸出的三種方法

    這篇文章主要介紹了Java實現(xiàn)整數(shù)的逆序輸出的三種方法,第一種是無限制整數(shù)的逆序輸出,第二種是非負整數(shù)的逆序輸出,第三種是非特殊情況的逆序輸出,每種方法給大家講解的非常詳細需要的朋友可以參考下
    2022-11-11
  • VSCode+Gradle搭建Java開發(fā)環(huán)境實現(xiàn)

    VSCode+Gradle搭建Java開發(fā)環(huán)境實現(xiàn)

    這篇文章主要介紹了VSCode+Gradle搭建Java開發(fā)環(huán)境實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • 關(guān)于ScheduledThreadPoolExecutor不執(zhí)行的原因分析

    關(guān)于ScheduledThreadPoolExecutor不執(zhí)行的原因分析

    這篇文章主要介紹了關(guān)于ScheduledThreadPoolExecutor不執(zhí)行的原因分析,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • java?Spring?Boot的介紹與初體驗

    java?Spring?Boot的介紹與初體驗

    大家好,本篇文章主要講的是java?Spring?Boot的介紹與初體驗,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01

最新評論