springCloud入门(1)服务的注册与发现(eureka)

2020-03-08  本文已影响0人  吗丁啉要餐前吃

springCloud是由spring社区开发的一套开源的微服务框架,基于springboot的功能。因此,对springboot不太熟的同学,建议先去看看springboot相关的知识。

  <!--1.添加对springboot的支持-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.2.RELEASE</version>
    <relativePath/> 
  </parent>
<!--2.添加springcloud的版本-->
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
  </properties>
<!--3.添加对spring-cloud的依赖管理-->
<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
<!--添加eureka的支持-->
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

2.开启eureka服务

@SpringBootApplication
@EnableEurekaServer //开启eureka服务
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class,args);
    }
}

3.添加配置文件application.yml

server:
  port: 8763
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    <!--eureka client-->
     <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--spring web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

2.开启eureka客户端

@SpringBootApplication
@EnableEurekaClient  //开启eureka客户端功能
public class MicroPApplication {
    public static void main(String[] args) {
        new SpringApplicationBuilder(MicroPApplication.class).run(args);
    }
}

3.配置bootstrap.yml

spring:
  application:
    name: micro-provider
server:
  port: 9001
eureka:
  client:
    service-url:
      defaultZone: http://admin:admin@localhost:8763/eureka/

启动服务,打开localhost:8763地址,我们就可以看到,服务已经注册到eureka上了。


image.png
上一篇 下一篇

猜你喜欢

热点阅读