SpringCloud搭建Consul服务注册中心

首先创建一个maven工程,引入pom依赖

<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>
    <!--SpringCloud consul-server -->
    <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>

配置application.yml文件

###eureka 服务端口号
server:
  port: 8502
spring:
  application:
    ##服务的名称
    name: consul-test-demo
  ####consul注册中心地址
  cloud:
    consul:
      ###consult地址
      host: localhost
      ###consul端口号
      port: 8500
      discovery:
        ##服务的注册地址
        hostname: 192.168.2.1
###默认情况下 服务注册到注册中心,地址随机生成英文pc-******:

启动项目

@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }

    @RequestMapping("/getManber")
    public String getMamber(){

        return "会员服务调用了-Szx's Blog";
    }
}

Last modification:May 9th, 2019 at 05:13 pm
如果觉得这篇技术文章对你有用,请随意赞赏

Leave a Comment