springcloud(三):服务提供与调用
上一篇文章我们介绍了eureka服务注册中心的搭建,这篇文章介绍一下如何使用eureka服务注册中心,搭建一个简单的服务端注册服务,客户端去调用服务使用的案例。愿意了解源码的朋友直接求求交流分享技术:二一四七七七五六三三
案例中有三个角色:服务注册中心、服务提供者、服务消费者,其中服务注册中心就是我们上一篇的eureka单机版启动既可,流程是首先启动注册中心,服务提供者生产服务并注册到服务中心中,消费者从服务中心中获取服务并执行。
服务提供
我们假设服务提供者有一个hello方法,可以根据传入的参数,提供输出“hello ,this is first messge”的服务
1、pom包配置
创建一个springboot项目,pom.xml中添加如下配置:
<dependencies> <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>
2、配置文件
application.properties配置如下:
server.port=9000 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka
3、启动类
启动类中添加@EnableDiscoveryClient注解
@SpringBootApplication @EnableDiscoveryClient public class ProducerApplication { public static void main(String[] args) { SpringApplication.run(ProducerApplication.class, args); } }
4、controller
提供hello服务
@RestController public class HelloController { @RequestMapping("/hello") public String index(@RequestParam String name) { return "hello "+name+",this is first messge"; } }
添加@EnableDiscoveryClient注解后,项目就具有了服务注册的功能。启动工程后,就可以在注册中心的页面看到SPRING-CLOUD-PRODUCER服务。
到此服务提供者配置就完成了。
服务调用
1、pom包配置
和服务提供者一致
<dependencies> <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>
2、配置文件
application.properties配置如下:
spring.application.name=spring-cloud-consumer server.port=9001 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka
3、启动类
启动类添加@EnableDiscoveryClient和@EnableFeignClients注解。
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
@EnableDiscoveryClient :启用服务注册与发现
@EnableFeignClients:启用feign进行远程调用
Feign是一个声明式Web Service客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。
4、feign调用实现
@FeignClient(name= "spring-cloud-producer") public interface HelloRemote { @RequestMapping(value = "/hello") public String hello(@RequestParam(value = "name") String name); }
name:远程服务名,及spring.application.name配置的名称
此类中的方法和远程服务中contoller中的方法名和参数需保持一致。
5、web层调用远程服务
将HelloRemote注入到controller层,像普通方法一样去调用即可。
@RestController public class ConsumerController { @Autowired HelloRemote HelloRemote; @RequestMapping("/hello/{name}") public String index(@PathVariable("name") String name) { return HelloRemote.hello(name); } }
到此,最简单的一个服务注册与调用的例子就完成了。
整体代码结构如下:
资料和源码来源
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
修改默认Shell环境
公司弄了台深度学习的服务器,前段时间给另一部门使用后,他们默认使用了 zsh 作为 默认 shell 环境,其中使用的 python 等环境基本是比较老版本,以及没有安装 tf 环境,果断的准备还原为我所习惯的 bash 环境。 先介绍下 zsh 安装过程吧~! # 安装 zsh $ sudo apt-get update && sudo apt-get -y zsh # 可选安装oh-my-zsh $ git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh $ cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc # 修改为默认 shell 环境 $ chsh -s /bin/zsh 因此还原就很容易咯~ $ chsh -s /bin/bash 经过上述操作之后,需要重新登录即可。
- 下一篇
Spring Boot 系统要求
Spring Boot 2.1.0.RELEASE 方需要Java 8 or 9的支持和Spring Framework 5.1.2.RELEASE以上的版本。 明确的构建工具的支持,请参考下面的构建工具: 构建工具 版本 Maven 3.3+ Gradle 4.4+ Servlet 容器 Spring Boot 支持下面的嵌入 servlet 容器: 名字 Servlet 版本 Tomcat 9.0 4.0 Jetty 9.4 3.1 Undertow 2.0 4.0 你也可以将 Spring Boot 部署到任何支持 Servlet 3.1+ 的容器中。 https://www.cwiki.us/display/SpringBootZH/System+Requirements
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- CentOS8安装Docker,最新的服务器搭配容器使用
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- 设置Eclipse缩进为4个空格,增强代码规范
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS8编译安装MySQL8.0.19
- SpringBoot2整合Redis,开启缓存,提高访问速度
- SpringBoot2配置默认Tomcat设置,开启更多高级功能