springcloud registry ----- Consul

Outline

  • The main scenarios: service discovery, service isolation, service configuration
  • HashiCorp company develops, use the Go language
  • Built-in registration and service discovery framework, distributed consensus protocol, health monitoring, Key / Value store, multi-data center solutions
  • Raft use algorithms to ensure consistency, more direct than the complex Paxos algorithm, comparatively speaking, zookeeper uses Paxos, and etcd used is the Raft
  • Supports http and dns protocol interface
  • Official web management interface
  • Consul is a strong consistency (CP): Registration will be slower than Eureka, success requires a majority node writes only considered successful registration. In addition, when the leader of hang up, during the re-election of the entire consul is not available to maintain strong consistency at the expense of usability.

Download and install

  • Download: https: //www.consul.io/downloads.html
  • According to their own computer systems to download files corresponding to the version I downloaded window, direct installation.
  • I unzip the downloaded file to D: \ under software \ consul directory, then run the consul agent -dev -client = 0.0.0.0 command to start the consul
  • To the browser input http://127.0.0.1:8500/, you can log in the admin interface consul Here Insert Picture Description
    to consul here you can start using the

Service Registration

consul support to register via http or dns way. We are more concerned about the http, take a look at is how to register.

  • The first is to find the interface needs to send registration: http: // ip: 8500 / v1 / catalog / register
  • Interface data, json form:
{
	"Datacenter":"dc1",
	"Node":"node01",
	"Address":"192.168.74.102",
	"Service":{
		"ID":"mysql-01",
		"Service":"mysql",
		"tags":["master","v1"],
		"Address":"192.168.74.102",
		"Port":3306
	}
}
  • Test, where I can use a simulation software to send http request - Su Fei development assistant, sent a PUT request Here Insert Picture Description
    Here Insert Picture Description
    the test is successful, it can be seen, the high degree of integration consul, can be very easy to develop. Another service acquisition interface, are interested can look at Baidu, is not here for show, in springcloud, the introduction of this dependence has everything wraps.

springcloud introduced consul

Consumer service providers and a service

I am here to follow the example of Eureka, using springboot build two simple service, which in addition to an ordinary springboot project, what is not

Introducing operation related dependent consul

<!-- springcloud 提供的对基于consul 的服务发现 -->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- acuator 的健康检查 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Configuration

server:
  port: 9001 #端口
spring:
  application:
    name: product-service #服务名称
  #配置consul
  cloud:
    consul:
      host: 127.0.0.1 #注册中心的主机ip地址
      port: 8500 #注册中心的端口
      discovery:
        #是否需要注册
        register: true
        #注册的实例id(唯一标志)
        instance-id: ${spring.application.name}-1
        #服务的名称
        service-name: ${spring.application.name}
        #服务的请求端口
        port: ${server.port}
        #指定开启ip地址注册
        prefer-ip-address: true
        #当前服务的请求ip
        ip-address: ${spring.cloud.client.ip-address}

Service call

As with the service provider, the service consumer configured, then call the service in the service consumers. Similarly, we need restTemplate, In addition, the consul dependencies are also integrated support for ribbon, and therefore, it can be treated restTemplate

@Bean
@LoadBalanced
public RestTemplate restTemplate(){
    return new RestTemplate();
}

Then use restTemplate access to relevant data from the service provider

//注入restTemplate
@Autowired
private RestTemplate restTemplate;


@RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
public String get(@PathVariable Long id){
    //通过instance里面的信息,组合成url,访问服务接口
    return restTemplate.getForObject("http://product-service/product/getProduct/"+id,String.class);
}

Self-test

Published 22 original articles · won praise 0 · Views 996

Guess you like

Origin blog.csdn.net/weixin_43119903/article/details/104879863