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(下)的更多相关文章

  1. Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)

    Spring Cloud 学习 之 Spring Cloud Eureka(源码分析) Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 ...

  2. Spring Cloud 学习 之 Spring Cloud Eureka(概述)

    Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 前述: ​ 服务治理可以说是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务 ...

  3. springCloud学习6(Spring Cloud Sleuth 分布式跟踪)

    springcloud 总集:https://www.tapme.top/blog/detail/2019-02-28-11-33 前言   在第四篇和第五篇中提到一个叫关联 id的东西,用这个东西来 ...

  4. Spring Cloud 学习 之 Spring Cloud Eureka(搭建)

    Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 文章目录 搭建服务注册中心: 注册服务提供者: 高可用注册中心: 搭建服务注册中心: ...

  5. springcloud学习04- 断路器Spring Cloud Netflix Hystrix

    依赖上个博客:https://www.cnblogs.com/wang-liang-blogs/p/12072423.html 1.断路器存在的原因 引用博客 https://blog.csdn.ne ...

  6. Spring Cloud 学习 之 Spring Cloud Eureka(架构)

    Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 文章目录 Eureka服务治理基础架构的三个核心要素: 服务治理机制: 服务提供者: ...

  7. 【SpringCloud微服务实战学习系列】服务治理Spring Cloud Eureka

    Spring Cloud Eureka是Spring Cloud Netflix微服务中的一部分,它基于NetFlix Sureka做了二次封装,主要负责完成微服务架构中的服务治理功能. 一.服务治理 ...

  8. spring cloud深入学习(二)-----服务注册中心spring cloud eureka

    服务治理 主要用来实现各个微服务实例的自动化注册与发现,为啥需要这玩意呢?在一开始比如A系统调用B服务,可能通过手工维护B服务的实例,并且还得采用负载均衡等方式,这些全部都得需要手工维护,等后面系统越 ...

  9. SpringCloud微服务实战一:Spring Cloud Eureka 服务发现与注册中心(高可用实列为两个注册中心)

    微服务架构: 微服务架构的核心思想是,一个应用是由多个小的.相互独立的.微服务组成,这些服务运行在自己的进程中,开发和发布都没有依赖.不同服务通过一些轻量级交互机制来通信,例如 RPC.HTTP 等, ...

  10. SpringCloud学习心得—1.3—Eureka与REST API

      SpringCloud学习心得—1.3—Eureka与REST API Eureka的REST API接口 API的基本访问 Eureka REST APIEureka 作为注册中心,其本质是存储 ...

随机推荐

  1. Linux 源码安装Ansible 参考篇

    Ansible 源码搭建配置 近期在学习自动化运维相关技术,文章主要模拟内网情况下对Ansible的安装演示,源码安装较为繁琐.枯燥,尤其是在实际安装过程中可能出现各式各样的问题,所有在安装过程中尽量 ...

  2. 4、架构--NFS实践、搭建web服务、文件共享

    笔记 1.晨考 1.数据备份的方式有哪些 全量和增量 2.数据备份的命令有哪些,都有哪些优点缺点 cp : 本地,全量复制 scp :远程,全量复制 rsync :远程,增量复制 3.rsync的参数 ...

  3. 01 MySQL数据库安装(Windows+Mac)

    目录 MySQL数据库安装 Windows 1.主要版本简介 2.软件下载 3.文件目录简介 4.使用 4.1配置环境变量 4.2登录 制作MySQL服务端开机自启动 运行MySQL 4.3 密码修改 ...

  4. C# 字符串计算MD5

    public static string ComputeMD5 (string text) // 计算字符串的 MD5 { System.Security.Cryptography.MD5Crypto ...

  5. 图文并茂详解 NAT 协议!

    什么是 NAT 协议 我们的计算机要想访问互联网上的信息,就需要一个地址,而且这个地址是大家(其他主机)所认可的,是公共的,这个地址也叫做公有 IP 地址. 与之相对的,除了公有 IP 地址外,还有私 ...

  6. [LeetCode]1512. 好数对的数目

    给你一个整数数组 nums . 如果一组数字 (i,j) 满足 nums[i] == nums[j] 且 i < j ,就可以认为这是一组 好数对 . 返回好数对的数目. 示例 1: 输入:nu ...

  7. 简述LSM-Tree

    LSM-Tree 1. 什么是LSM-Tree LSM-Tree 即 Log Structrued Merge Tree,这是一种分层有序,硬盘友好的数据结构.核心思想是利用磁盘顺序写性能远高于随机写 ...

  8. 内网流量操控---pingtunnel建立icmp隧道

    一.pingtunnel工作原理 在上面的实验环境中,我们将分别在攻击机kali 2020和webserver上部署pingtunnel工具,在量太主机之间实现icmp隧道,再在kali2020上监听 ...

  9. 说出来你可能不信,我用excel就能做一张高端的统计报表

    统计报表是指各级企事业.行政单位按规定的表格形式.内容.时间要求报送程序,自上而下统一布置,提供统计资料的一种统计调查方式.统计报表具有来源可靠.回收率高.方式灵活等特点,是各个基层企业或事业单位填报 ...

  10. 【C#TAP 异步编程】构造函数 OOP

    原文:异步 OOP 2:构造函数 (stephencleary.com) 异步构造带来了一个有趣的问题.能够在构造函数中使用会很有用,但这意味着构造函数必须返回一个表示将来将构造的值,而不是构造的值. ...