spring cloud注册服务

来源:互联网 发布:厦门创易网络 编辑:程序博客网 时间:2024/05/22 12:21

前面两篇博文已经将注册中心搭建好,下面我们将服务注册到注册中心。服务的注册使用的还是eureka.

一、eureka的常用注解:

  1. @EnableEurekaClient: 该注解表明应用既作为eureka实例又为eureka client 可以发现注册的服务
  2. @EnableEurekaServer: 该注解表明应用为eureka服务,有可以联合多个服务作为集群,对外提供服务注册以及发现功能
    这节我们就使用@EnableEurekaClient将服务注册到注册中心

二、注册服务

1.新建一个spring-boot应用

这里写图片描述

2.修改pom.xml

<dependencies><!--eureka客户端依赖 -->    <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-starter-eureka</artifactId>    </dependency><!--spring boot 单元测试 -->    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-test</artifactId>        <scope>test</scope>    </dependency>    <!--表明是一个web应用 -->    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <!--为方便开发人员,在修改代码后不需要重新启动应用 -->    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-devtools</artifactId>    </dependency>    </dependencies><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> 

3.在启动类中添加@EnableEurekaClient

package com.example;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClientpublic class SpringCloudClientPersonApplication {    public static void main(String[] args) {        SpringApplication.run(SpringCloudClientPersonApplication.class, args);    }}

4.编写一个rest风格的controller

package com.example.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/person")public class PersonController {    @RequestMapping(value="/get",method=RequestMethod.GET)    public String getPerson(String id){        return "tom";    }}

5.修改properties配置文件

server.port=9000spring.application.name=eureka.client eureka.instance.appname=eureka.client.01 #,该名字将注册到eureka注册中心,在发现服务时discoveryClient.getServices()获取的是这个名称,底层代码对其转大写,也就是页面上显示的服务eureka.client.serviceUrl.defaultZone=http://eureka-server-peer1:8761/eureka/,http://eureka-server-peer2:8762/eureka/,http://eureka-server-peer3:8763/eureka/

注:
1.spring.application.name=eureka.client.01 ,应用起个名字,在发现服务时,discoveryClient.getInstances(s)方法匹配的是这个名字,所以如果和eureka.instance.appname这个名称就不一致就无法匹配
2.eureka.instance.appname=eureka.client.01 该名字将注册到eureka注册中心,在发现服务时discoveryClient.getServices()获取的是这个名称,底层代码对其转大写,也就是页面上显示的application
3.建议将两个名称起一样的,这样就可以在发现服务并调用服务时比较方便,当然,实际开发中我们调用服务都是知道对方spring.application.name,直接通过discoveryClient.getInstances(s)就可以调用,无需通过discoveryClient.getServices()获取所有注册的服务。

访问注册中心

访问地址:http://localhost:8761/
访问地址:http://localhost:8762/
访问地址:http://localhost:8763/
这里写图片描述
这里写图片描述
这里写图片描述
从上面可以看到服务已经注册到注册中心,下面我们发现服务。

原创粉丝点击