欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

Ribbon怎么在SpringCloud中使用-創(chuàng)新互聯(lián)

Ribbon怎么在SpringCloud中使用?針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站的關(guān)注點(diǎn)不是能為您做些什么網(wǎng)站,而是怎么做網(wǎng)站,有沒有做好網(wǎng)站,給創(chuàng)新互聯(lián)一個(gè)展示的機(jī)會來證明自己,這并不會花費(fèi)您太多時(shí)間,或許會給您帶來新的靈感和驚喜。面向用戶友好,注重用戶體驗(yàn),一切以用戶為中心。

搭建Eureka服務(wù)器

 配置 pom.xml,加入springCloud核心依賴、配置及eureka服務(wù)器依賴

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.13.RELEASE</version>
</parent>
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>Dalston.SR5</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>
<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
  </dependency>
</dependencies>

配置 application.yml(紅色部分是必須要寫的,黑色部分不寫也能正常運(yùn)行 但是建議寫上,在這里筆者將官網(wǎng)的代碼貼上)

server:
 port: 8761
eureka:
 instance:
  hostname: localhost
 client:
  registerWithEureka: false 禁止向eureka注冊服務(wù),因?yàn)樗约罕旧砭褪欠?wù)器
  fetchRegistry: false 這里不需要抓取注冊表
  serviceUrl:
   defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

創(chuàng)建啟動類:Application.java(將服務(wù)跑起來放著,稍后會用到)配置 pom.xml,加入springCloud核心依賴、配置及eureka服務(wù)依賴

@SpringBootApplication
@EnableEurekaServer
public class Application {
  
  public static void main(String[] args) {
    new SpringApplicationBuilder(Application.class).web(true).run(args);
  }
}

Ribbon怎么在SpringCloud中使用

服務(wù)提供者

配置 pom.xml,加入springCloud核心依賴、配置及eureka客戶端依賴

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.13.RELEASE</version>
</parent>
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>Dalston.SR5</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>
<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
  </dependency>
</dependencies>

配置 application.yml(需要使用defaultZone向服務(wù)器注冊服務(wù),否則就算該服務(wù)運(yùn)行起來了,但沒有向服務(wù)器注冊服務(wù),也是使用不了的)(name 這個(gè)名稱是顯示在服務(wù)列表中的名稱,養(yǎng)成好習(xí)慣,一定要起有意義的名稱)

spring:
 application:
  name: springCloud-ribbon-police
eureka:
 client:
  serviceUrl:
   defaultZone: http://localhost:8761/eureka/

因?yàn)樵摲?wù)是提供服務(wù)的,所以下面會建一個(gè)實(shí)體類及Controller用來對外提供服務(wù),創(chuàng)建實(shí)體類:Police.java

public class Police {
  private String id;// 警察編號,用來保存用戶輸入的參數(shù)
  private String url;// 處理請求的服務(wù)器url
  private String message;// 提示信息
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String getUrl() {
    return url;
  }
  public void setUrl(String url) {
    this.url = url;
  }
  public String getMessage() {
    return message;
  }
  public void setMessage(String message) {
    this.message = message;
  } 
}

創(chuàng)建對外提供服務(wù)的Controller:PoliceController.java(@RestController注解中包含了@Controller+@ResponseBody)

@RestController
public class PoliceController {

  @RequestMapping(value="/getPolice", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
  public Police getPolice(HttpServletRequest request){
    Police p = new Police();
    p.setUrl(request.getRequestURL().toString());
    p.setMessage("警察派出成功");
    return p;
  }
  
  @RequestMapping(value="/getPoliceById/{id}", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
  public Police getPolice(HttpServletRequest request, @PathVariable("id") String id){
    Police p = new Police();
    p.setId(id);
    p.setUrl(request.getRequestURL().toString());
    p.setMessage("指定警察派出成功");
    return p;
  }
}

因?yàn)槲覀円獪y試負(fù)載均衡,所以這里的服務(wù)提供者需要開啟多個(gè)服務(wù)實(shí)例,下面我們用讀取手動輸入端口號的方法,啟動多個(gè)服務(wù)實(shí)例,筆者在這里啟動了兩個(gè)服務(wù)實(shí)例:8080、8081

@SpringBootApplication
@EnableEurekaClient
public class PoliceApplication {
  
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String port = scan.nextLine();
    new SpringApplicationBuilder(PoliceApplication.class).properties("server.port="+port).run(args);
  } 
}

如下圖,出現(xiàn)了兩個(gè)服務(wù)實(shí)例,分別是:8080、8081,紅色的信息咱們先不管他,如果實(shí)在有看著不順眼的小伙伴,可以配置心跳(簡單的來說,就是配置服務(wù)器每隔多久檢查一次服務(wù)實(shí)例狀態(tài),如果某個(gè)服務(wù)因?yàn)槟承┰蛲5?不能用了,那么就將該服務(wù) 從服務(wù)列表中移除掉)

Ribbon怎么在SpringCloud中使用

服務(wù)調(diào)用者

 配置 pom.xml,加入springCloud核心依賴、配置及eureka客戶端依賴、Ribbon依賴

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.13.RELEASE</version>
</parent>
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>Dalston.SR5</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>
<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-ribbon</artifactId>
  </dependency>
</dependencies>

 配置 application.yml(這里也將該服務(wù)注冊到服務(wù)器,一定要進(jìn)行注冊)

server:
 port: 9090
spring:
 application:
  name: springCloud-ribbon-person
eureka:
 client:
  serviceUrl:
   defaultZone: http://localhost:8761/eureka/

 創(chuàng)建調(diào)用服務(wù)Controller:PersonController.java

RestTemplate 是由 Spring Web 模塊提供的工具類,與 SpringCloud 無關(guān),是獨(dú)立存在的

因 SpringCloud 對 RestTemplate 進(jìn)行了一定的擴(kuò)展,所以 RestTemplate 具備了負(fù)載均衡的功能

@RestController
@Configuration
public class PersonController {
  @Bean
  @LoadBalanced
  public RestTemplate getRestTemplate(){
    return new RestTemplate();
  }
  @RequestMapping("/getPolice")
  public String getPolice(){
    RestTemplate rt = getRestTemplate();
    String result = rt.getForObject("http://springCloud-ribbon-police/getPolice", String.class);
    return result;
  }
  @RequestMapping("/getPoliceById/{id}")
  public String getPoliceById(@PathVariable("id") String id){
    RestTemplate rt = getRestTemplate();
    String result = rt.getForObject("http://springCloud-ribbon-police/getPoliceById/"+id, String.class);
    return result;
  }
}

創(chuàng)建啟動類:PersonApplication.java

@SpringBootApplication
@EnableEurekaClient
public class PersonApplication {
  
  public static void main(String[] args) {
    new SpringApplicationBuilder(PersonApplication.class).web(true).run(args);
  } 
}

Ribbon怎么在SpringCloud中使用

到目前為止,eureka服務(wù)器、服務(wù)提供者、服務(wù)調(diào)用者(負(fù)載均衡)就已經(jīng)全寫好了,下面我們訪問接口,來試一下 服務(wù)到底能不能調(diào)通

我們分別調(diào)用:http://localhost:9090/getPolice、http://localhost:9090/getPoliceById/100

Ribbon怎么在SpringCloud中使用

Ribbon怎么在SpringCloud中使用

關(guān)于Ribbon怎么在SpringCloud中使用問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。

網(wǎng)站標(biāo)題:Ribbon怎么在SpringCloud中使用-創(chuàng)新互聯(lián)
本文路徑:http://aaarwkj.com/article18/isogp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、建站公司定制開發(fā)、網(wǎng)站排名企業(yè)建站、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
亚洲综合激情一区二区| 日韩毛片中文字幕在线观看 | 最新国产毛片久热精品视频| 午夜高清影院免费观看| 国产精品成人一区二区三| 日本av天堂中文字幕| 91桃色午夜福利视频| 最新亚洲av熟女播放| 久久草福利视频在线观看| 亚洲毛片一区在线播放| 青青草原天堂在线免费观看| 中文字幕日本乱码精品久久| 午夜福利主播一区二区| 一区二区五区日韩国产| 国产精品久久久久精品爆| 国内熟妇人妻色在线三级| 午夜香蕉av一区二区三区| 国产女同av一区二区三区| 国产姐弟操大率悠荡笕| av网址不卡在线免费观看| 欧美三级伦理片免费观看| 日本高清精品视频在线| 九九有点热以前的视频| 色婷婷一区二区三区四| 久草午夜福利视频免费观看| 欧美日韩国产精品精品| 色综合久久天天射天天干| 精品欧美日韩国产一区| av天堂久久人妻精品加勒比| 最新手机免费黄色av网站| 国产精品男人在线播放| 国产精品日本欧美一区二区| 亚洲成人av网址大全| 天堂av影片在线观看| 欧美精品三级不卡在线| 色老头视频一区二区三区| 日韩免费高清不卡视频| 亚洲成在人天堂一区二区| 久娜娜精品视频在线观看| 成人精品午夜福利视频| 日韩一二卡在线观看视频|