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 是一个工具,它提供组件来解决微服务架构中一些最常见的挑战: ...
随机推荐
- 一个jsqlparse+git做的小工具帮我节省时间摸鱼
背景 前些时间做了个小工具解决了团队内数据库脚本检验&多测试环境自动执行的问题,感觉挺有意思,在这跟大家分享一下. 工具诞生之前的流程是这样: 1.开发人员先在开发环境编写脚本&执行: ...
- IIS部署WebApi跨域不生效
在IIS8.5上部署了WebApi程序,但是跨域不生效检查了前端和后端都没有问题. 后面才发现在应用程序池中需要设置为集成模式.经典模式下不能正常使用
- java - - spring:定时任务
转载:https://www.cnblogs.com/lishupeng/p/7680644.html 开启定时任务: <beans xmlns="http://www.springf ...
- python安装dlib库报错
问题描述 我是debain 系的linux系统没遇到这个问题,在centos系统遇到的 Collecting dlib Downloading http://mirrors.cloud.aliyunc ...
- 文件服务器 — File Browser
前言 一直想部署一套文件服务器,供队友之间相互传输文件.平时用微信发送文件真的太烦了,每发送或者接收一次都会有一个新的文件,造成重复文件太多了.文件服务器统一管理,自己需要什么文件再下载. 前面也安装 ...
- 【SQL必知必会】SQL知识查缺补漏
一.使用函数处理数据 1.字符串处理函数-顾客登录名[sql22] 思路1:substring(word,1,n).upper.concat SELECT cust_id, cust_name, UP ...
- python3中的常见知识点2
python3中的常见知识点2 列表与栈和队列 map()函数 python列表遍历的4种方式 参考链接 列表栈和队列 1.列表作为栈使用 栈:先进后出,First In Last Out 使用 ap ...
- 学习ASP.NET Core Blazor编程系列十六——排序
学习ASP.NET Core Blazor编程系列文章之目录 学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应 ...
- 【深入浅出SpringCloud原理及实战】「SpringCloud-Alibaba系列」微服务模式搭建系统基础架构实战指南及版本规划踩坑分析
Spring Cloud Alibaba Nacos Discovery Spring Boot 应用程序在服务注册与发现方面提供和 Nacos 的无缝集成. 通过一些简单的注解,您可以快速来注册一个 ...
- 使用 System.Text.Json 时,如何处理 Dictionary 中 Key 为自定义类型的问题
在使用 System.Text.Json 进行 JSON 序列化和反序列化操作时,我们会遇到一个问题:如何处理字典中的 Key 为自定义类型的问题. 背景说明 例如,我们有如下代码: // 定义一 ...