一、概述

服务调用

1.1、nginx方式

  

1.2、注册中心

  

二、注册中心【zookeeper】

2.1、安装zookeeper3.4.11

2.2、服务提供方,需要在服务启动时吗、,把服务的信息(IP,port)注册到注册中心。

在上一节的mall-product中pom

        <dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-discovery-server</artifactId>
<version>2.12.0</version>
</dependency>

增加配置

zookeeper.address=127.0.0.1:2181

增加注册类 ServiceRegister

@Component
public class ServiceRegister implements ApplicationRunner { @Value("${zookeeper.address}")
private String zkAddress; @Override
public void run(ApplicationArguments args) throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient(zkAddress, new RetryOneTime(1000));
client.start();
client.blockUntilConnected(); ServiceInstance<Object> instance = ServiceInstance.builder().name("product").address("127.0.0.1").port(8080).build();//注册了地址和端口
ServiceDiscovery<Object> discovery = ServiceDiscoveryBuilder.builder(Object.class).client(client)
.basePath("/soa").build(); discovery.registerService(instance);
discovery.start();
}
}

可以再zookeeper客户端查看

ls /
ls /soa
ls /soa/product
get /soa/product/d5770a1f-640f-4b17-b5d0-bfb2fd4b0623

查看注册服务具体信息

{"name":"product","id":"d5770a1f-640f-4b17-b5d0-bfb2fd4b0623","address":"127.0.0.1","port":8080,"sslPort":null,"payload":null,"registrationTimeUTC":1523196340298,"serviceType":"DYNAMIC","uriSpec":null,"enabled":true}
cZxid = 0x10
ctime = Sun Apr 08 22:05:40 CST 2018
mZxid = 0x10
mtime = Sun Apr 08 22:05:40 CST 2018
pZxid = 0x10
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x1007c571a950003
dataLength = 216
numChildren = 0

2.3、调用方

pom

        <dependency>                
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-discovery</artifactId>    
<version>2.12.0</version>
</dependency>

在进行调用额时候,需要先从注册中心获取到服务地址,然后根据获取到的服务地址进行调用。

调用

    public static void main(String[] args) throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new RetryOneTime(1000));
client.start();
client.blockUntilConnected(); ServiceDiscovery<Object> discovery = ServiceDiscoveryBuilder.builder(Object.class).client(client)
.basePath("/soa").build(); Collection<ServiceInstance<Object>> instances = discovery.queryForInstances("product"); instances.forEach((item) -> {
System.out.println(item.getAddress());
System.out.println(item.getPort()); RestTemplate rt = new RestTemplate();
String string = rt.getForObject("http://" + item.getAddress() + ":" + item.getPort() + "/soa/product/1",
String.class);
System.out.println(string);
Response<Product> fromJson = new Gson().fromJson(string, new TypeToken<Response<Product>>() {
}.getType()); System.out.println(new Gson().toJson(fromJson));
});
}

如果有多个服务可以增加负载类即可

查看源码:https://github.com/bjlhx15/spring-boot.git

中的

  其中mall-product中的

  注册注册中心

  其中mall-webz中的

  client是测试代码,LoadBalance是负责均衡类

023-Spring Boot 服务的注册和发现的更多相关文章

  1. Spring Cloud服务的注册与发现

    Spring Cloud简介: Spring Cloud为开发人员提供了快速构建分布式系统中的一些通用模式(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁,领导选举,分 ...

  2. Spring Cloud服务的注册与发现(Eureka)

    一.spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运 ...

  3. Spring Cloud 服务的注册与发现(Eureka)

    Eureka服务注册中心 一.Eureka Server Eureka Server是服务的注册中心,这是分布式服务的基础,我们看看这一部分如何搭建. 首先,Spring Cloud是基于Spring ...

  4. Spring Cloud-Eureka实现服务的注册与发现(二)

    在Spring Cloud中是使用Eureka来实现服务的注册与发现的 请勿使用eureka2.x  用于生产 2.x已经停止开发了  使用1.x  最新版是1.9  我这里demo是使用1.9  详 ...

  5. .net core + eureka + spring boot 服务注册与调用

    .net core + eureka + spring boot 服务注册与简单的调用 假期小长假遇上疫情只能去家里蹲了,刚好有时间总结一下. 概述 微服务架构是当前比较火的分布式架构,本篇基于.ne ...

  6. C5. Spring 服务的注册与发现(Spring Cloud Eureka)

    [概述] Eureka 作为 Spring Cloud 分布式解决方案中重要的一环,实现了服务的注册与发现等功能.Eureka 包括 Eureka Server 和 Eureka Client,具体的 ...

  7. 玩转SpringCloud(F版本) 一.服务的注册与发现(Eureka)

    一.服务的注册与发现(Eureka) spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等 ...

  8. SpringCloud-微服务的注册与发现Eureka(二)

    一.SpringCloud简介 Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均 ...

  9. (转) 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)

    一.spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运 ...

随机推荐

  1. C#之正则表达式验证

    /** 匹配身份证号 规则: 15位纯数字或者18位纯数字或者17位数字加一位x */ var regex = new System.Text.RegularExpressions.Regex(@&q ...

  2. 用kaptcha生成验证码

    1.新建web项目,导入jar包:kaptcha-2.3.jar 2.配置web.xml代码如下: <?xml version="1.0" encoding="UT ...

  3. hdu5800 To My Girlfriend dp 需要比较扎实的dp基础。

    To My Girlfriend Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  4. vSphere ESXi主机配置iSCSI存储

    vSphere ESXi主机配置iSCSI存储 vSphere ESXi主机一般连接的存储类型有光纤存储.iSCSI存储两类.本次案例为iSCSI存储连接ESXi主机的配置. 案例环境:ESXi主机通 ...

  5. Who's in the Middle - poj 2388 (快速排序寻找中位数)

    题意; 寻找中位数 利用快速排序来寻找中位数. #include <iostream> using namespace std; int N; ]; int Median(int left ...

  6. Easyui 基于kindeditor的扩展

    源码 /** * Author : ____′↘夏悸 * Easyui KindEditor的简单扩展. * 有了这个之后,你就可以像使用Easyui组件的方式使用KindEditor了 * 前提是你 ...

  7. Spring MVC参数方法名称解析器

    以下示例显示如何使用Spring Web MVC框架来实现多动作控制器的参数方法名称解析器. MultiActionController类可在单个控制器中分别映射多个URL到对应的方法. 所下所示配置 ...

  8. 创建一个动作-Action类:

    让我们创建一个Java文件HelloWorldAction.java的Java资源> SRC下一个的包名com.yiibai.struts2与下面的内容. package com.yiibai. ...

  9. FreeMarker / S2SH 各种报错解决方案

    1. org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of ...

  10. C#安装,启动,停止,卸载Windows服务

    public class OptionServices    {        //安装服务        public static void InstallService(string filep ...