Consul 微服务注册与发现
来源:互联网 发布:c语言图形函数库 编辑:程序博客网 时间:2024/06/01 10:09
1. 创建Spring Boot项目
IDE :IntelliJ IDEA
File ->new->project->Spring Initializer 创建Spring Boot项目
pom.xml
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>consul_demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>consul_demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> <version>1.2.1.RELEASE</version> </dependency> <dependency> <groupId>com.corundumstudio.socketio</groupId> <artifactId>netty-socketio</artifactId> <version>1.7.12</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.5</version> </dependency> <!-- https://mvnrepository.com/artifact/com.orbitz.consul/consul-client --> <dependency> <groupId>com.orbitz.consul</groupId> <artifactId>consul-client</artifactId> <version>0.16.3</version> </dependency> <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.37</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
2. Controller
package com.example.demo.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map;/** * Created by zxl on 17-10-22. */@RestController@RequestMapping("/api")public class HttpController { /** * 健康检查 * @return */ @RequestMapping("/healthCheck") public Object healthCheck() { return "success"; } /** * 服务接口 * @return */ @RequestMapping("/hello") @ResponseBody public Object service() { return "hello"; }}
3. application.properties
spring.application.name=consul_demoserver.port=8080spring.cloud.consul.host=localhostspring.cloud.consul.port=8500spring.cloud.consul.enabled=truespring.cloud.consul.discovery.enabled=true#spring.cloud.consul.discovery.instanceId=${spring.application.name}//微服务名称spring.cloud.consul.discovery.serviceName=consulServiceTestspring.cloud.consul.discovery.hostname=http://${spring.cloud.consul.host}/api//健康检查spring.cloud.consul.discovery.healthCheckUrl=http://${spring.cloud.consul.host}/api/healthCheckspring.cloud.consul.discovery.healthCheckInterval=10sspring.cloud.consul.discovery.tags=dev
4. consul启动
进入consul目录执行如下脚本
consul_start.sh
export node=`hostname`killall -9 consul./consul agent -data-dir /software/consul -node=$node -bind=192.xx.xxx.xx(本机Ip) -join=192.yy.yy.yy(consul Server Ip)
5. 启动程序
执行main方法
package com.example.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@EnableDiscoveryClient@SpringBootApplicationpublic class ConsulDemoApplication { public static void main(String[] args) { SpringApplication.run(ConsulDemoApplication.class, args); }}
6. 服务发现
public class ConsulUtil { static Consul consul = Consul.builder().withHostAndPort( HostAndPort.fromString(PropertyUtils.getProperty("ConsulServerIp"))).build(); public static Consul getConsul() { return consul; } public static String getService(String serviceName, String tag) { String address = null; Consul consul = ConsulUtil.getConsul(); HealthClient healthClient = consul.healthClient(); ConsulResponse<List<ServiceHealth>> allHealthyServiceInstance = healthClient.getHealthyServiceInstances(serviceName); //获取健康的服务 List<ServiceHealth> response = allHealthyServiceInstance.getResponse(); List<Service> services = new ArrayList<>(response.size()); for (ServiceHealth serviceHealth : response) { services.add(serviceHealth.getService()); } Random random = new Random(); if (services.size() == 0) {//无法获取健康的服务 return null; } address = services.get(random.nextInt(services.size())).getAddress() + "/" + tag; return address; }}
阅读全文
0 0
- Consul 微服务注册与发现
- Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)[Dalston版]
- Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston版】
- Spring Cloud构建微服务架构:Consul服务注册与发现
- Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)
- Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)
- Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston版】
- 微服务注册发现集群搭建——Registrator + Consul + Consul-template + nginx
- 微服务注册发现集群搭建——Registrator + Consul + Consul-template + nginx
- [转载]微服务注册发现集群搭建——Registrator + Consul + Consul-template + nginx
- 微服务注册与发现
- spring cloud搭建微服务second-fiberhome(二):结合consul实现服务注册与服务发现
- etcd 微服务注册与发现
- Spring Cloud 微服务注册与发现
- Consul 服务注册与服务发现
- Consul 服务注册与服务发现
- Consul服务注册发现
- 微服务理论与实践(六)-服务注册与发现
- Window环境下Redis安装
- 周末训练笔记+hdu1576 A/B
- studio添加依赖的几种方法
- MIT6.824-lab2 raft
- git快速入门
- Consul 微服务注册与发现
- Leetcode 3 Longest Substring Without Repeating Characters
- 网络扫描与监听---网络与系统安全实验
- MySQL的视图与索引
- FPGA驱动OLED动态显示(Verilog代码)——Demo演示(链接)
- 设计模式之桥梁模式
- LCA 0.3
- 参数估计与假设检验
- 碎片fragment