SpringCloud服务消费有哪几种方式?(%d,java,几种方式,编程语言)

时间:2024-04-29 07:44:35 作者 : 石家庄SEO 分类 : 编程语言
  • TAG :

    SpringCloud%E6%9C%8D%E5%8A%A1%E6%B6%88%E8%B4%B9%E6%9C%89%E5%93%AA%E5%87%A0%E7%A7%8D%E6%96%B9%E5%BC%8F%EF%BC%9F

一、使用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;
/**

本文:SpringCloud服务消费有哪几种方式?的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:好程序员Java学习路线分享Redis快速入门下一篇:

5 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18