SpringCloud study notes 5 - SpringCloud build Consul registry

Eight, Consul registry environment to build

    1,  Consul Introduction

                Consul is an open source distributed service discovery and configuration management system, the company HashiCorp go with language development. It has many advantages, including: raft-based protocol, it is relatively simple; support health check, while supporting dns and http protocols, supports WAN clusters across data centers ,, provide cross-platform graphical interface, support for linux, mac, windows.

                Consul integration SpringCloud learning website: https: //springcloud.cc/spring-cloud-consul.html

                Consul Download: https: //www.consul.io/downloads.html

     2, Consul environment to build

              (1) windows Installation

                        Official website to download windows version, executable file, set the environment variable After decompression, the use of direct consul in dos command window, add the directory where the consul behind the path, and then execute the start command: consul Agent -dev -ui -node = GH     where: - dev development server mode is activated, -node node named gh, -ui can access the interface, default access, and then test the access address http: // localhost: 8500. (Default port 8500)

              (2) mac version installed,

                   1) os mac command to view system version: uname -an, the official website to download the corresponding version of the mac version of the consul file.

                   2) the decompressed file copied to the consul / usr / local / bin under sudo cp consul / usr / local / bin

                   3) Open the bin file, perform consul, see the consul command, as you were successful.

                    

       4) Enter the start command, the browser address bar, enter http: // localhost: 8500, the effect is as follows:

              

 

Nine, SpringCloud Consul alternative use as a registration center Eureka

   1, the establishment maven project, import the relevant dependent jar package

     

<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.gonghua</groupId>
  <artifactId>springcloud-consul-member</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <parent>
  	<groupId>org.springframework.boot</groupId>
  	<artifactId>spring-boot-starter-parent</artifactId>
  	<version>2.0.1.RELEASE</version>
  </parent>
  
  <!-- 管理依赖 -->
  <dependencyManagement>
  		<dependencies>
  			<dependency>
  				<groupId>org.springframework.cloud</groupId>
  				<artifactId>spring-cloud-dependencies</artifactId>
  				<version>Finchley.M7</version>
  				<type>pom</type>
  				<scope>import</scope>
  			</dependency>
  		</dependencies>
  </dependencyManagement>
  
  <dependencies>
  <!-- springboot整合web组件 -->
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-web</artifactId>
  	</dependency>
  	<!-- springboot整合consul客户端 -->
    <dependency>
  		<groupId>org.springframework.cloud</groupId>
  		<artifactId>spring-cloud-starter-consul-discovery</artifactId>
  	</dependency>
  	
  	 
  </dependencies>
  
  <!-- 注意:这里必须添加,否则各种依赖会出问题 -->
  <repositories>
  	<repository>
  		<id>spring-milestones</id>
  		<name>Spring Milestones</name>
  		<url>https://repo.spring.io/libs-milestone</url>
  		<snapshots>
  			<enabled>false</enabled>
  		</snapshots>
  	</repository>
  </repositories>
 
</project>

2, the application.properties configuration, as follows:

##服务端口号
server.port=8005
##服务别名--服务注册到注册中心名称
spring.application.name=consul-member
##注册到consul服务地址
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
##服务地址直接为ip地址
spring.cloud.consul.discovery.hostname=127.0.0.1
##默认情况下,服务注册到注册中心,地址随机生成的英文

3, start the service, access consul page, the effect is as follows:

 

Service consumers do not write the code, and other services as consumers only need to change the pom-dependent and yml configuration can be.

 

Additional: How to Get to the list of services on the registry information, as follows:

    //获取注册中心上的服务列表信息
	@RequestMapping
	public List<ServiceInstance> discoveryClientMember(){
		List<ServiceInstance> instances = discoveryClient.getInstances("zk-member");
		for (ServiceInstance serviceInstance : instances) {
			System.out.println("url:---"+serviceInstance.getUri());
		}
		return instances;
	}

 

Published 27 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/gonghua0502/article/details/103264302