Spring Cloud 微服务一:Consul注册中心
- Consul介绍
- Consul is a service mesh solution providing a full featured control plane with service discovery, configuration, and segmentation functionality. Each of these features can be used individually as needed, or they can be used together to build a full service mesh. Consul requires a data plane and supports both a proxy and native integration model. Consul ships with a simple built-in proxy so that everything works out of the box, but also supports 3rd party proxy integrations such as Envoy. 以上是官网介绍,大概意思是:Consul是一个service mesh 解决方案,包含服务发现、配置、外部服务;这些功能可以按需单独使用。
- Consul核心能力包括:服务发现、健康检查、KV存储、安全通信、多数据中心。本章主要使用服务发现功能。Consul官网:https://www.consul.io/
- docker安装:安装方法参考:https://github.com/JensenShao/consul 。以单机模式安装Consul,第一步,获取镜像:docker pull wdijkerman/consul;第二步,docker宿主机上创建文件
{
"data_dir": "/consul/data",
"log_level": "INFO",
"client_addr": "0.0.0.0",
"ports": {
"dns": 53
},
"ui": true,
"server": true,
"bootstrap_expect": 1,
"disable_update_check": true
};第三步,启动镜像:docker run -u root -p 8400:8400 -p 8500:8500 -p 8600:53/udp -h server1 -v /root/shaozj/consul/config/my_config.json:/consul/config/my_config.json:ro --privileged=true wdijkerman/consul;更多参数配置可参考上面链接地址。 - 原生安装方式参考:https://www.consul.io/docs/install/index.html
- 环境准备:本机安装Maven,JDK1.8,Intellij Idea环境;spring环境:spring boot使用2.0.8.RELEASE版本,spring cloud使用Finchley.SR2, 具体版本间的兼容关系请参考spring官网:http://spring.io/projects/spring-cloud,里面有明确说明
- 创建工程骨架:创建父工程parent,创建三个module:user-api,user-service,user-consumer;其中user-api中定义了服务接口,user-service和user-consumer都会依赖这个接口;user-service是对于user-api中接口的实现;user-consumer主要通过feign来调用服务
- 编写user-api代码:首先,由于api工程中会用到web的一些注解,需要pom中依赖spring-boot-starter-web。创建实体类User.java, 属性有id,name,创建接口UserService:
public interface UserService{
@GetMapping("/all")
List<User> getAllUsers();
}这样一个接口就完成了
- 接下来编写user-service,主要是对user-api接口的实现,是服务的提供方。首先,pom中需要依赖user-api,另外需要依赖spring cloud consul服务发现组件:spring-cloud-starter-consul-discovery。创建UserService实现类UserServiceImpl:
@RestController
public class UserServiceImpl implements UserService{
@Override
public List<User> getAllUsers(){
// 模拟DAO
List<User> userList = new ArrayList<>();
userList.add(new User(1, "consul"));
userList.add(new User(1, "feign")); return userList;
}
}此处还需要添加一个springboot的启动类UserServiceApplication用于启动,
@SpringBootApplication
@EnableDiscoveryClient
public class UserServiceApplication{
public static void main(String[] args){
SpringApplication.run(UserServiceApplication.class, args);
}
}其中@EnableDiscoveryClient注解作用是让注册中心能够发现并扫描该服务。最后添加spring配置application.yml:
spring:
application:
name: user-service
cloud:
consul:
host: 127.0.0.1
port: 8500
discovery:
register: true
hostname: 127.0.0.1 server:
port: 10086其中register表示该工程下的服务会注册到注册中心
- 接下来编写user-service,主要是对user-api接口的实现,是服务的提供方。首先,pom中需要依赖user-api,另外需要依赖spring cloud consul服务发现组件:spring-cloud-starter-consul-discovery。创建UserService实现类UserServiceImpl:
- 实现user-consumer,该工程主要是服务的调用方。同样先在pom中添加依赖包:user-api, 同时需要依赖spring cloud feign:spring-cloud-starter-openfeign, 还需要依赖:spring-cloud-starter-consul-discovery作为http客户端。首先定义一个接口UserServiceClient继承UserService(此处为feign的使用方式):
//其中@FeignClient注解表示要调用的微服务
@FeignClient(value="user-service")
public UserServiceClient extends UserService{}创建UserController调用服务:
@RestController
public class UserController{
//自动注入feign客户端
@Autowired
private UserServiceClient userServiceClient; @GetMapping("/users")
public List<User> getAllUsers(){
//调用微服务
return userServiceClient.getAllUsers();
}
}创建springboot启动类UserConsumerApplication:
//@EnableFeignClients表示启用feign客户端,通过此注解可以使用feign
@EnableFeignClients
@SpringBootApplication
public class UserConsumerApplication{
public static void main(String[] args){
SpringApplication.run(UserConsumerApplication.class, args);
}
}创建spring配置文件application.yml:
spring:
application:
name: user-consumer
cloud:
consul:
host: 127.0.0.1
port: 8500
discovery:
# false表示该工程下的服务不会被注册
register: false server:
port: 10080 - 分别启动user-service,user-consumer应用,在浏览器访问:http://localhost:10080/users,能够返回用户列表即表示成功
2. Consul安装有两种方式:原生安装,docker安装,本例主要采用docker方式
3.spring 集成,springcloud对Consul的基本操作进行了封装,我们可以方便地使用consul的功能,本文中主要是使用consul的服务发现功能,客户端使用Springcloud feign做http客户端以及负载均衡,以用户管理为例描述服务发现功能。
Spring Cloud 微服务一:Consul注册中心的更多相关文章
- SpringCloud微服务实战一:Spring Cloud Eureka 服务发现与注册中心(高可用实列为两个注册中心)
微服务架构: 微服务架构的核心思想是,一个应用是由多个小的.相互独立的.微服务组成,这些服务运行在自己的进程中,开发和发布都没有依赖.不同服务通过一些轻量级交互机制来通信,例如 RPC.HTTP 等, ...
- spring cloud微服务实践二
在上一篇,我们已经搭建了spring cloud微服务中的注册中心.但只有一个注册中心还远远不够. 接下来我们就来尝试提供服务. 注:这一个系列的开发环境版本为 java1.8, spring boo ...
- Spring Cloud微服务Sentinel+Apollo限流、熔断实战总结
在Spring Cloud微服务体系中,由于限流熔断组件Hystrix开源版本不在维护,因此国内不少有类似需求的公司已经将眼光转向阿里开源的Sentinel框架.而以下要介绍的正是作者最近两个月的真实 ...
- Spring Cloud微服务限流之Sentinel+Apollo生产实践
Sentinel概述 在基于Spring Cloud构建的微服务体系中,服务之间的调用链路会随着系统的演进变得越来越长,这无疑会增加了整个系统的不可靠因素.在并发流量比较高的情况下,由于网络调用之间存 ...
- 如何优化Spring Cloud微服务注册中心架构?
作者: 石杉的架构笔记 1.再回顾:什么是服务注册中心? 先回顾一下什么叫做服务注册中心? 顾名思义,假设你有一个分布式系统,里面包含了多个服务,部署在不同的机器上,然后这些不同机器上的服务之间要互相 ...
- spring cloud微服务快速教程之(七) Spring Cloud Alibaba--nacos(一)、服务注册发现
0.前言 什么是Spring Cloud Alibaba? Spring Cloud Alibaba 是阿里开源的,致力于提供微服务开发的一站式解决方案.此项目包含开发分布式应用微服务的必需组件,方便 ...
- Spring Cloud 微服务架构全链路实践
阅读目录: 1. 网关请求流程 2. Eureka 服务治理 3. Config 配置中心 4. Hystrix 监控 5. 服务调用链路 6. ELK 日志链路 7. 统一格式返回 Java 微服务 ...
- Spring Cloud微服务中网关服务是如何实现的?(Zuul篇)
导读 我们知道在基于Spring Cloud的微服务体系中,各个微服务除了在内部提供服务外,有些服务接口还需要直接提供给客户端,如Andirod.IOS.H5等等. 而一个很尴尬的境地是,如果直接将提 ...
- 全链路实践Spring Cloud 微服务架构
Spring Cloud 微服务架构全链路实践Spring Cloud 微服务架构全链路实践 阅读目录: 网关请求流程 Eureka 服务治理 Config 配置中心 Hystrix 监控 服务调用链 ...
- Spring Cloud 微服务六:调用链跟踪Spring cloud sleuth +zipkin
前言:随着微服务系统的增加,服务之间的调用关系变得会非常复杂,这给运维以及排查问题带来了很大的麻烦,这时服务调用监控就显得非常重要了.spring cloud sleuth实现了对分布式服务的监控解决 ...
随机推荐
- linux 下 多进程与多线程
[Linux]多进程与多线程之间的区别 http://blog.csdn.net/byrsongqq/article/details/6339240 网络编程中设计并发服务器,使用多进程与多线程 ,请 ...
- s 中日期 转换成时间戳 例如2013-08-30 转换为时间戳
以前遇到过一个关于时间戳的问题,为了不被大家鄙视,先说一下概念. 具体时间戳怎么定义的我也不清楚,但百度百科中有这么一句:“时间戳是自 1970 年 1 月 1 日(00:00:00 GMT)至当前时 ...
- hdu 4908(思路题)
BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Alfred添加百度搜索
Alfred默认的搜索只有 google; amazon和wikipakia, 我想加个百度搜索,怎么添加呢? 1.首先添加百度搜索,添加http://www.baidu.com/s?wd={quer ...
- Android Studio插件Gsonformat的安装和使用
在开发中,我们获得服务端的json数据后要建立自己的bean,但是一条一条写相当麻烦,使用了GsonFormat插件,用起来非常方便. 安装 方法1: 1.Android studio File-&g ...
- 51nod 1006 最长公共子序列Lcs 【LCS/打印path】
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...
- Scaling the Messages Application Back End 【转】
11年的blog. Facebook Messages seamlessly integrates many communication channels: email, SMS, Facebook ...
- QQ聊天窗口上的广告与QQ弹窗广告的完美屏蔽去除
涉及的软件 1. QQ (笔者的为v8.2版本) 2. Win7 3. ADSafe(3.13.308.9900正式版) 前言 QQ广告十分讨人厌,除了QQ弹窗的广告,让人十分反感外,最近发现QQ聊 ...
- 鸟哥的linux私房菜服务器架设篇之准备工作和网络基础
架设服务器的基本功课 1基础网络的基本概念,以方便进行联网和设定及除错 2熟悉操作系统的简易操作:包括登录分析,账号管理,文本编辑器的使用等等的技巧 3信息安全方面:包括防火墙与软件更新方面的相关知识 ...
- 初始----python数字图像处理--:环境安装与配置
一提到数字图像处理编程,可能大多数人就会想到matlab,但matlab也有自身的缺点: 1.不开源,价格贵 2.软件容量大.一般3G以上,高版本甚至达5G以上. 3.只能做研究,不易转化成软件. 因 ...