Spring Cloud(Dalston.SR5)--Eureka 服务消费
服务被注册、发布到 Eureka 服务器后,需要有程序去发现他,并且进行调用,称为服务消费,一个服务可能会部署多个实例,调用过程可能涉及负载均衡、服务器查找等问题,这些问题 Netflix 项目已经帮助我们解决了,并且 Spring Cloud 已经封装了一次,我们可以仅需编辑少量代码就可以实现服务调用。
- 创建项目
创建名称为 service-invoker 的 Spring Cloud 项目,修改 POM.xml 中增加以下依赖项:
<?xmlversion="1.0"encoding="UTF-8"?>
<projectxmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.lixue.webservice</groupId>
<artifactId>service-invoker</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/><!--lookupparentfromrepository-->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR5</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 创建 REST 调用
创建 InvokerHelloWorldController 类,用于调用之前创建的 helloworld-provider 的 REST 服务,在调用服务时输入的 url 为 http://服务名称:端口/uri 的方式调用,没有指定具体的服务地址,那为什么能够访问到我们的服务呢。
因为 Spring Cloud 对 RestTemplate 进行了扩展,在启动类中使用了 @LoadBalanced 注解进行标注,使得 RestTemplate 实例具有了访问分布式服务的能力,代码如下:
package org.lixue.serviceinvoker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
@RestController
public class InvokerHelloWorldController{
@Autowired
private RestTemplate restTemplate;
@RequestMapping(method=RequestMethod.GET,name="speak")
public String invokerSpeak(@RequestParam(value="body",required=false)String body)
throws UnsupportedEncodingException{
StringencodeBody="";
if(body!=null){
encodeBody="?body="+URLEncoder.encode(body,"utf-8");
}
returnrestTemplate.getForObject("http://helloworld-provider/speak"+encodeBody,String.class);
}
}
- 启动类启用 Eureka 客户端
在启动类中,使用 @EnableDiscoveryClient 注解来修改启动类,该注解使得服务调用有能力去 Eureka 中发现服务,RestTemplate 对象实例使用 @LoadBalanced 注解来标注,表示该对象实例具有了访问分布式服务的能力。
package org.lixue.serviceinvoker;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceInvokerApplication{
publicstaticvoidmain(String[]args){
SpringApplication.run(ServiceInvokerApplication.class,args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
- 基本配置
#配置应用名称
spring:
application:
name:service-invoker
#配置服务端口
server:
#eureka基本配置
eureka:
instance:
#配置应用名称,优先级低于spring.applicaton.name
appname:service-invoker
client:
#配置服务注册中心地址
service-url:
defaultZone:http://localhost:9000/eureka/
- 启动项目
按照顺序启动 Eureka 注册中心、服务提供者和项目,访问地址 http://localhost:8088/speak ,可以看到响应报文
hello world 的返回,表示服务调用正常
Spring Cloud(Dalston.SR5)--Eureka 服务消费的更多相关文章
- Spring Cloud(Dalston.SR5)--Eureka 服务实例健康检查
默认情况下,Eureka 客户端每隔 30 秒会发送一次心跳给服务器端,告知正常存活,但是,实际环境中有可能出现这种情况,客户端表面上可以正常发送心跳,但实际上服务是不可用的,例如,一个需要访问数据的 ...
- Spring Cloud(Dalston.SR5)--Eureka 注册中心高可用-服务提供和消费
由于 Eureka 注册中心只是在内存中保存服务注册实例,并且没有将服务注册实例进行同步,因此我们需要对服务提供和消费进行调整,需要指定服务提供和消费的注册.服务发现的具体Eureka 注册中心配置, ...
- Spring Cloud(Dalston.SR5)--Eureka 服务提供者
要使微服务应用向注册中心发布自己,首先需要在 pom.xml 配置文件中增加对 spring-boot-starter-eureka 的依赖,然后在主类中增加 @EnableDiscoveryClie ...
- Spring Cloud(Dalston.SR5)--Eureka 注册中心搭建
基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务治理可以说是微服务架构中最为核心和基础的模块,他主要用来实现各个微服务实例的自动化注册与发现 服务注册:在 ...
- Spring Cloud(Dalston.SR5)--Eureka 常用配置
配置参数 默认值 说明 服务注册中心配置 Bean类:org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean eu ...
- Spring Cloud(Dalston.SR5)--Eureka 注册中心高可用搭建
高可用集群 在微服务架构这样的分布式环境中,我们需要充分考虑发生故障的情况,所以在生产环境中必须对各个组件进行高可用部署,对与微服务和服务注册中心都需要高可用部署,Eureka 高可用实际上就是将自己 ...
- spring cloud深入学习(三)-----服务消费
在上一篇博文中简单实现了eureka-server以及eureka-provider,后面会实现eureka-cosumer,现在针对eureka做进一步的详解. 微服务整体架构 文字再美也没有图片直 ...
- Spring Cloud(Dalston.SR5)--Zuul 网关-微服务集群
通过 url 映射的方式来实现 zuul 的转发有局限性,比如每增加一个服务就需要配置一条内容,另外后端的服务如果是动态来提供,就不能采用这种方案来配置了.实际上在实现微服务架构时,服务名与服务实例地 ...
- 2.Spring Cloud初相识--------Eureka服务注册与消费
前言: 1.Eureka介绍: Spring Cloud Eureka,使用Netflix Eureka来实现服务注册与发现,它既包含了服务端组件,也包含了客户端组件,并且服务端与客户端均采用Java ...
随机推荐
- 广播多路访问链路上的OSPF
实验要求:配置OSPF 拓扑如下: 配置如下: R1 enableconfigure terminal interface l0ip address 192.168.10.1 255.255.255. ...
- Refused to execute inline event handler because it violates the following Content Security Policy directive: "xxx". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...')
/********************************************************************************* * Refused to exec ...
- linux 禁ping和开启ping方法
Linux 禁ping和开启ping操作# echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all如果要恢复,只要:# echo 0 > /pro ...
- inner join 与一般笛卡尔积的区别
inner join 与一般笛卡尔积的区别:inner join是笛卡尔积的特殊形式.如果有表a和表b,表a有m条记录,表b有n条记录,则一般笛卡尔积后得到的记录条数是m*n条,记录之间的组合是随意的 ...
- day 023-python 包
包 : 我 们创建的每个文件夹都可以被称之为包. 但是我们要注意, 在python2中规定.中包内必须存在 __init__.py文件. python3可有可无,但一般要求写上.创建包的目的不是为了 ...
- HDU 2058:The sum problem(数学)
The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 牛客国庆集训派对Day1 L-New Game!(最短路)
链接:https://www.nowcoder.com/acm/contest/201/L 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...
- C# 日常
var lines = JsonConvert.DeserializeObject<List<qqq>>(dataSource); 类参数 publist string d ...
- 《DSP using MATLAB》Problem 5.17
1.代码 %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% ...
- 如何查看你的VPS是什么虚拟化架构?
使用virt-what即可了 CentOS安装 virt-what yum install virt-what Debian/ubuntu 安装 virt-what apt-get install v ...