Spring Cloud 之 服务注册与发现实战
一. 启动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 之 服务注册与发现实战的更多相关文章
- Spring cloud实现服务注册及发现
服务注册与发现对于微服务系统来说非常重要.有了服务发现与注册,你就不需要整天改服务调用的配置文件了,你只需要使用服务的标识符,就可以访问到服务. 本文属于<7天学会spring cloud系列& ...
- Spring Cloud 之 服务注册与发现
作为微服务框架,提供服务注册发现是最基本的功能.Spring Cloud 针对服务注册发现 提供了 Eureka版本的实现 .Zookeeper版本的实现.Consul版本的实现.由于历史原因 Eur ...
- 用ZooKeeper做为注册中心搭建基于Spring Cloud实现服务注册与发现
前提: 先安装好ZooKeeper的环境,搭建参考:http://www.cnblogs.com/EasonJim/p/7482961.html 说明: 可以再简单的理解为有两方协作,一个是服务提供这 ...
- SpringBoot + Spring Cloud Consul 服务注册和发现
什么是Consul Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其它分布式服务注册与发现的方案,Consul 的方案更"一站式" ...
- SpringBoot + Spring Cloud Eureka 服务注册与发现
什么是Spring Cloud Eureka Eureka是Netflix公司开发的开源服务注册发现组件,服务发现可以说是微服务开发的核心功能了,微服务部署后一定要有服务注册和发现的能力,Eureka ...
- 【Spring Cloud】服务注册与发现组件——Eureka(二)
一.Eureka原理 1.架构图 首先来看eureka的官方结构图 所有应用作为Eureka Client和Eureka Server交互,服务提供者启动时向Eureka Server注册自己的IP. ...
- 如何优化Spring Cloud微服务注册中心架构?
作者: 石杉的架构笔记 1.再回顾:什么是服务注册中心? 先回顾一下什么叫做服务注册中心? 顾名思义,假设你有一个分布式系统,里面包含了多个服务,部署在不同的机器上,然后这些不同机器上的服务之间要互相 ...
- 玩转Spring Cloud之服务注册发现(eureka)及负载均衡消费(ribbon、feign)
如果说用Spring Boot+Spring MVC是开发单体应用(或单体服务)的利器,那么Spring Boot+Spring MVC+Spring Cloud将是开发分布式应用(快速构建微服务)的 ...
- spring cloud Eureka 服务注册发现与调用
记录一下用spring cloud Eureka搭建服务注册与发现框架的过程. 为了创建spring项目方便,使用了STS. 一.Eureka注册中心 1.新建项目-Spring Starter Pr ...
随机推荐
- MySQL数据库执行计划(简单版)
+++++++++++++++++++++++++++++++++++++++++++标题:MySQL数据库执行计划简单版时间:2019年2月25日内容:MySQL数据库执行计划简单版重点:MySQL ...
- 文件实时同步(rsync+inotify)
目标服务器:10.11.6.11 源服务器:10.11.6.12 准备条件: 1.关闭selinux: vi /etc/selinux/config #编辑防火墙配置文件 #SELINUX=enfor ...
- AOP - 2 实例(SpringBoot 注解方式)
1.创建Spring Boot项目 创建一个Spring Boot 项目,然后pom中引入web 模块与AOP相关依赖. <dependency> <groupId>org.s ...
- 浅谈kafka streams
随着数据时代的到来,数据的实时计算也越来越被大家重视.实时计算的一个重要方向就是实时流计算,目前关于流计算的有很多成熟的技术实现方案,比如Storm.Spark Streaming.flink等.我今 ...
- 转载泡泡机器人——IMU预积分总结与公式推导1
IMU预积分技术最早由T Lupton于12年提出[1],C Forster于15年[2][3][4]将其进一步拓展到李代数上,形成了一套优雅的理论体系.Forster将IMU预积分在开源因子图优化库 ...
- java篇 之 操作符
操作符:1.赋值操作符 用(+= ,^=...不会改变类型,如果用 = 会进行隐式转换类型) short x = 0; int i = 123456; x += i;//编译通过 x= x + i;/ ...
- python的局部变量,全局变量,类变量,实例变量
定义: a.全局变量:在模块内.在所有函数外面.在class外面,这就是全局变量. b.局部变量:在函数内.在class的方法内(未加self修饰的),这就是局部变量. c. 静态变量:在class内 ...
- Git 操作命令
一.Git 基本配置 1.配置 命令:git config --global prop_name prop_value 如配置git用户名与邮箱: git config --global user. ...
- docker-lnmp dockerfile
code: FROM php:7.1.26-fpm WORKDIR /usr/share/nginx/html # bcmath pdo_mysql intl gd zip opcache xdebu ...
- ADC采样工作原理详解
如何利用单片机的ADC模块(或者独立的ADC芯片)得到接入ADC管脚上的实际电压值?这个问题,是第一次接触ADC时候,大家都会遇到的问题.会读到什么值单片机会读到什么值?需要看一个特性,就是几位的AD ...