springcloud搭建服务注册中心与服务发现

1、创建服务注册中心

创建一个普通的Spring Boot工程

首先我们需要创建一个普通的Spring Boot工程,命名为eureka-server,普通到什么程度呢?就是一个starter都不需要添加,创建成功之后就只引用了一个父starter。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
    <relativePath/>
</parent>

添加Eureka依赖

工程创建成功之后,向pom.xml文件中添加eureka-server的依赖,目前eureka的稳定版本是Dalston.SR3,添加完依赖之后,pom.xml文件如下所示:

<dependency>    
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR3</version><!-- eureka版本 -->
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

配置服务注册中心

最后我们再做一点简单的配置就可以了,配置就写在Spring Boot的配置文件application.properties中,写法如下:

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/

或者application.yml

server:
  port: 1111 #服务端口
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false #是否将eureka自身作为应用注册到eureka注册中心
    fetch-registry: false #为true时,可以启动,但报异常:Cannot execute request on any known server
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

OK,那么关于这几行注释,我说如下几点:

1.server.port=1111表示设置该服务注册中心的端口号 

2.eureka.instance.hostname=localhost表示设置该服务注册中心的hostname 

3.eureka.client.register-with-eureka=false,由于我们目前创建的应用是一个服务注册中心,而不是普通的应用。默认情况下,这个应用会向注册中心(也是它自己)注册它自己,设置为false表示禁止这种默认行为 

4.eureka.client.fetch-registry=false,表示不去检索其他的服务,因为服务注册中心本身的职责就是维护服务实例,它也不需要去检索其他服务

服务注册中心启动类

启动一个服务注册中心的方式很简单,就是在Spring Boot的入口类上添加一个@EnableEurekaServer注解,不加这个注解访问会报404,如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

启动测试

OK,做完这一切之后,我们就可以启动这一个Spring Boot 服务,服务启动成功之后,在浏览器中输入:http://localhost:1111

DS Replicas

Instances currently registered with Eureka

Application AMIs Availability Zones Status
No instances available

2、注册服务提供者

OK,那么现在服务注册中心有了之后,我们可以考虑向这个服务注册中心注册一个服务提供者了。

创建一个新的Spring Boot工程

还是创建一个Spring Boot工程,这次创建比之前创建多一个步骤,在创建的时候选中web的starter,我们来创建一个web工程。(本案例在blogdo基础上带动)

添加Eureka依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependencyManagement>
    <dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

配置服务名称和注册中心地址

最后的最后,我们在application-dev.yml文件中配置一下服务名和注册中心地址即可,如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1111/eureka
spring:
  application:
    name: blogdo

创建应用的入口,添加注解@EnableEurekaClient

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class BlogdoApplication {
    public static void main(String[] args) {
        SpringApplication.run(BlogdoApplication.class, args);
        System.out.println("ヾ(◍°∇°◍)ノ゙    blogdo启动成功      ヾ(◍°∇°◍)ノ゙\n");
    }
}

测试

做完这一切之后,我们就可以来测试了,直接运行这个Spring Boot工程,运行成功之后,我们刷新刚才的http://localhost:1111,就可以看到有一个服务已经注册成功了。

DS Replicas

Instances currently registered with Eureka

Application AMIs Availability Zones Status
BLOGDO n/a (1) (1) UP (1) - SZVY2AWX5511361.china.huawei.com:blogdo:8083
 

猜你喜欢

转载自www.cnblogs.com/xyhero/p/35853ba394be11e1417af443534caae7.html