第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

試水Spring Cloud Hystrix

標(biāo)簽:
Java

Spring Cloud Hystrix是一个容错库,它实现了断路器模式,使得当服务发生异常时,会自动切断连接,并将请求引导至预设的回调方法。

服务端

在Spring Tool Suite的文件菜单中,点击新建Spring Starter Project。建立一个普通的Restful风格的服务。
5ba7c04a00018d1505530740.jpg

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@SpringBootApplicationpublic class SpringcloudHystrixServerApplication {    public static void main(String[] args) {
        SpringApplication.run(SpringcloudHystrixServerApplication.class, args);
    }    @RequestMapping(value = "/message")    public String getMessage() {        return "Hello World!";
    }
}

application.properties文件中配置服务的端口,server.port=8200

服务启动后,可以在浏览器查看相应接口。
5ba7c04b0001c9e703370075.jpg

客户端

再建立一个客户端应用程序,在创建时选择Hystrix,Hystrix Dashboard,Actuator和Web模块。
5ba7c04b00012e0405530740.jpg

项目创建完成后,添加一个Service,其中包括调用服务端接口的方法及一个回调方法。注意这里@HystrixCommand的用法。

import java.net.URI;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;@Servicepublic class MessageService {    private final RestTemplate restTemplate;    public MessageService(RestTemplate rest) {        this.restTemplate = rest;
    }    @HystrixCommand(fallbackMethod = "reliable")    public String getMessage() {
        URI uri = URI.create("http://localhost:8200/message");        return this.restTemplate.getForObject(uri, String.class);
    }    public String reliable() {        return "Woo, something wrong!";
    }
}

在客户端的入口方法加上@EnableCircuitBreaker标记,并把它的端口设为server.port=8300

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.client.RestTemplateBuilder;import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;import org.springframework.context.annotation.Bean;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@EnableHystrixDashboard@EnableCircuitBreaker@RestController@SpringBootApplicationpublic class SpringcloudHystrixClientApplication {    @Autowired
    private MessageService messageService;    @Bean
    public RestTemplate rest(RestTemplateBuilder builder) {        return builder.build();
    }    public static void main(String[] args) {
        SpringApplication.run(SpringcloudHystrixClientApplication.class, args);
    }    @RequestMapping("/message")    public String getMessge() {        return messageService.getMessage();
    }
}

启动客户端后,如果在浏览器里看到页面能正常获取服务端的数据,说明当前客户端与服务端运作都是正常的。
5ba7c04b000184e603430077.jpg

然后,停止服务端,让情况出现异常。

刷新页面,可以看到这次的结果也在预料之内,当客户端调用服务端失败后,通过Hystrix的作用,自动切换至调用预设的回调方法。

5ba7c04b0001ce1403480071.jpg

仪表盘

Hystrix自带可视化仪表盘,在上面的客户端代码中,入口方法除了增加了@EnableCircuitBreaker标记外,还有@EnableHystrixDashboard。这样的设置便可以启用Hystrix的仪表盘。

不过在application.properties文件还需要加上以下配置,以避免“Unable to connect to Command Metric Stream”错误。

management.endpoints.web.exposure.include=hystrix.stream
management.endpoints.web.base-path=/

当客户端被启动后,使用http://localhost:8300/hystrix路径可以直接访问仪表盘。

5ba7c04b0001759c13510576.jpg

之后在Hystrix Dashboard下面的地址栏内填上http://localhost:8300/hystrix.stream,再点击Monitor Stream按钮,监控结果一览无遗。

5ba7c04c0001627a10520460.jpg

原文出处:https://www.cnblogs.com/kenwoo/p/9693980.html  

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶(hù)
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消