玩转springcloud(三):服务的提供者与调用者(注册于发现)
一、简介
上文我们实践了cloud的注册中心的单服务于多节点的搭建,房子造好了得有人来住不是,这篇我们实践下服务提供者于调用者的案例,也就是服务端和客户端的调用。
本文会设计三个module:注册中心(eureka),服务提供方(server),服务调用方(client)。其中注册中心,我们就用上文搞的,不能重复造轮子啊~~
简单说下项目执行及调用过程:首先启动注册中心,然后在启动服务端和客户端,服务端会注册到注册中心,
二、实践演练
服务提供方
我们在服务端创建一个接口可接收字符串参数,然后拼接这个参数输出("hello " + 参数 + ",this is first messge,我是8079服务哦")
1、老生常谈,先上pom依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
2、配置文件走起
spring:
application:
name: spring-cloud-eureka-producer
server:
port: 8079
eureka:
client:
serviceUrl:
defaultZone: http://xjy1:8097/eureka/
参数之前都结实过啦,这里不再赘述
3、启动类添加注解@EnableDiscoveryClient
package com.oldmonk.cloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /**
* @program: cloud
* @description: 启动类
* @author: xujingyang
* @create: 2019-10-09 12:06
**/
@SpringBootApplication
@EnableDiscoveryClient
public class ErkaProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ErkaProducerApplication.class);
}
}
添加@EnableDiscoveryClient注解后,项目就具有了服务注册的功能。启动工程后,就可以在注册中心的页面看到SPRING-CLOUD-PRODUCER服务。
4、提供的接口服务
package com.oldmonk.cloud.controller; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* @program: cloud
* @description: 测试接口
* @author: xujingyang
* @create: 2019-10-08 14:45
**/
@RestController
public class Hello { @RequestMapping("/hello")
public String index(String name) {
return "hello " + name + ",this is first messge,我是8079服务哦";
} }
5、启动注册中心,然后启动服务提供方,得下图

至此服务提供方就配置完成了!
服务调用方
1、老生常谈,先上pom依赖 ,与提供方是一样的
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
2、配置文件走起
spring:
application:
name: spring-cloud-eureka-consumer
server:
port: 9009
eureka:
client:
serviceUrl:
defaultZone: http://xjy1:8097/eureka/
3、启动类添加注解
package com.oldmonk.cloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients; /**
* @program: cloud
* @description: 启动类
* @author: xujingyang
* @create: 2019-10-09 12:06
**/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ErkaConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ErkaConsumerApplication.class);
}
}
@EnableDiscoveryClient:启用服务注册与发现@EnableFeignClients:启用feign进行远程调用
Feign是一个声明式Web Service客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。
4、客户端接口
package com.oldmonk.cloud.controller; import com.oldmonk.cloud.service.remote.HelloRemote;
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; /**
* @program: cloud
* @description: 测试接口
* @author: xujingyang
* @create: 2019-10-08 14:45
**/
@RestController
public class HelloController { @Autowired
HelloRemote remote; @RequestMapping("/hi/{name}")
public String index(@PathVariable("name") String name) {
return remote.hello(name);
} }
5、feign调用
package com.oldmonk.cloud.service.remote; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; /**
* @program: cloud-lx
* @description: 远程服务调用
* @author: xujingyang
* @create: 2019-10-09 13:07
**/
@FeignClient(name = "spring-cloud-eureka-producer")
public interface HelloRemote { @RequestMapping("/hello")
String hello(@RequestParam("name") String name);
}
参数必须保持一致与服务提供方的接口
6、启动

至此,服务调用方配置完成
测试
简单调用
1、先输入:http://localhost:8079/hello?name=xjy检查服务提供方服务是否正常

说服务提供方正常启动,提供的服务也正常。
2、浏览器中输入:http://localhost:9009/hi/xjy

说明客户端已经成功的通过feign调用了远程服务hello,并且将结果返回到了浏览器。
多节点负载均衡
1、把生产者配置文件做下改动
---
spring:
application:
name: spring-cloud-eureka-producer
profiles:
active: xjy1
server:
port: 8079
eureka:
client:
serviceUrl:
defaultZone: http://xjy1:8097/eureka/ ---
spring:
application:
name: spring-cloud-eureka-producer
profiles:
active: xjy2
server:
port: 8078
eureka:
client:
serviceUrl:
defaultZone: http://xjy1:8097/eureka/
2、controller也要修改下
package com.oldmonk.cloud.controller; import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* @program: cloud
* @description: 测试接口
* @author: xujingyang
* @create: 2019-10-08 14:45
**/
@RestController
public class Hello { @Value("${spring.profiles}")
private String profile; @RequestMapping("/hello")
public String index(String name) {
return "hello " + name + ",this is first messge,我是【" + profile + "】服务哦";
} }
2、打包,按照之前说的方式启动两次
启动2个服务形成集群注册中心,启动命令:
java -jar eureka-producer-cluster-0.0.-SNAPSHOT.jar --spring.profiles.active=xjy1
java -jar eureka-producer-cluster-0.0.-SNAPSHOT.jar --spring.profiles.active=xjy2
3、然后看到注册中心两个服务都注册进来了,加上消费方一共三个服务

4、然后在浏览器再次输入:http://localhost:9009/hi/xjy 进行测试:
第一次返回结果:hello xjy,this is first messge,我是【xjy1】服务哦
第二次返回结果:hello xjy,this is first messge,我是【xjy2】服务哦
不断的进行测试下去会发现两种结果交替出现,说明两个服务中心自动提供了服务均衡负载的功能。
如果我们将服务提供者的数量在提高为N个,测试结果一样,请求会自动轮询到每个服务端来处理。
三、案例源码
连接点不动时请复制粘贴此链接:https://github.com/oIdmonk/springcloud-xjy
代码基于boot2.1.9.RELEASE版本,cloudGreenwich.SR3版本
玩转springcloud(三):服务的提供者与调用者(注册于发现)的更多相关文章
- 十七、springcloud(三)服务的注册与调用
1.启动服务注册中心Eureka(见上篇) 启动成功后,暂时无服务 2.项目框架 3.创建服务提供者(spring-cloud-houge-provider)jar a.application.pro ...
- SpringCloud微服务小白入门之Eureka注册中心和服务中心搭建示例
一.注册中心配置文件 代码复制区域: spring: application: name: spring-cloud-server server: port: 7000 eureka: instanc ...
- 微服务通信之feign的注册、发现过程
前言 feign 是目前微服务间通信的主流方式,是springCloud中一个非常重要的组件.他涉及到了负载均衡.限流等组件.真正意义上掌握了feign可以说就掌握了微服务. 一.feign的使用 f ...
- SpringCloud的入门学习之概念理解、Eureka服务注册与发现入门
1.微服务与微服务架构.微服务概念如下所示: 答:微服务强调的是服务的大小,它关注的是某一个点,是具体解决某一个问题.提供落地对应服务的一个服务应用,狭意的看,可以看作Eclipse里面的一个个微服务 ...
- 微服务框架SpringCloud(Dalston版)学习 (一):Eureka服务注册与发现
eureka-server eureka服务端,提供服务的注册与发现,类似于zookeeper 新建spring-boot工程,pom依赖: <dependency> <groupI ...
- SpringColud Eureka的服务注册与发现
一.Eureka简介 本文中所有代码都会上传到git上,请放心浏览 项目git地址:https://github.com/839022478/Spring-Cloud 在传统应用中,组件之间的调用,通 ...
- 玩转SpringCloud(F版本) 三.断路器(Hystrix)RestTemplate+Ribbon和Feign两种方式
此文章基于: 玩转SpringCloud 一.服务的注册与发现(Eureka) 玩转SpringCloud 二.服务消费者(1)ribbon+restTemplate 转SpringCloud 二.服 ...
- 三. SpringCloud服务注册与发现
1. Eureka 1.1 Eureka理解 什么是服务治理 Spring Cloud封装了Netflix公司开发的Eurkeka模块来实现服务治理 在传统的rpc远程调用框架中,管理每个服务与服务之 ...
- springcloud(第三篇)springcloud eureka 服务注册与发现 *****
http://blog.csdn.net/liaokailin/article/details/51314001 ******************************************* ...
随机推荐
- iOS 11适配和iPhone X的适配
这两天对自己负责的项目进行iOS 11和iPhone X的适配,网上的博客很多,也看了很多别人的记录博客,这里把自己遇到的问题记录下,当然有些不仅仅是iOS 11和iPhone X的适配,还包括自己遇 ...
- Mac PyCharm2019激活方法
此教程支持最新2019.2版本Pycharm及其他软件 此教程实时更新,请放心使用:如果有新版本出现猪哥都会第一时间尝试激活: pycharm官网下载地址:http://www.jetbrains.c ...
- 第8课.第一个ARM裸板程序(点亮led)及申引
1.原理图 2.芯片手册 3.几条汇编代码 1.ldr:读内存 ldr R0, [R1] 假设R1的值是x,读取地址x上的数据(4字节),保存到R0中 ldr R0, =0x12345678 (4字节 ...
- Mybatis插件之Mybatis-Plus(SpringBoot)
这边只在SpringBoot下进行简单查询的测试,接下来会博客会介绍增删改的操作. 数据库表结构如下: 开始测试: 1.新建工程(trymp_springboot)并把项目结构建立好 2.导入pom. ...
- Netty学习篇③--整合springboot
经过前面的netty学习,大概了解了netty各个组件的概念和作用,开始自己瞎鼓捣netty和我们常用的项目的整合(很简单的整合) 项目准备 工具:IDEA2017 jar包导入:maven 项目框架 ...
- 《PC Assembly Language》读书笔记
本书下载地址:pcasm-book. 前言 8086处理器只支持实模式(real mode),不能满足安全.多任务等需求. Q:为什么实模式不安全.不支持多任务?为什么虚模式能解决这些问题? A: 以 ...
- 【AtCoder】diverta 2019 Programming Contest
diverta 2019 Programming Contest 因为评测机的缘故--它unrated了.. A - Consecutive Integers #include <bits/st ...
- Python 解leetcode:2. Add Two Numbers
题目描述:输入两个非空单链表,链表的每个结点的值是一个1位数整数,两个链表都是一个大整数每一位的逆序排序,求这两个链表代表的整数的和的链表值: 思路: 分别遍历两个链表,转化成相应的整数,求和后把结果 ...
- Numbers(CodeForces-128D)【思维/list】
题目链接:https://vjudge.net/problem/CodeForces-128D 题意:给出一组数,要求将这些数排列成一个环,满足每相邻两个数的差值为1,问能否完成. 思路:先取出最小的 ...
- php中连接tcp服务的三种方式
首先需要现有一个 tcp 服务,我们使用 php中的 socket 系列函数实现 <?php //创建socket套接字 $socket = socket_create(AF_INET, SOC ...