一. 启动Eureka Server集群

准备二台云主机,二个eureka server服务互相进行复制。准备二个application.yml配置,分别如下:

application-server1.yml

spring:
 application:
name: eurekaServer1
server:
port: 8761 eureka:
instance:
hostname: eurekaServer1
appname: eurekaServer1
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://eurekaServer2:${server.port}/eureka/

application-server2.yml

spring:
 application:
  name: eurekaServer2
server:
port: 8761 eureka:
instance:
hostname: eurekaServer2
appname: eurekaServer2
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://eurekaServer1:${server.port}/eureka/

 启动时利用springboot 的 profile机制分别进行启动:java -jar  eureka-server.jar --spring.profiles.active={profile名}  方式让不同的 application-{profile名}.yml文件生效

二. 启动服务Provider (此处以发送手机验证码的服务为例)

简单提供一个Controller

@RestController
@RequestMapping(value = "/ecshop/api/1.0")
public class SmsController {
@Autowired
private SmsUtil smsUtil; @RequestMapping(value = "sendMobileCode", method = RequestMethod.GET)
public Result sendMobileCode(String mobile, String bizType) {
smsUtil.sentMobileCode(mobile, bizType);
Result result = new Result(true, "0", "手机验证码发送成功", false);
return result;
} }

需要pom中添加

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

配置文件 application.yml

spring:
application:
name: commonservices //此处必须要配置,否则会导致ribbon找不到服务
http:
encoding:
force: true
charset: UTF-8
enabled: true
server:
tomcat:
uri-encoding: UTF-8
port: 1001
eureka:
instance:
appname: commonservices
client:
serviceUrl:
defaultZone: http://eurekaServer1:8761/eureka/,http://eurekaServer2:8761/eureka/

启动类

@SpringBootApplication
@EnableDiscoveryClient
public class CommonsApplication
{
public static void main( String[] args )
{
SpringApplication.run(CommonsApplication.class, args) ;
}
}

三. 设置服务调用接口

@FeignClient("commonservices")    //注解中应填写相应服务的spring.application.name对应的值
public interface SmsApi { @RequestMapping(value = "/ecshop/api/1.0/sendMobileCode", method = RequestMethod.GET) //访问路径要和真实服务的路径一致
public Result sendMobileCode(@RequestParam("mobile") String mobile, @RequestParam("bizType") String bizType) ; //参数前一定要有@RequestParam注解 }

注:1. 返回类型 Result 一定要有无参的构造函数,否则 Feign 会 无法 根据 传递 过来 的 JSON 字符串 转换 为 User 对象, 从而 抛出 异常, 造成 调用 不成功。

2. 如果请求参数是个对象,则使用@RequestBody注解,例:User register(@RequestBody User user);

四. 启动服务Consumer (假设现在有一个注册服务需要调用短信服务)

RegistService代码

@Service
public class RegistService { @Autowired
private SmsApi smsApi ; //注入服务接口,实际注入的是OpenFeign框架通过字节码生成的包装类对象 public void sendSmsCode(String mobile) { try {
Result result = smsApi.sendMobileCode(mobile, bizType) ; //调用服务
} catch (AttemptLimitException e) {
throw new UserCenterException(errorObj);
} } }

RegistController代码

@RestController
@RequestMapping(value = "/ecshop/api/1.0/registry")
public class RegistController { @Autowired
private RegistService registService; // 发送验证码
@RequestMapping(value = "/sendSmsCode", method = RequestMethod.GET)
public Result sendSmsCode(String mobile) { //核对手机格式是否正确
RegistUtils.checkMobileFormat(mobile); registService.sendSmsCode(mobile); return new Result(true, "R004", "发送验证码成功~", false); }
}

启动类

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
} }

配置文件

spring:
application:
name: registerservices
server:
tomcat:
uri-encoding: UTF-8
port: 1001
eureka:
instance:
appname: registerservices
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://eurekaServer1:8761/eureka/,http://eurekaServer2:8761/eureka/ //此处也可以只注册一个注册中心

五. 测试

   1. 通过浏览器访问 http://xxx/ecshop/api/1.0/registry

2.通过单元测试方式

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class RegistServiceTest { @Autowired
private RegistService registService ; @Test
public void sendSmsCode() {
String mobile = "xxxx7222222" ;
registService.sendSmsCode(mobile);
}

Spring Cloud 之 服务注册与发现实战的更多相关文章

  1. Spring cloud实现服务注册及发现

    服务注册与发现对于微服务系统来说非常重要.有了服务发现与注册,你就不需要整天改服务调用的配置文件了,你只需要使用服务的标识符,就可以访问到服务. 本文属于<7天学会spring cloud系列& ...

  2. Spring Cloud 之 服务注册与发现

    作为微服务框架,提供服务注册发现是最基本的功能.Spring Cloud 针对服务注册发现 提供了 Eureka版本的实现 .Zookeeper版本的实现.Consul版本的实现.由于历史原因 Eur ...

  3. 用ZooKeeper做为注册中心搭建基于Spring Cloud实现服务注册与发现

    前提: 先安装好ZooKeeper的环境,搭建参考:http://www.cnblogs.com/EasonJim/p/7482961.html 说明: 可以再简单的理解为有两方协作,一个是服务提供这 ...

  4. SpringBoot + Spring Cloud Consul 服务注册和发现

    什么是Consul Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其它分布式服务注册与发现的方案,Consul 的方案更"一站式" ...

  5. SpringBoot + Spring Cloud Eureka 服务注册与发现

    什么是Spring Cloud Eureka Eureka是Netflix公司开发的开源服务注册发现组件,服务发现可以说是微服务开发的核心功能了,微服务部署后一定要有服务注册和发现的能力,Eureka ...

  6. 【Spring Cloud】服务注册与发现组件——Eureka(二)

    一.Eureka原理 1.架构图 首先来看eureka的官方结构图 所有应用作为Eureka Client和Eureka Server交互,服务提供者启动时向Eureka Server注册自己的IP. ...

  7. 如何优化Spring Cloud微服务注册中心架构?

    作者: 石杉的架构笔记 1.再回顾:什么是服务注册中心? 先回顾一下什么叫做服务注册中心? 顾名思义,假设你有一个分布式系统,里面包含了多个服务,部署在不同的机器上,然后这些不同机器上的服务之间要互相 ...

  8. 玩转Spring Cloud之服务注册发现(eureka)及负载均衡消费(ribbon、feign)

    如果说用Spring Boot+Spring MVC是开发单体应用(或单体服务)的利器,那么Spring Boot+Spring MVC+Spring Cloud将是开发分布式应用(快速构建微服务)的 ...

  9. spring cloud Eureka 服务注册发现与调用

    记录一下用spring cloud Eureka搭建服务注册与发现框架的过程. 为了创建spring项目方便,使用了STS. 一.Eureka注册中心 1.新建项目-Spring Starter Pr ...

随机推荐

  1. 初识Haskell 四:函数function之二 常见函数

    对Discrete Mathematics Using a Computer的第一章Introduction to Haskell进行总结.环境Windows,关于函数的部分太长了,分开写. 常用的对 ...

  2. 菜鸟学IT之python3关于列表,元组,字典,集合浅认识!

    作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2753 一.列表,元组,字典,集合分别如何增删改查及遍历. 列表 # 列表的 ...

  3. 二 Array 数组常用操作方法

    数组链接 Array 构造上的方法 一.Array.from() Array.from方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable) ...

  4. jstl的foreach标签

    jsp支持丰富的jstl标签语言(需要jar包支持),其中list循环(迭代)用的是<c:forEach></c:forEach>标签. 这个标签的作用就是迭代输出标签内部的内 ...

  5. 小程序组件中有bindinput监听报异常

    真机上有问题,ide上是没问题的,   组件有处理函数,结果异常说页面没有处理函数,加上处理函数后就不报异常了.

  6. spring boot Websocket(使用笔记)

    ​ 使用websocket有两种方式:1是使用sockjs,2是使用h5的标准.使用Html5标准自然更方便简单,所以记录的是配合h5的使用方法. 1.pom ​ 核心是@ServerEndpoint ...

  7. const命令

    一.基本用法 声明一个只读的常量,这个值不会变. const声明常量与let一样,不可重复声明. 二.本质(我困惑的地方) const实际上保证的并不是变量的值不可以改动,而是变量指向的内存地址不可改 ...

  8. Elasticsearch 目录总结

    一:Elasticsearch (及工具插件)安装相关: 二:Elasticsearch 数据新增相关: 三:Elasticsearch 数据删除相关: 四:Elasticsearch 数据更新相关: ...

  9. 使用apidoc生成项目文档

    [1]npm install apidoc -g 全局安装apidoc [2]apidoc -v 查看是否安装成功 [3]apidoc.json apidoc的项目级配置文件,它必须位于整个工程目录顶 ...

  10. abp添加动态菜单

    abp中MenuDefinition封装了导航栏上的主菜单的属性,MenuItemDefinition则封装了子菜单的属性,子菜单可以引用其他子菜单构成一个菜单树. MenuDefinitio成员如下 ...