一. 启动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. spring boot中配置日志log和热部署

    Java的日志有很多 个人强烈不推荐log4j ,推荐log4j2和logback 在高并发,多线程的环境下log4j1 的性能和log4j2相比可以用junk来形容  对就是junk.log4j2的 ...

  2. jQuery手机端点击弹出分享按钮代码

    一.HTML代码如下: <span onClick="toshare()" style="border:dotted 1px #ddd;display:block; ...

  3. Linux系统安装jdk教程

    本文仅仅适用于刚刚接触Linux系统的童鞋,毕竟本人也才刚刚玩这个东西,在此记录下以便于以后能查阅及其他童鞋能进行参考,本文为原创随笔,如需转发,请标明出处,谢谢: 此处我采用的是用VMware搭建的 ...

  4. git配置ssh秘钥(公钥以及私钥)

    桌面版git,  本文以github为例,gitlab等其它托管平台一样操作 当我们将代码托管到远程平台(GitHub.gitlab等)时, 我们需要在本地使用git进行push/pull代码时,需要 ...

  5. 【MongoDB异常】Exception authenticating MongoCredential解决方法

    我们通过ideal编辑器编辑 springboot时候,出现这个错误: com.mongodb.MongoSecurityException: Exception authenticating Mon ...

  6. Spring Mybatis多数据源配置范例

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  7. UOJ22 UR #1外星人(动态规划)

    https://www.cnblogs.com/Gloid/p/10629779.html 这一场的D. #include<bits/stdc++.h> using namespace s ...

  8. htaccess 的使用基本小节 For apache httpd

    htaccess 的使用基本小节 For apache httpd .htaccess的基本作用 .htaccess是一个纯文本文件,它里面存放着Apache服务器配置相关的指令.       .ht ...

  9. Docker 入门篇

    Docker 简介 作为一种新兴的虚拟化方式,Docker 跟传统的虚拟化方式相比具有众多的优势. 更高效的利用系统资源 更快速的启动时间 一致的运行环境 持续交付和部署 更轻松的迁移 更轻松的维护和 ...

  10. Java复习总结——String

    概览 String被声明为final,因此它不可被继承. public final class String implements java.io.Serializable, Comparable&l ...