一. 启动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. [转帖]deb包转化为rpm包

    deb包转化为rpm包 https://www.cnblogs.com/noxy/p/6371399.html 改天尝试一下之前经常遇到能下载deb包 下载不到rpm包的情况. deb文件格式本是ub ...

  2. filebeat-6.4.3-windows-x86_64输出Kafka

    配置filebeat.yml文件 filebeat.prospectors: - type: log encoding: utf- enabled: true paths: - e:\log.log ...

  3. Pyhon进阶9---类的继承

    类的继承 基本概念 定义 格式如下 继承中的访问控制 class Animal: __CNOUT = 0 HEIGHT = 0 def __init__(self,age,weight,height) ...

  4. 面试题(转载csdn)

    转自https://blog.csdn.net/linzhiqiang0316/article/details/80473906 相关概念 面向对象的三个特征 封装,继承,多态,这个应该是人人皆知,有 ...

  5. sql语句基础

    数据库库(DataBase):就是一个存储数据的仓库.为了方便数据的存储和管理,它将数据按照特定的规律存储在磁盘上.通过数据库管理系统,可以有效的组织和管理存储在数据库中的数据.SQL(Structu ...

  6. log4j2日志模板

    log4j2.xml <?xml version="1.0" encoding="UTF-8"?> <!--设置log4j2的自身log级别为 ...

  7. Keras 获取中间某一层输出

    1.使用函数模型API,新建一个model,将输入和输出定义为原来的model的输入和想要的那一层的输出,然后重新进行predict. #coding=utf-8 import seaborn as ...

  8. linux网络性能测试工具ipref安装与使用

    一.iperf工具安装 源码包下载地址:https://iperf.fr/iperf-download.php#archlinux 选择对应系统的版本就是解压安装了 完成 测试发现有问题 问题原因:L ...

  9. vim打开退出命令

    打开文件方法:cd /Users/liuchang/.jenkins/secrets && vim initialAdminPassword 退出方法:先按ESC,再输入冒号,在输入命 ...

  10. \t \r \n \f

    \t 的意思是 :水平制表符.将当前位置移到下一个tab位置. \r 的意思是: 回车.将当前位置移到本行的开头. \n 的意思是:回车换行.将当前位置移到下一行的开头. \f的意思是:换页.将当前位 ...