spring cloud搭建服务注册中心并注册服务

我会带着你远行 2021-09-28 03:08 490阅读 0赞

搭建服务注册中心

  1. 添加依赖

    <?xml version=”1.0” encoding=”UTF-8”?>


    4.0.0

    org.springframework.boot
    spring-boot-starter-parent
    2.0.1.RELEASE


    com.zyl
    eureka-server
    0.0.1-SNAPSHOT
    eureka-server
    Demo project for Spring Boot


    UTF-8
    UTF-8
    1.8
    Finchley SR1




    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-server
    2.0.1.RELEASE



    org.springframework.boot
    spring-boot-autoconfigure
    2.0.4.RELEASE
    compile






    org.springframework.cloud
    spring-cloud-dependencies
    ${spring-cloud.version}
    pom
    import







    org.springframework.boot
    spring-boot-maven-plugin






    spring-milestones
    Spring Milestones
    https://repo.spring.io/milestone



  2. 启动服务注册中心 @EnableEurekaServer

    @EnableEurekaServer
    @SpringBootApplication
    public class EurekaServerApplication {

    1. public static void main(String[] args) {
    2. SpringApplication.run(EurekaServerApplication.class, args);
    3. }

    }

  3. 配置文件 (禁用客户端行为)

    server.port=1111

    eureka.instance.hostname=localhost
    eureka.client.register-with-eureka=false
    eureka.client.fetch-registry=false
    eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

由于这是一个服务注册中心,则不应该像注册中心注册自己 eureka.client.register-with-eureka=false

注册中心的职责是维护服务实例,并不需要去检索服务 eureka.client.fetch-registry=false

注册服务提供者

  1. 添加依赖

    <?xml version=”1.0” encoding=”UTF-8”?>


    4.0.0

    org.springframework.boot
    spring-boot-starter-parent
    2.0.1.RELEASE


    com.zyl
    demoserver
    0.0.1-SNAPSHOT
    demoserver
    Demo project for Spring Boot


    UTF-8
    UTF-8
    1.8
    Finchley SR2




    org.springframework.boot
    spring-boot-starter-web


    org.springframework.boot
    spring-boot-starter


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client
    2.0.1.RELEASE



    org.springframework.boot
    spring-boot-starter-test
    test






    org.springframework.cloud
    spring-cloud-dependencies
    ${spring-cloud.version}
    pom
    import







    org.springframework.boot
    spring-boot-maven-plugin




  2. 创建一个访问接口

    @RestController
    public class UserController {

    1. @RequestMapping(value = "/index")
    2. public String index(){
    3. return "Hello World";
    4. }

    }

  3. 在主服务上添加注解

    @EnableEurekaClient
    @SpringBootApplication
    public class DemoserverApplication {

    1. public static void main(String[] args) {
    2. SpringApplication.run(DemoserverApplication.class, args);
    3. }

    }

  4. 配置文件

    spring.application.name=demo-server
    eureka.client.service-url.defaultZone=http://localhost:1111/eureka/

发表评论

表情:
评论列表 (有 0 条评论,490人围观)

还没有评论,来说两句吧...

相关阅读