中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringCloud服務消費有哪幾種方式?

發布時間:2020-06-23 16:36:43 來源:網絡 閱讀:444 作者:專注地一哥 欄目:編程語言

一、使用LoadBalancerClient
LoadBalancerClient接口的命名中,可以看出這是一個負載均衡客戶端的抽象定義,spring提供了一個實現
org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient
1、pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring boot test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2、application.yml
server:
#服務端口號
port: 8080
spring:
application:
#服務名稱
name: vis-basic-report
thymeleaf:
cache: false
cloud:
consul:
host: 192.168.12.125
port: 8500
discovery:
#是否需要注冊到consul中
register: true
#服務地址直接為IP地址
hostname: 192.168.12.1
management:
endpoints:
web:
exposure:
include: '*'
3、啟動類
package com.wzl.springcloud.basic.report;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;@SpringBootApplication
br/>@SpringBootApplication
public class ReportServerApplication {
public static void main(String[] args) {
SpringApplication.run(ReportServerApplication.class, args);
}
}
4、相關實現類
package com.wzl.springcloud.basic.report.service;
import java.util.List;
import com.wzl.springcloud.basic.report.vo.City;
import com.wzl.springcloud.basic.report.vo.WeatherResponse;
public interface WeatherReportService {
// 根據城市ID同步天氣
WeatherResponse getDataByCityId(String cityId);
// 根據城市name同步天氣
WeatherResponse getDataByCityName(String cityName);
// 獲取所有城市列表
List<City> getDataByCities();
}
package com.wzl.springcloud.basic.report.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSON;
import com.wzl.springcloud.basic.report.service.WeatherReportService;
import com.wzl.springcloud.basic.report.vo.City;
import com.wzl.springcloud.basic.report.vo.WeatherResponse;
/**

  • LoadBalancer客戶端*/
    @Service
    br/>*/
    @Service
    br/>@Autowired

    @Override
    public WeatherResponse getDataByCityId(String cityId) {
    WeatherResponse weatherResponse = null;
    ServiceInstance serviceInstance = loadBalancer.choose("vis-basic-weather");
    String uri = serviceInstance.getUri().toString() + "/weather/cityId/" + cityId;
    // 調用服務接口來獲取
    ResponseEntity<String> respString = new RestTemplate().getForEntity(uri, String.class);
    // 判斷ResponseEntity的狀態碼是否為200,為200時取出strBody
    if (respString.getStatusCodeValue() == 200) {
    String jsonStr = respString.getBody();
    weatherResponse = JSON.parseObject(jsonStr, WeatherResponse.class);
    }
    return weatherResponse;}
    @Override
    br/>}
    @Override
    WeatherResponse weatherResponse = null;
    ServiceInstance serviceInstance = loadBalancer.choose("vis-basic-weather");
    String uri = serviceInstance.getUri().toString() + "/weather/cityName/" + cityName;
    // 調用服務接口來獲取
    ResponseEntity<String> respString = new RestTemplate().getForEntity(uri, String.class);
    // 判斷ResponseEntity的狀態碼是否為200,為200時取出strBody
    if (respString.getStatusCodeValue() == 200) {
    String jsonStr = respString.getBody();
    weatherResponse = JSON.parseObject(jsonStr, WeatherResponse.class);
    }
    return weatherResponse;
    }
    function(){ //AxiTrader返傭:www.kaifx.cn/broker/axitrader.html@Override
    br/>@Override
    List<City> cityList = null;
    ServiceInstance serviceInstance = loadBalancer.choose("vis-basic-city");
    String uri = serviceInstance.getUri().toString() + "/cities/getList";
    // 調用服務接口來獲取
    ResponseEntity<String> respString = new RestTemplate().getForEntity(uri, String.class);
    // 判斷ResponseEntity的狀態碼是否為200,為200時取出strBody
    if (respString.getStatusCodeValue() == 200) {
    String jsonStr = respString.getBody();
    cityList = JSON.parseArray(jsonStr, City.class);
    }
    return cityList;
    }
    }
    二、使用Ribbon
    Spring Cloud Ribbon是基于Netflix Ribbon實現的一套客戶端負載均衡的工具。它是一個基于HTTP和TCP的客戶端負載均衡器。它可以通過在客戶端中配置ribbonServerList來設置服務端列表去輪詢訪問以達到均衡負載的作用。
    1、pom.xml
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.7.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.47</version>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- spring boot test -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
    </dependencies>
    2、application.yml
    server:
    #服務端口號
    port: 8080
    spring:
    application:
    #服務名稱
    name: vis-basic-report
    thymeleaf:
    cache: false
    cloud:
    consul:
    host: 192.168.12.125
    port: 8500
    discovery:
    #是否需要注冊到consul中
    register: true
    #服務地址直接為IP地址
    hostname: 192.168.12.1
    management:
    endpoints:
    web:
    exposure:
    include: '*'
    3、啟動類 & RestConfiguration
    package com.wzl.springcloud.basic.report;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
    br/>@SpringBootApplication
    public class ReportServerApplication {
    public static void main(String[] args) {
    SpringApplication.run(ReportServerApplication.class, args);
    }
    }
    package com.wzl.springcloud.basic.report.config;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.web.client.RestTemplateBuilder;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.client.RestTemplate;@Configuration
    br/>@Configuration
    br/>@Autowired
    br/>@Bean
    public RestTemplate restTemplate() {
    return builder.build();}
    }
    @LoadBalanced注解表明這個restRemplate開啟負載均衡的功能,用LoadBalancerClient配置,并且會替換URL中的服務名稱為具體的IP地址
    br/>}
    }
    @LoadBalanced注解表明這個restRemplate開啟負載均衡的功能,用LoadBalancerClient配置,并且會替換URL中的服務名稱為具體的IP地址
    package com.wzl.springcloud.basic.report.service;
    import java.util.List;
    import com.wzl.springcloud.basic.report.vo.City;
    import com.wzl.springcloud.basic.report.vo.WeatherResponse;
    public interface WeatherReportService {
    // 根據城市ID同步天氣
    WeatherResponse getDataByCityId(String cityId);
    // 根據城市name同步天氣
    WeatherResponse getDataByCityName(String cityName);
    // 獲取所有城市列表
    List<City> getDataByCities();
    }
    package com.wzl.springcloud.basic.report.service.impl;
    import java.util.List;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Service;
    import org.springframework.web.client.RestTemplate;
    import com.alibaba.fastjson.JSON;
    import com.wzl.springcloud.basic.report.service.WeatherReportService;
    import com.wzl.springcloud.basic.report.vo.City;
    import com.wzl.springcloud.basic.report.vo.WeatherResponse;
    /**

  • Ribbon客戶端*/
    @Service
    br/>*/
    @Service
    br/>@Autowired
    private RestTemplate restTemplate;

    @Override
    public WeatherResponse getDataByCityId(String cityId) {
    WeatherResponse weatherResponse = null;
    String uri = "http://vis-basic-weather/weather/cityId/" + cityId;
    // 調用服務接口來獲取
    ResponseEntity<String> respString = restTemplate.getForEntity(uri, String.class);
    // 判斷ResponseEntity的狀態碼是否為200,為200時取出strBody
    if (respString.getStatusCodeValue() == 200) {
    String jsonStr = respString.getBody();
    weatherResponse = JSON.parseObject(jsonStr, WeatherResponse.class);
    }
    return weatherResponse;}
    @Override
    br/>}
    @Override
    WeatherResponse weatherResponse = null;
    String uri = "http://vis-basic-weather/weather/cityName/" + cityName;
    // 調用服務接口來獲取
    ResponseEntity<String> respString = restTemplate.getForEntity(uri, String.class);
    // 判斷ResponseEntity的狀態碼是否為200,為200時取出strBody
    if (respString.getStatusCodeValue() == 200) {
    String jsonStr = respString.getBody();
    weatherResponse = JSON.parseObject(jsonStr, WeatherResponse.class);
    }
    return weatherResponse;}
    @Override
    br/>}
    @Override
    List<City> cityList = null;
    String uri = "http://vis-basic-city/cities/getList";
    // 調用服務接口來獲取
    ResponseEntity<String> respString = restTemplate.getForEntity(uri, String.class);
    // 判斷ResponseEntity的狀態碼是否為200,為200時取出strBody
    if (respString.getStatusCodeValue() == 200) {
    String jsonStr = respString.getBody();
    cityList = JSON.parseArray(jsonStr, City.class);
    }
    return cityList;
    }
    }

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

龙里县| 枣阳市| 扶绥县| 乌拉特中旗| 漾濞| 澎湖县| 彰武县| 东台市| 闵行区| 诏安县| 正阳县| 德化县| 房产| 黄梅县| 大同县| 资兴市| 南投市| 石台县| 东方市| 顺昌县| 偃师市| 营山县| 赫章县| 峨山| 喀喇沁旗| 南京市| 北安市| 蕲春县| 富锦市| 五原县| 延川县| 岗巴县| 新安县| 乌海市| 芒康县| 来凤县| 庆安县| 天柱县| 东阳市| 平南县| 屯门区|