springcloud学习03-spring cloud eureka(下)
7.配置服务提供者(生产者)
7.1.配置resources/application.yml.
值eureka.client.service-url(或serviceUrl).defaultZone是配置将服务注册到那个注册中心,server.port是自己服务占用的端口,spring.application.name是服务在注册中心的名字,需要是唯一的,见7.2下图:
- eureka:
- client:
- serviceUrl:
- defaultZone: http://localhost:8888/eureka/
- server:
- port: 8889
- spring:
- application:
- name: server-provider
7.2.在启动入口类上加入注解@EnableEurekaClient,然后启动,刷新localhost:8888,可以看到这个服务。
7.3.用postman测试这个服务,在启动类上加注解@RestController(sprig中注解constroller与responsebody的合体),测个返回hello world!的接口(一个服务)。EurekaProviderApplication.java文件
- package com.springcloud.eurekaprovider;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
- @SpringBootApplication
- @EnableEurekaClient
- @RestController
- public class EurekaProviderApplication {
- public static void main(String[] args) {
- SpringApplication.run(EurekaProviderApplication.class, args);
- }
- @RequestMapping("/hello")
- public String home() {
- return "hello world! ";
- }
- }
postman选择get/post请求,然后输入localhost:8889/hello发送请求能看到hello world!就说明服务时可用的
8.陪服务的消费者
8.1.打开pom文件,添加几个新的依赖,我看到有说spring-cloud-starter-eureka这个包被官方放弃了,和spring-cloud-starter-netflix-eureka-server推荐用作个包,我这里都加进来了,参考的文章是这样的,等下成功了,我去掉试一下。(参考文章:https://blog.csdn.net/zhou199252/article/details/80745151)
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka</artifactId>
- <version>1.4.4.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-ribbon</artifactId>
- <version>1.4.2.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
8.2.配置resources/application.yml.各个含义与配置生产者哪里一个意思。
- eureka:
- client:
- serviceUrl:
- defaultZone: http://localhost:8888/eureka/
- server:
- port: 8887
- spring:
- application:
- name: server-customer
8.3.启动入口类中加入注解:@EnableDiscoveryClient.启动服务,然后刷新eureka注册中心可以到这个服务。(这个和我想的不一样呀服务消费者不应该在这一呀)
8.4.用postman进行测试,在启动类中加入restTemplate以消费相关的服务。
启动类中加入restTemplate,注解@LoadBalanced是开启负载均衡,具体解释https://blog.csdn.net/u011063112/article/details/81295376
这个restTemplate可以在service时在创建(new),测试时可以这样,但是在工程中可能到处在用,所以启动时就加入容器中,通过依赖注入的方式调用。
- package com.springcloud.eurekacustomer;
- 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 EurekaCustomerApplication {
- public static void main(String[] args) {
- SpringApplication.run(EurekaCustomerApplication.class, args);
- }
- @Bean
- @LoadBalanced
- RestTemplate restTemplate(){
- return new RestTemplate();
- }
- }
简单测试,增加HelloController.java和HelloService.java文件。
目录结构
- package com.springcloud.eurekacustomer;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.web.client.RestTemplate;
- @Service
- public class HelloService {
- @Autowired
- RestTemplate restTemplate;
- public String helloService(){
- String rltStr = restTemplate.getForObject("http://server-provider/hello",String.class);
- rltStr = rltStr + " form service-provider by service-customer.";
- return rltStr;
- }
- }
- package com.springcloud.eurekacustomer;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- public class HelloController {
- @Autowired
- HelloService helloService;
- @RequestMapping("/cushello")
- public String hello(){
- return helloService.helloService();
- }
- }
用postman测试的结果如下,
8.5.关于消费者是否用在注册中心中注册吗?
查了一下没有找到相关的解释,先放着吧。
8.6.总结。
通过这个调用过程可以看出来,他与dubbo有一个不同时,customer的调用并没有依赖provider相关的代码即jar,这样开发中就不需要频繁的更新jar包了。
springcloud学习03-spring cloud eureka(下)的更多相关文章
- Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)
Spring Cloud 学习 之 Spring Cloud Eureka(源码分析) Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 ...
- Spring Cloud 学习 之 Spring Cloud Eureka(概述)
Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 前述: 服务治理可以说是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务 ...
- springCloud学习6(Spring Cloud Sleuth 分布式跟踪)
springcloud 总集:https://www.tapme.top/blog/detail/2019-02-28-11-33 前言 在第四篇和第五篇中提到一个叫关联 id的东西,用这个东西来 ...
- Spring Cloud 学习 之 Spring Cloud Eureka(搭建)
Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 文章目录 搭建服务注册中心: 注册服务提供者: 高可用注册中心: 搭建服务注册中心: ...
- springcloud学习04- 断路器Spring Cloud Netflix Hystrix
依赖上个博客:https://www.cnblogs.com/wang-liang-blogs/p/12072423.html 1.断路器存在的原因 引用博客 https://blog.csdn.ne ...
- Spring Cloud 学习 之 Spring Cloud Eureka(架构)
Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 文章目录 Eureka服务治理基础架构的三个核心要素: 服务治理机制: 服务提供者: ...
- 【SpringCloud微服务实战学习系列】服务治理Spring Cloud Eureka
Spring Cloud Eureka是Spring Cloud Netflix微服务中的一部分,它基于NetFlix Sureka做了二次封装,主要负责完成微服务架构中的服务治理功能. 一.服务治理 ...
- spring cloud深入学习(二)-----服务注册中心spring cloud eureka
服务治理 主要用来实现各个微服务实例的自动化注册与发现,为啥需要这玩意呢?在一开始比如A系统调用B服务,可能通过手工维护B服务的实例,并且还得采用负载均衡等方式,这些全部都得需要手工维护,等后面系统越 ...
- SpringCloud微服务实战一:Spring Cloud Eureka 服务发现与注册中心(高可用实列为两个注册中心)
微服务架构: 微服务架构的核心思想是,一个应用是由多个小的.相互独立的.微服务组成,这些服务运行在自己的进程中,开发和发布都没有依赖.不同服务通过一些轻量级交互机制来通信,例如 RPC.HTTP 等, ...
- SpringCloud学习心得—1.3—Eureka与REST API
SpringCloud学习心得—1.3—Eureka与REST API Eureka的REST API接口 API的基本访问 Eureka REST APIEureka 作为注册中心,其本质是存储 ...
随机推荐
- maven下使用jstl标签(1.2)版本
使用的是1.2版本的,只需要一个jstl-1.2.jar jsp中头部加入<%@ taglib prefix="c" uri="http://java.sun ...
- C++ 序列操作函数最全总结
标准库定义了许多用于操作序列的算法,大多在algorithm和numeric文件中,大多数函数的原理并不复杂,但是在很多情况下可以替代手写的情况,甚至更加优秀. 这类算法函数非常多,但是他们都有共同的 ...
- 华为模拟器ensp AR启动失败 代码40 解决方案
前几天更换了一台电脑,安装好Windows10 ,安装ensp 模拟器,安装好ensp后,发现AR都启动不了.卸载重新安装还是不行,此时度娘了一下,发现都说是协助不彻底,没有清理干净安装插件.我做了最 ...
- [旧][Android] View 工作原理(二)
备注 原发表于2016.05.27,资料已过时,仅作备份,谨慎参考 前言 本文大量参照<Android 开发艺术探索>及参考资料的内容整合,主要帮助自己理清 View 的工作原理.深入学习 ...
- iOS 通知扩展插件
iOS 通知扩展插件 目录 iOS 通知扩展插件 Notification Service Extension 新建一个target 代码实现 注意事项 UINotificationConentExt ...
- 数据分析需要学什么?BI工具有速成?
我们都知道,成为一个数据分析师的必经之路,必须要会使用SQL和R语言.假如你想学会数据分析的话,先别着急着学编程技术,先学好excel,把excel真正学会了,操作熟练了,会做常用函数公式,数据透视 ...
- C# new操作符的作用
CLR要求所有对象(实例)都用new操作符创建,那么new操作符做了哪些事呢?1. 计算字节数 计算类型及其所有基类型(父类)中定义的所有实例字段需要的字节数.堆上每个对象都需要一些额外的成员, ...
- Oracle PSU 简介
转至:http://blog.itpub.net/30327022/viewspace-2642815/ Oracle RAC创建完毕后,我们通常需要打上最新的PSU,因为里面是包含GI和DB的补丁集 ...
- AcWing 325. 计算机
传送门 题目大意: 一棵无根树,每条边有一个距离,求每个顶点到距离其最远的顶点的距离. 思路: 考虑树形DP+换根. 令D[x]x到以x为根的子树当中的最长距离,d[x]为次长距离,U[x]为x向上走 ...
- 大学英语四级之听力Key Words
Key Words关键词 所谓关键词,指的是短文中能够标志着正确答案的词,也就是说,关键词后面往往是正确答案 听力中的关键词可为宏观和微观两方面,一般出现在如下10种位置. 一.宏观方向(主旨大意) ...