spring cloud Eureka

spring cloud Eureka (服务注册与发现中心)在spring cloud大家庭中就扮演了服务注册与发现的角色

应用:

1、引入依赖:

    <parent>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-parent</artifactId>  
            <version>1.4.0.RELEASE</version>  
        </parent>  
      
        <dependencies>  
            <dependency>  
              <groupId>org.springframework.cloud</groupId>  
              <artifactId>spring-cloud-starter-eureka-server</artifactId>  
            </dependency>  
            <dependency>  
              <groupId>org.springframework.boot</groupId>  
              <artifactId>spring-boot-starter-test</artifactId>  
              <scope>test</scope>  
            </dependency>  
         </dependencies>  
          <dependencyManagement>  
            <dependencies>  
              <dependency>  
                <groupId>org.springframework.cloud</groupId>  
                <artifactId>spring-cloud-dependencies</artifactId>  
                <version>Camden.SR2</version>  
                <type>pom</type>  
                <scope>import</scope>  
              </dependency>  
            </dependencies>  

          </dependencyManagement>

2、添加application.yml配置文件:

        server: port: 8020

        eureka:
              client:
                    register-with-eureka: false    #是否将eureka自身作为应用注册到eureka注册中心
                    fetch-registry: false          #为true时,可以启动,但报异常:Cannot execute request on any known server
                    service-url:
                    defaultZone: http://user:password@localhost:8020/eureka


3、在启动类上添加注解@EnableEurekaServer:

        @EnableEurekaServer
        @SpringBootApplication
        @EnableFeignClients
        public class MovCenterBootstrap {
            public static void main(String[] args) {
            SpringApplication.run(MovCenterBootstrap.class, args);

            }

        }


4、运行main方法,访问http://localhost:8020/

   如果访问正常就表示Eureka server已经正常启动。那接下来就开始client客户端,即服务的编写,将每个服务注册到这个server中心。


5、创建Eureka服务提供方:

        1》引入pom文件:

            <parent>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-starter-parent</artifactId>  
                <version>1.3.5.RELEASE</version>  
                <relativePath/> <!-- lookup parent from repository -->  
            </parent>  
 
            <dependencies>  
                <dependency>  
                    <groupId>org.springframework.boot</groupId>  
                    <artifactId>spring-boot-starter-test</artifactId>  
                    <scope>test</scope>  
             </dependency>  
 
            <dependency>  
                <groupId>org.springframework.cloud</groupId>  
                <artifactId>spring-cloud-starter-eureka</artifactId>  
            </dependency>  
        </dependencies>  
 
        <dependencyManagement>  
            <dependencies>  
                <dependency>  
                <groupId>org.springframework.cloud</groupId>  
                <artifactId>spring-cloud-dependencies</artifactId>  
                <version>Brixton.RELEASE</version>  
                <type>pom</type>  
                <scope>import</scope>  
            </dependency>  
            </dependencies>  

        </dependencyManagement>


        2》添加application.yml配置文件

                server:port: 8010

                #spring
                spring: application: name: emos-user-center

                eureka:
                      client:
                            serviceUrl:
                                 defaultZone: http://user:password@localhost:8020/eureka  #注册 中心已经开启认证
                       instance:
                              prefer-ip-address: true
                               instanceId: ${spring.application.name}:${spring.application.instance_id:${server.port}}

        3》启动服务类:

                @SpringBootApplication
                @EnableEurekaClient //启动EnableEureka客户端
                @RestController
                public class UserCenterBootstrap {

                    @GetMapping("/hello/{name}")
                    public String hello(@PathVariable String name) {
                        System.out.println(name + " welcome . My is microservice provider user");
                        return name + " welcome . My is microservice provider user";
                    }

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

                }

        4》启动服务,刷新http://localhost:8020/ 服务注册面板,会看到8010端口的服务注册到了Eureka server端,显示在了server面板页面。

        

EMOS-USER-CENTERn/a (1)(1)UP (1) - emos-user-center:8010

此时如果将服务提供端的端口改为8040,并重启服务提供端的服务,启动完之后刷新服务注册中心,这时会显示有两个服务注册到服务注册中心:

EMOS-USER-CENTERn/a (2)(2)UP (2) - emos-user-center:8040 , emos-user-center:8010

            





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值