基于spring-cloud的微服务(2) eureka服务提供方的注册和消费方的消费
启动Eureka注册中心之后,服务提供方就可以注册到Eureka上去(作为一个Eureka的客户端)
我们使用IDEA提供的spring initializer来新建一个springcloud项目

填写相关的包名等信息:

选择web和Erureka Discorvery

填写Gradle等相关信息完成创建

我们需要在启动类里边增加注解 @EnableDiscoveryClient 来标识这是一个Eureka客户端
启动类的代码如下:
package com.jiaoyiping.springcloud.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
创建一个我们用来真正提供服务的Controller(在本例中,我们假设从路径中取到两个int值,将他们的和用字符形式返回,代码如下):
package com.jiaoyiping.springcloud.provider;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProductionController {
//简单计算两个数字的和,转换为字符串返回
@RequestMapping(value = "/add/{a}/{b}")
public String add(@PathVariable("a") int a, @PathVariable("b") int b) {
return (a + b) + "";
}
}
修改应用的配置文件,指定服务的名称,网卡信息和eureka等相关信息:配置文件如下:
spring:
application:
name: provider
cloud:
inetutils:
preferred-networks: 192.168.1.
server:
port: 8082
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8081/eureka
instance:
prefer-ip-address: true
instance-id: 192.168.1.5:${server.port}
运行启动类,可以在eureka的页面上看到一个名为provider的服务已经注册上去了:

服务的消费方:
服务的消费方也是和服务提供方一样,也会到Eureka中去注册,按照上边的步骤,我们新建一个consumer项目,比前边的provider多加上Feign的依赖

在启动类上边,多加两个注解:
@EnableFeignClients
@EnableDiscoveryClient
启动类的代码如下:
package com.jiaoyiping.springcloud.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
新建一个ProductControllerRemote接口,作为FeignClient,里边定义的方法和服务提供方的方法一致;
package com.jiaoyiping.springcloud.consumer;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(name = "provider")
public interface ProductControllerRemote {
@RequestMapping(value = "/add/{a}/{b}")
String add(@PathVariable("a") int a, @PathVariable("b") int b);
}
@FeignClient注解的name,指的是注册到eureka上的应用的名称(在服务提供方的spring的配置文件里配置) 接口的方法映射到服务提供方的相应的方法上,只需要定义,不需要实现,Feign会自动调用服务提供方的相应的方法
使用@FeignClient注解的这个类,可以作为一个spring的bean,注入到需要的地方去,直接调用相应的方法即可
作为例子,我们在消费方新定义一个Controller,来调用ProductControllerRemote:
package com.jiaoyiping.springcloud.consumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsumerController {
@Autowired
private ProductControllerRemote productControllerRemote;
@RequestMapping("/getresult/{a}/{b}")
public String getResult(@PathVariable("a") int a, @PathVariable("b") int b) {
return productControllerRemote.add(a, b);
}
}
然后是消费方的配置文件,需要配置Eureka的地址和服务的端口等信息:
spring:
application:
name: cunsumer
cloud:
inetutils:
preferred-networks: 192.168.1.
server:
port: 8083
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8081/eureka/
instance:
prefer-ip-address: true
instance-id: 192.168.1.5:${server.port}
启动消费方的启动类,可以看到,消费方也被注册到Eureka中去了:

使用postman调用消费方对外提供的接口,我们可以看到,消费方调用了服务提供方提供的服务,正确的返回了结果:

基于spring-cloud的微服务(2) eureka服务提供方的注册和消费方的消费的更多相关文章
- 基于Spring Cloud的微服务入门教程
(本教程的原地址发布在本人的简书上:http://www.jianshu.com/p/947d57d042e7,若各位看官有什么问题或不同看法请在这里或简书留言,谢谢!) 本人也是前段时间才开始接触S ...
- 干货|基于 Spring Cloud 的微服务落地
转自 微服务架构模式的核心在于如何识别服务的边界,设计出合理的微服务.但如果要将微服务架构运用到生产项目上,并且能够发挥该架构模式的重要作用,则需要微服务框架的支持. 在Java生态圈,目前使用较多的 ...
- 基于Spring Cloud的微服务落地
微服务架构模式的核心在于如何识别服务的边界,设计出合理的微服务.但如果要将微服务架构运用到生产项目上,并且能够发挥该架构模式的重要作用,则需要微服务框架的支持. 在Java生态圈,目前使用较多的微服务 ...
- 基于 Spring Cloud 的微服务架构实践指南(下)
show me the code and talk to me,做的出来更要说的明白 本文源码,请点击learnSpringCloud 我是布尔bl,你的支持是我分享的动力! 一.引入 上回 基于 S ...
- Spring Cloud(二):Eureka 服务注册中心
前言 服务治理 随着业务的发展,微服务应用也随之增加,这些服务的管理和治理会越来越难,并且集群规模.服务位置.服务命名都会发生变化,手动维护的方式极易发生错误或是命名冲突等问题.而服务治理正是为了解决 ...
- 基于 Spring Cloud 的微服务架构实践指南(上)
show me the code and talk to me,做的出来更要说的明白 GitHub 项目learnSpringCloud同步收录 我是布尔bl,你的支持是我分享的动力! 一. 引入 上 ...
- Spring Cloud(二)服务提供者 Eureka + 服务消费者(rest + Ribbon)
Ribbon是什么? Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起.Ribbon客户端组件提供一系列完善的配置项如连接超时 ...
- spring cloud 系列第1篇 —— eureka 服务的注册与发现 (F版本)
源码仓库地址:https://github.com/heibaiying/spring-samples-for-all 一.eureka 简介 Spring Cloud Eureka使用Netflix ...
- Spring Cloud(四)服务提供者 Eureka + 服务消费者 Feign
上一篇文章,讲述了如何通过RestTemplate + Ribbon去消费服务,这篇文章主要讲述如何通过Feign去消费服务. Feign简介 Feign是一个声明式的伪Http客户端,它使得写Htt ...
- 画了一张基于Spring Cloud的微服务系统架构图
随机推荐
- 大数据学习笔记01-HDFS-集群安装
安装 下载 Hadoop,以2.7.5版本为例 在虚拟机上创建目录bigdata,即执行mkdir bigdata 上传到master机器节点的目录~/bigdata下(可以用FileZilla等ft ...
- “A configuration with this name already exists” error in eclipse run configurations
“A configuration with this name already exists” error in eclipse run configurations Is there a way t ...
- .net framework 4.0 在 VS2010 安装目录下位置 dotNetFx40_Full_x86_x64.exe在磁盘哪个目录?
.net framework 4.0 在 VS2010 安装目录下位置 dotNetFx40_Full_x86_x64.exe在磁盘哪个目录? 使用VS2010开发应用程序完毕后,在发布应用程序时,常 ...
- Win7下telnet使用
出于安全考虑,win7已经禁用了telnet这一功能, telnet是明文传输的,安全性很差. 知道了这一点就不奇怪为什么在win7下不能使用telnet了,下面就详细介绍下如何重新开启telnet服 ...
- 单例模式__new__
单例模式,使用__new__ __new__是构造函数, __init__是初始化方法,先调用了__new__返回了实例,__init__给这个实例初始化绑定一些属性. class Singleton ...
- Oauth2.0(一):为什么需要 Oauth2.0 协议?
假设有两家互联网企业 A 和 B,其中 B 是一家提供相片云存储的公司.即 B 的用户可以把相片上传到 B 网站上长期保存,然后可以在不同的设备上查看.某一天,A 和 B 谈成了一项合作:希望 B 用 ...
- Java适配器模式的简单应用
对于刚从工厂生产出来的商品,有些功能并不能完全满足用户的需要.因此,用户通常会对其进行一定的改装工作.编写程序为普通的汽车增加GPS定位功能,借此演示适配器模式的用法. 思路分析: 这个问题的需求是, ...
- Go之继承的实现
go的继承是使用匿名字段来实现的 package util //----------------Person---------------- type Person struct { Name str ...
- ios开发之--调试方法
概述 基本操作 全局断点 条件断点 开启僵尸对象 LLDB命令 概述 在开发项目的工程中,肯定会遇到各种各样的bug,且大多数的bug都和自己有关:那么在和bug斗智斗勇的过程中,如果能快速准确的一击 ...
- /var/spool/postfix/maildrop/ 中有大量的文件
今天查看硬盘剩余的容量,发现‘/’目录下占用了大量的空间:可我在这个目录下面没有放什么东西:仔细查看在/var/spool/postfix/maildrop/ 中发现了大量的文件.怎么会有这么多的文件 ...