首页 > 编程语言 > 详细

SpringCloud服务消费有哪几种方式?

时间:2019-09-23 16:21:48      阅读:110      评论:0      收藏:0      [点我收藏+]

一、使用LoadBalancerClient

LoadBalancerClient接口的命名中,可以看出这是一个负载均衡客户端的抽象定义,spring提供了一个实现

org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient

1pom.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-consul-discovery</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>

2application.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

@EnableDiscoveryClient

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

public class WeatherReportServiceLoadBalancerClientImpl implements WeatherReportService {

@Autowired

private LoadBalancerClient loadBalancer;

@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

public WeatherResponse getDataByCityName(String cityName) {

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;

}

@Override

public List<City> getDataByCities() {

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实现的一套客户端负载均衡的工具。它是一个基于HTTPTCP的客户端负载均衡器。它可以通过在客户端中配置ribbonServerList来设置服务端列表去轮询访问以达到均衡负载的作用。

1pom.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-consul-discovery</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>

2application.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

@EnableFeignClients

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

public class RestConfiguration {

@Autowired

private RestTemplateBuilder builder;

@Bean

@LoadBalanced

public RestTemplate restTemplate() {

return builder.build();

}

}

@LoadBalanced注解表明这个restRemplate开启负载均衡的功能,用外汇返佣LoadBalancerClient配置,并且会替换URL中的服务名称为具体的IP地址

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.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

public class WeatherReportServiceRibbonImpl implements WeatherReportService {

@Autowired

// rest客户端的封装

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

public WeatherResponse getDataByCityName(String cityName) {

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

public List<City> getDataByCities() {

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;

}

}

SpringCloud服务消费有哪几种方式?

原文:https://www.cnblogs.com/benming/p/11571940.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!