天天看點

Consul服務注冊中心cloud注冊中心Consul的(window)安裝代碼版本注冊服務提供者(在SpringBoot web基礎上)注冊服務消費者(在SpringBoot web基礎上)遇到的坑

cloud注冊中心

SpringCloud的服務注冊中心有Eureka、Zookeeper、Consul和Nacos

Eureka(AP)在SpringBoot1.x中比較合适,但是現在是SpringBoot2.x,而且Eureka已經停止更新了,就是不建議在使用了

Zookeeper(CP)是dubbo的注冊中心,曾經的Eureka都涼了,萬一我用别的注冊中心又涼了怎麼辦,不如用zookeeper

Consul(CP)是Go語言開發的,而Java開發者可能會對不同的語言有偏見。

Nacos是比較推薦的服務注冊中心。

Consul的(window)安裝

Consul下載下傳位址 https://www.consul.io/downloads.html

Consul服務注冊中心cloud注冊中心Consul的(window)安裝代碼版本注冊服務提供者(在SpringBoot web基礎上)注冊服務消費者(在SpringBoot web基礎上)遇到的坑

下載下傳完解壓後是一個consul.exe

檢視consul的版本(不用配置環境變量)

consul --version
           
Consul服務注冊中心cloud注冊中心Consul的(window)安裝代碼版本注冊服務提供者(在SpringBoot web基礎上)注冊服務消費者(在SpringBoot web基礎上)遇到的坑

啟動consul

consul agent -dev
           
Consul服務注冊中心cloud注冊中心Consul的(window)安裝代碼版本注冊服務提供者(在SpringBoot web基礎上)注冊服務消費者(在SpringBoot web基礎上)遇到的坑

在浏覽器輸入

http://localhost:8500/
           
Consul服務注冊中心cloud注冊中心Consul的(window)安裝代碼版本注冊服務提供者(在SpringBoot web基礎上)注冊服務消費者(在SpringBoot web基礎上)遇到的坑

代碼版本

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
           
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>
           

代碼下載下傳

https://github.com/cbeann/share/tree/master/springcloud-Consul-demo

注冊服務提供者(在SpringBoot web基礎上)

添加依賴

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
           

修改application.yml檔案

server:
  port: 8001

# 服務别名--consul注冊中心名稱
spring:
  application:
    name: provider-8001
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}
           

修改主啟動類 

@EnableDiscoveryClient
           

編寫controller

@RestController
public class HelloController {

    @Value("${server.port}")
    private String serverPort;


    @RequestMapping("/provider/consul")
    public String paymentConsul() {
        return "springCloud with consul: " + serverPort + "\t" + UUID.randomUUID().toString();
    }

}
           

  啟動并且自測

http://localhost:8001/provider/consul

注冊服務消費者(在SpringBoot web基礎上)

添加依賴

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
           

 修改application.yml檔案

server:
  port: 80

# 服務别名---consul注冊中心名稱
spring:
  application:
    name: consumer-80
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}
           

修改主啟動類 

@EnableDiscoveryClient
           

添加RestTemplate配置類

@Configuration
public class ApplicationContextConfig {

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

編寫controller

@RestController
public class HelloController {
    //調用服務的名稱provider-8001
    private static final String INVOKE_URL = "http://provider-8001";
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping(value = "/consumer/consul")
    public String paymentInfo(){
        String result = restTemplate.getForObject(INVOKE_URL + "/provider/consul", String.class);
        return result;
    }

}
           

啟動自測

http://localhost/consumer/consul

遇到的坑

如下圖所示,正常情況是綠色對勾是2,這樣通路會報 No instances available for provider

Consul服務注冊中心cloud注冊中心Consul的(window)安裝代碼版本注冊服務提供者(在SpringBoot web基礎上)注冊服務消費者(在SpringBoot web基礎上)遇到的坑

解決辦法

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
           

繼續閱讀