spring-cloud03-consul
官网的安装说明https://learn.hashicorp.com/tutorials/consul/get-started-install
1.下载安装
环境:阿里云服务器,consul1.9.5,
consul是google开源的一个使用go语言开发的服务发现、配置管理中心服务。内置了服务注册与发现框 架、分布一致性协议实现、健康检查、Key/Value存储、多数据中心方案,不再需要依赖其他工具(比如ZooKeeper等)。服务部署简单,只有一个可运行的二进制的包。每个节点都需要运行agent,他有两种运行模式server和client。每个数据中心官方建议需要3或5个server节点以保证数据安全,同时保证server-leader的选举能够正确的进行。
consul用于微服务下的服务治理,主要特点有:服务发现、服务配置、健康检查、键值存储、安全服务通信、多数据中心等
1.1下载
https://www.consul.io/downloads

选择linux版本下载
1.2.上传到服务器

1.3解压
命令:unzip consulzip文件名字

1.4把解压出来的consul文件移动到可用目录下
命令:echo $PATH 查看可用目录,下面列出了很多目录,冒号隔开的。选择一个目录,把consul文件移动到该目录下。我选择的是/usr/local/bin
1.5验证安装
如下表示成功
命令:consul

1.6启动consul代理
命令:./consul agent -dev -ui -node=consul-dev -client=192.168.128.149
后面的ip是阿里云的私网ip
1.7阿里云配置规则,打开8500端口
1.8访问
2.搭建服务提供者
2.1、新建一个maven项目(cloud-providerconsul-payment8006)
结构如下:

2.2、引入依赖,编辑pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.atguigu.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>cloud-providerconsul-payment8006</artifactId> <dependencies>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<!--热部署-代码改变自动编译-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!-- consul-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency> </dependencies> </project>
2.3、编辑配置文件application.yml
server:
port: 8006 #当前服务端口
spring:
application:
name: consul-provider-payment #服务名称 注意这个名称不要太长,我前面名字是consul-provider-payment-con,导致访问consul页面的额时候这个服务有红叉
cloud:
consul:
host: 59.120.138.4 #consul所在服务器ip
port: 8500 #consul端口
discovery:
service-name: ${spring.application.name}
heartbeat:
enabled: true #维持心跳
2.4、编写主启动类
package com.atguigu.springcloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /**
* @Classname Payment8006
* @Description TODO
* @Date 2021/4/22 0022 下午 2:23
* @Created by jcc
*/
@SpringBootApplication
@EnableDiscoveryClient
public class Payment8006 {
public static void main(String[] args) {
SpringApplication.run(Payment8006.class,args);
}
}
2.5、编写Controller
package com.atguigu.springcloud.controller; import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*; import javax.annotation.Resource;
import java.util.UUID; @RestController
@Slf4j
public class PaymentController { @Value("${server.port}")
private String serverPort; @GetMapping(value = "/payment/consul")
public String paymentConsul(){
return "springcloud with consul: "+serverPort+"\t"+ UUID.randomUUID().toString();
}
}
2.6、启动项目,测试
1)查看consul页面

2)使用地址:http://localhost:8006/payment/consul
3搭建服务消费者
1、新建一个maven项目(cloud-consumerconsul-order80)
项目结构如下:

2、引入pom依赖,同上(与服务提供者依赖相同)
3、编辑application.yml文件
server:
port: 80
spring:
application:
name: consul-consumer-order
cloud:
consul:
host: 59.120.138.4
port: 8500
discovery:
service-name: ${spring.application.name}
heartbeat:
enabled: true
4、编写主启动类
package com.atguigu.springcloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /**
* @Classname ConsulOrder80
* @Description TODO
* @Date 2021/4/22 0022 下午 3:14
* @Created by jcc
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ConsulOrder80 {
public static void main(String[] args) {
SpringApplication.run(ConsulOrder80.class,args);
}
}
5、编辑配置类,注入RestTemplate对象
package com.atguigu.springcloud.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate; /**
* @Classname ApplicationContextConfig
* @Description TODO
* @Date 2021/4/22 0022 下午 3:02
* @Created by jcc
*/
@Configuration
public class ApplicationContextConfig {
@LoadBalanced
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
6、编辑Controller
package com.atguigu.springcloud.controller; import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate; import javax.annotation.Resource;
import java.util.UUID; @RestController
@Slf4j
public class PaymentController { @Value("${server.port}")
private String serverPort; public static final String INVOME_URL = "http://consul-provider-payment"; @Resource
private RestTemplate restTemplate; @GetMapping("/consumer/payment/consul")
public String payment (){
String result = restTemplate.getForObject(INVOME_URL+"/payment/consul",String.class);
return result;
}
}
7、启动项目测试
1)查看consul页面

2)访问http://localhost/consumer/payment/consul

spring-cloud03-consul的更多相关文章
- Spring Cloud Consul入门
1. Consul介绍 Consul是一套开源的分布式服务发现和配置管理系统,支持多数据中心分布式高可用.Consul是HashiCorp( Vagrant的创建者)开发的一个服务发现与配置项目,用G ...
- Spring Cloud Consul 实现服务注册和发现
Spring Cloud 是一个基于 Spring Boot 实现的云应用开发工具,它为基于 JVM 的云应用开发中涉及的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布 ...
- Spring Cloud Consul使用——服务注册与发现(注册中心)
整理自该文章 一.Consul 服务端接下来我们开发 Consul 的服务端,创建一个 spring-cloud-consul-producer 项目 1.添加依赖包 <dependencies ...
- Spring Cloud Consul
1.2.0.RELEASE 该项目通过自动配置并绑定到Spring环境和其他Spring编程模型成语,为Spring Boot应用程序提供Consul集成.通过几个简单的注释,您可以快速启用和配置应用 ...
- Spring Cloud Consul 之Greenwich版本全攻略
什么是Consul Consul是HashiCorp公司推出的开源软件,使用GO语言编写,提供了分布式系统的服务注册和发现.配置等功能,这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全 ...
- 服务注册发现、配置中心集一体的 Spring Cloud Consul
前面讲了 Eureka 和 Spring Cloud Config,今天介绍一个全能选手 「Consul」.它是 HashiCorp 公司推出,用于提供服务发现和服务配置的工具.用 go 语言开发,具 ...
- Spring Cloud Consul Config 知识点
Spring Cloud Consul Config 是 Config Server 和 Client的替代方案. 搭建一个配置中心,可以选择的方案: Spring Cloud Config 或者 S ...
- spring cloud consul上下线体验
spring cloud consul中默认会将spring.application.name作为ID 同一服务起多个实例时,ID默认会变成${spring.application.name}-${s ...
- Spring Cloud Consul综合整理
该项目通过自动配置和Spring环境以及其他Spring编程模型习惯用法提供了Spring Boot应用程序的Consul集成. 通过一些简单的注释,您可以快速启用和配置应用程序内的通用模式,并使用基 ...
- Spring Cloud Consul 入门指引
1 概述 Spring Cloud Consul 项目为 Spring Boot 应用程序提供了与 Consul 的轻松集成. Consul 是一个工具,它提供组件来解决微服务架构中一些最常见的挑战: ...
随机推荐
- Go语言核心36讲40
我相信,经过上一次的学习,你已经对strings.Builder和strings.Reader这两个类型足够熟悉了. 我上次还建议你去自行查阅strings代码包中的其他程序实体.如果你认真去看了,那 ...
- 二叉树总结——BiTree
在C++编译器下可直接运行 #include <stdio.h> #include <malloc.h> //算法思想:先读入根结点数据,并且创建根结点,在读入左子树数据并创建 ...
- React实用插件收集
1.react-img-editor 图片编辑 demo: npm install react-img-editor -S 引入和使用 import ReactImgEditor from 'reac ...
- 一个jsqlparse+git做的小工具帮我节省时间摸鱼
背景 前些时间做了个小工具解决了团队内数据库脚本检验&多测试环境自动执行的问题,感觉挺有意思,在这跟大家分享一下. 工具诞生之前的流程是这样: 1.开发人员先在开发环境编写脚本&执行: ...
- day20-web开发会话技术02
WEB开发会话技术02 6.Cookie的生命周期 默认情况下,Cookie只在浏览器的内存中存活,也就是说,当你关闭浏览器后,Cookie就会消失.但是也可以通过方法设置cookie的生存时间. c ...
- 踩坑记录:Redis的lettuce连接池不生效
踩坑记录:Redis的lettuce连接池不生效 一.lettuce客户端 lettuce客户端 Lettuce 和 Jedis 的都是连接Redis Server的客户端程序.Jedis在实现上是直 ...
- JAVA中生成随机数Random VS ThreadLocalRandom性能比较
前言 大家项目中如果有生成随机数的需求,我想大多都会选择使用Random来实现,它内部使用了CAS来实现. 实际上,JDK1.7之后,提供了另外一个生成随机数的类ThreadLocalRandom,那 ...
- 侦察工具——Httrack
前言 web渗透学习笔记,实验环境为Metasploitable靶机上的DVWA.此随笔介绍Web渗透侦察工具Httrack Httrack 简介 Httrack能够克隆拷贝目标网站上的所有可访问.可 ...
- k8s本地联调工具kt-connect
1.Kt Connect简介 KT Connect ( Kubernetes Developer Tool ) 是轻量级的面向 Kubernetes 用户的开发测试环境治理辅助工具.其核心是通过建立本 ...
- 痞子衡嵌入式:Farewell, 我的写博故事2016-2019
-- 题图:苏州天平山枫叶 现在是 2022 年末,痞子衡又要起笔博文年终总结了,看着 2020 年之前的博文总结缺失,始终觉得缺憾,所以写下此篇 2016 - 2019 总结合辑.2016 年之前, ...