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
如果是使用域名进行调用就不用开启负载均衡

posted @ 2020-08-28 10:08  neona  阅读(328)  评论(0编辑  收藏  举报