一、概述

服务调用

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. MVC 的八个扩展点

    Asp.net MVC中常用的八个扩展点并举例说明. 一.ActionResult ActionResult代表了每个Action的返回结果.asp.net mvc提供了众多内置的ActionResu ...

  2. CentOS7中关闭selinux

    在安装Cobbler和Puppet时需要关闭selinux,但是通常情况下载安装完CentOS7后,默认情况下SElinux是启用状态, 如下所示: [csharp] view plaincopy   ...

  3. springboot+thymeleaf打war包在外部tomcat运行

    工程目录 pom文件注意点 <packaging>war</packaging> <dependency> <groupId>org.springfra ...

  4. atitit.taskService 任务管理器的设计 v1

    atitit.taskService 任务管理器的设计 v1 任务管理器的点 Sametime_exe_count Per task sleepMillSec Timeout_secs 作者:: 绰号 ...

  5. vivado与modelsim的联合仿真(一)

    vivado软件中也自带仿真工具,但用了几天之后感觉仿真速度有点慢,至少比modelsim慢挺多的.而modelsim是我比较熟悉的一款仿真软件,固然选它作为设计功能的验证.为了将vivado和mod ...

  6. centos7下安装mysql5.7和jdk 1.8

    安装mysql5.7 具体安装过程可参见官网:A Quick Guide to Using the MySQL Yum Repository 进入/usr/local/src文件夹. cd /usr/ ...

  7. JS常用的方法总结

    /** * 将参数格式化为对象 * @param urlParams type string * @example * let urlParams = 'name="xiaoliu" ...

  8. js array filter pop push shift unshift方法

    JavaScript Array filter() 方法  JavaScript Array 对象 实例 返回数组 ages 中所有元素都大于 18 的元素: var ages = [32, 33,  ...

  9. Unity3D学习笔记——Rigdbody刚体组件

    Rigdbody刚体组件:必须和碰撞体(Colliders)一起使用,否则会发生穿过的现象.碰撞体(Colliders)不是必须和刚体一起使用. 刚体的作用:使游戏物体能获得重力,接受外界的受力和扭力 ...

  10. 转载:DenseNet算法详解

    原文连接:http://blog.csdn.net/u014380165/article/details/75142664 参考连接:http://blog.csdn.net/u012938704/a ...