1. 程式人生 > 實用技巧 >Consul 服務註冊與發現

Consul 服務註冊與發現

Consul是一種分散式,高度可用且具有資料中心感知能力的解決方案,用於跨動態,分散式基礎架構連線和配置應用程式。

1.下載consul.exe

2.在consul.exe資料夾位址列輸入cmd

3.輸入consul agent -dev

4.訪問http://localhost:8500

cloud-provider-paymentcounsul
<dependencies>
        <!--SpringCloud consul-server-->
        <dependency>
            <groupId>org.springframework.cloud</
groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>com.etc.cloud</groupId> <artifactId>cloud-api-commons</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <
artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
server:
  port: 8005

spring:
  application:
    name: cloud-provider-paymentcounsul

  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        hostname: 127.0.0.1
        service-name: ${spring.application.name}
@Slf4j
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8005 {
    public static void main(String[] args) {
        SpringApplication.run( PaymentMain8005.class,args);
        log.info("****************PaymentMain8005 啟動 ************ ");
    }
}
@RestController
public class PaymentController {
    @Value("${server.port}")
    private String serverPort;


    @GetMapping("/payment/consul")
    public String payment(){
        return "spring cloud consul " + serverPort + UUID.randomUUID();
    }
}

consul用@EnableDiscoveryClient註解

cloud-consumer-orderconsul:

<dependencies>
        <!--SpringCloud consul-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.etc.cloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
server:
  port: 8000

spring:
  application:
    name: cloud-consumer-orderconsul

  cloud:
    consul:
      #consul 地址
      host: localhost
      port: 8500
      discovery:
        hostname: 127.0.0.1
        service-name: ${spring.application.name}
@Slf4j
@EnableDiscoveryClient
@SpringBootApplication
public class OrderMain8000 {
    public static void main(String[] args) {
        SpringApplication.run( OrderMain8000.class,args);
        log.info("****************OrderMain8000 啟動 ************ ");
    }
}
@RestController
public class OrderController {
    private static  final String URL = "http://cloud-provider-paymentcounsul";

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/consul")
    public String paymentinfio(){
        return restTemplate.getForObject(URL + "/payment/consul",String.class);
    }
}
@Configuration
public class SpringApplicationConfig {
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}
 @LoadBalanced  開啟負載均衡的註解
讓這個RestTemplate在請求時擁有客戶端負載均衡的能力
在springcloud 的使用中如果使用RestTemplate來進行rpc遠端呼叫的時候 ,
在呼叫會員服務的時候有的會選擇使用會員服務端在註冊中心註冊的名稱來進行遠端呼叫
也有的會直接使用域名進行呼叫,在這個過程中如果使用會員的註冊名稱的話在RestTemplate 那裡開啟 負載均衡 : @LoadBalanced
如果是使用域名進行呼叫就不用開啟負載均衡