最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

前言

這個(gè)項(xiàng)目的功能很簡單只涉及了查詢功能,這個(gè)項(xiàng)目的的目的是熟悉SpringCloud框架,明白服務(wù)與服務(wù)之間的調(diào)用是通過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ù)庫設(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  開發(fā)房源查詢模塊:

2.1 house-list 模塊 

2.1.1模塊總覽:

端口號(hào) 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 依賴項(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)生成重寫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 房價(jià)模塊

2.2.1 總覽

端口號(hào) 8083

 url接口 /price

參數(shù)名稱 :houseId

參數(shù)類型 : int

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

 url接口 /HouseInPrice

參數(shù)名稱 :null

參數(shù)類型 : null

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

 url接口 /houseAndPrice

參數(shù)名稱 :null

參數(shù)類型 : null

說明 : 獲取到房子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)問題會(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è)類來作為我們引入模塊的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 

/**
 * 描述:     房價(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

/**
 * 描述:     房價(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

/**
 * 描述:     房價(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 開發(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 開發(fā)多了獲取地址這一步是對(duì)ip服務(wù)進(jìn)行解耦 。

Eureka client改寫

我們把強(qiáng)兩個(gè)模塊進(jìn)行Eureka Client改寫也很簡單,只用在啟動(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)問題,我們不希望整個(gè)系統(tǒng)都崩潰掉,那我們可以采取降級(jí)的手段,雖然不能返回正常的數(shù)據(jù)卻可以保證系統(tǒng)正常運(yùn)行。運(yùn)用斷路器我們不僅要配置配置文件,加依賴,還要書寫一個(gè)短路類(參見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 過濾器

pre 過濾器在路由請(qǐng)求之前運(yùn)行
route 過濾器可以處理請(qǐng)求的實(shí)際路由
post 路由請(qǐng)求后運(yùn)行過濾器
error如果在處理請(qǐng)求的過程中發(fā)生錯(cuò)誤,則過濾器將運(yùn)行

這里寫一個(gè)前置一個(gè)后置

/**
 * 描述:     前置過濾器
 */
@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;
    }
    //通過過濾器將要實(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;
    }
}
/**
 * 描述:     后置過濾器
 */
@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í)現(xiàn)了

總結(jié)

1 通過這個(gè)項(xiàng)目我們了解了SpringCloud的幾個(gè)重要組件

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

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

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

5 使用到了負(fù)載均衡

6 使用到了熔斷器

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

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

相關(guān)文章

  • Java中注解的常見用法總結(jié)

    Java中注解的常見用法總結(jié)

    注解(Annotation),也叫元數(shù)據(jù),是JDK1.5及以后版本引入的一個(gè)特性,本文主要為大家介紹了注解的常見用法,需要的小伙伴可以參考一下
    2023-07-07
  • springboot整合sentinel接口熔斷的實(shí)現(xiàn)示例

    springboot整合sentinel接口熔斷的實(shí)現(xiàn)示例

    為了防止慢接口導(dǎo)致的服務(wù)阻塞,可以通過添加熔斷處理來避免應(yīng)用的大量工作線程陷入阻塞,保證其他接口的正常運(yùn)行,本文介紹了如何使用Spring Boot與Sentinel進(jìn)行接口熔斷的配置與實(shí)現(xiàn),感興趣的可以了解一下
    2024-09-09
  • java 數(shù)據(jù)結(jié)構(gòu)基本算法希爾排序

    java 數(shù)據(jù)結(jié)構(gòu)基本算法希爾排序

    這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)基本算法希爾排序的相關(guān)資料,需要的朋友可以參考下
    2017-08-08
  • SpringBoot監(jiān)控SQL運(yùn)行情況的流程步驟

    SpringBoot監(jiān)控SQL運(yùn)行情況的流程步驟

    Druid是Java語言中最好的數(shù)據(jù)庫連接池,雖然?HikariCP?的速度稍快,但是,Druid能夠提供強(qiáng)大的監(jiān)控和擴(kuò)展功能?,也是阿里巴巴的開源項(xiàng)目,本文給大家介紹了SpringBoot監(jiān)控SQL運(yùn)行情況的流程步驟,需要的朋友可以參考下
    2024-03-03
  • 解決Springboot 2 的@RequestParam接收數(shù)組異常問題

    解決Springboot 2 的@RequestParam接收數(shù)組異常問題

    這篇文章主要介紹了解決Springboot 2 的@RequestParam接收數(shù)組異常問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java語言實(shí)現(xiàn)簡單FTP軟件 FTP上傳下載管理模塊實(shí)現(xiàn)(11)

    Java語言實(shí)現(xiàn)簡單FTP軟件 FTP上傳下載管理模塊實(shí)現(xiàn)(11)

    這篇文章主要為大家詳細(xì)介紹了Java語言實(shí)現(xiàn)簡單FTP軟件,F(xiàn)TP本地文件管理模塊的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • SpringBoot集成geodesy實(shí)現(xiàn)距離計(jì)算功能

    SpringBoot集成geodesy實(shí)現(xiàn)距離計(jì)算功能

    Geodesy:大地測(cè)量學(xué)的神奇力量 Geodesy,又稱大地測(cè)量學(xué),是一門研究地球形狀、大小及其重力場(chǎng)的學(xué)科,在地球距離計(jì)算中,它扮演著至關(guān)重要的角色,故本文給大家介紹了SpringBoot集成geodesy實(shí)現(xiàn)距離計(jì)算功能,感興趣的朋友可以參考下
    2024-06-06
  • 使用Spring注入Hibernate驗(yàn)證框架

    使用Spring注入Hibernate驗(yàn)證框架

    這篇文章主要介紹了使用Spring注入Hibernate驗(yàn)證框架方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java 反射機(jī)制詳解及實(shí)例

    Java 反射機(jī)制詳解及實(shí)例

    這篇文章主要介紹了Java 反射機(jī)制詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • SpringSecurity整合springBoot、redis實(shí)現(xiàn)登錄互踢功能

    SpringSecurity整合springBoot、redis實(shí)現(xiàn)登錄互踢功能

    這篇文章主要介紹了SpringSecurity整合springBoot、redis實(shí)現(xiàn)登錄互踢,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-05-05

最新評(píng)論

惠州市| 洪泽县| 乌兰县| 定安县| 固阳县| 惠州市| 汉阴县| 清丰县| 太和县| 辉县市| 织金县| 淄博市| 台南县| 丁青县| 岑溪市| 达拉特旗| 收藏| 娱乐| 盈江县| 广平县| 瓦房店市| 蓬安县| 吉木乃县| 揭东县| 余江县| 雷州市| 尚义县| 东莞市| 嫩江县| 岳西县| 广河县| 石狮市| 顺昌县| 通州市| 关岭| 郧西县| 叶城县| 湘潭县| 金溪县| 江源县| 卫辉市|