springcloud 相同服务名_JAVA语言之Spring-cloud 服务发现与消费(以ribbon为例)

本文主要向大家介绍了JAVA语言的Spring-cloud 服务发现与消费,通过具体的实例让大家了解,希望对大家学习JAVA语言有所帮助。

说明:

ribbon是spring-cloud中作为服务消费者的一种角色,客户端可以通过它来对服务提供者的服务进行消费,

比如本例中是服务提供者注册到注册中心,服务提供者提供了一个服务接口,返回一个hello字符串,我们通过ribbon将这个接口调用,再不暴露真实服务提供者的地址的同时,获取服务提供者的服务

前提:

按照之前几个教程,搭建出注册中心、服务提供者。这里可以使用分片的注册中心,也可以不使用,这里暂时定为使用之前搭好的分片注册中心,服务提供者仅提供一个即可。

准备工作:

1、启动注册中心

按照之前的教程,分别使用peer1和peer2进行启动两个分片的注册中心,如果是单节点直接启动项目即可

启动后可以查看下localhost:1111或localhost:1112,如图

2、启动服务提供者

这里为了查看负载均衡的情况,所以需要启动两个服务提供者

按照之前教程中,分别开启两个terminal进行指定端口启动(两个相同的服务也可以在各自的配置文件中配置不同的端口),两个终端指令分别如下:

cd target

java -jar SpringCloudDemo-0.0.1-SNAPSHOT.jar --server.port=8080

复制代码

cd target

java -jar SpringCloudDemo-0.0.1-SNAPSHOT.jar --server.port=8081

复制代码

启动结果:

至此,准备工作已经完成

正文:

1、ribbon服务搭建

新建一个maven项目,不使用模板即可。项目名为robbin-customer,导入依赖参考如下:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.hellxz

ribbon-customer

1.0-SNAPSHOT

org.springframework.boot

spring-boot-starter-parent

1.5.9.RELEASE

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-eureka

RELEASE

org.springframework.cloud

spring-cloud-starter-ribbon

RELEASE

org.springframework.cloud

spring-cloud-dependencies

RELEASE

import

pom

复制代码

新建springboot启动类,将RestTemplate交给Spring容器进行管理

package com.cnblogs.hellxz;

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;

/**

* @Author : Hellxz

* @Description: 消费应用

* @Date : 2018/4/16 15:45

*/

@EnableDiscoveryClient

@SpringBootApplication

public class CustomerApplication {

[url=home.php?mod=space&uid=4377]@Bean[/url] //将此Bean交给spring容器

@LoadBalanced //通过此注解开启负载均衡

RestTemplate restTemplate(){

return new RestTemplate();

}

public static void main(String[] args) {

SpringApplication.run(CustomerApplication.class, args);

}

}

复制代码

在src/resources目录下创建一个application.yml进行配置注册信息相关,本文使用了分片的注册中心,单节点请配置一个defaltZone即可

server:

port: 9000 #为ribbon-customer指定服务端口

spring:

application:

name: ribbon-customer #指定应用名

#指定eureka注册中心地址

eureka:

client:

serviceUrl:

defaultZone: http://peer1:1111/eureka/,http://peer2:1112/eureka/

复制代码

在启动类同级目录创建CustomerController,注入RestTemplate进行调用服务接口

package com.cnblogs.hellxz;

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

import org.springframework.web.client.RestTemplate;

/**

* @Author : Hellxz

* @Description: 消费者应用

* @Date : 2018/4/16 15:54

*/

@RestController

public class CustomerController {

@Autowired

//注入restTemplate

private RestTemplate restTemplate;

@RequestMapping(value="/ribbon-customer", method=RequestMethod.GET)

public String helloCustomer(){

//这里注释掉,因为之前想当然使用了直链访问服务提供者的接口,这样是不会返回结果的,而且会报错

//return restTemplate.getForEntity("http://localhost:8080/hello",String.class).getBody();

//使用restTemplate调用微服务接口

return restTemplate.getForEntity("http://hello-service/hello", String.class).getBody();

}

}

复制代码

注意:上述代码第24行给出了一个错误的演示,如果出现访问ribbon接口报错,出现白色报错页面,请检查这里

至此ribbon消费者应用搭建完成,启动测试

测试:

访问http://localhost:1111/ 我们发现这个ribbon消费者应用已经注册到注册中心中了

访问http://localhost:9000/ribbon-customer

还记得服务提供者项目中的如图方法中如果有访问则会打印信息,因为启动了两个服务提供者,这里可以测试ribbon的负载均衡

查看服务提供者输出

而第二个终端没有,依旧显示到Resolving eureka endpoints via configuration这行

刷新页面查看终端,由于ribbon的默认负载均衡的实现是轮询的,所以可能出现多次访问同一服务,多刷新几次页面,一定会在另一个Termical中显示的!

结语:

Ribbon作为服务消费者,可以在用户获取到服务提供者提供的服务的同时,不向用户暴露接口地址。可以看到,这里调用服务接口的时候使用的是服务提供者的服务名代替主机名,这在服务治理框架中,这种特性很重要。

希望这篇文章可以帮助到你,总之同学们,IT资讯尽在职坐标。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值