SpringCloud学习笔记(二)Eureka服务注册

上节学习了Eureka进行服务治理未写完,这次将服务进行注册到Eureka中,在上次的架子上继续前进。

yyc-registry 注册中心详见:SpringCloud学习笔记-Eureka服务治理

首先新建一个yyc-test模块,进行服务提供,使注册到Eureka的注册中心,

在pom文件中我们引入yyc父类,并引入web模块

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.zhaixingzu</groupId>
        <artifactId>yyc</artifactId>
        <version>1.1.0-SNAPSHOT</version>
    </parent>
    <artifactId>yyc-test</artifactId>
    <packaging>jar</packaging>
    <name>yyc-test</name>
    <description>test</description>

    <dependencies>
        <!--web 模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <!--排除tomcat依赖-->
                <exclusion>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--undertow容器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
    </dependencies>

</project>

记得,在父类yyc的pom中引入依赖:

扫描二维码关注公众号,回复: 9982205 查看本文章
 <!--eureka 客户端-->
 <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
 </dependency>

如果使用监控,可以引入:

<!--监控-->
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--监控客户端-->
<dependency>
     <groupId>de.codecentric</groupId>
     <artifactId>spring-boot-admin-starter-client</artifactId>
     <version>${spring-boot-admin.version}</version>
</dependency>

在yyc-test的resources的bootstrap.yml文件中写入配置信息

server:
  port: 8800

spring:
  application:
    name: yyc-test

eureka:
  client:
      serviceUrl:
            defaultZone: http://admin:[email protected]:9900/eureka/  #设置与eureka交互的地址

在yyc-test的TestApplication启动类中

package com.zhaixingzu.yyc.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 *
 * @author Herbert
 * @date 2019年06月19日
 */
@EnableEurekaClient
@SpringBootApplication
public class TestApplication {

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

}

yyc-test中具体结构

好了,现在先启动注册中心,在需要注册的服务

以此为:

yyc-registry    ------>  yyc-test

成功访问页面:

注意事项:

1:Springboot的启动类上添加@EnableEurekaServer或者@EnableDiscoveryClient,这两个注解的注解的区别主要是:

@EnableEurekaServer是基于 spring-cloud-netflix依赖,只能为eureka作用,是专门给Eureka用的 

@EnableDiscoveryClient是基于 spring-cloud-commons依赖,并且在classpath中实现,是给比如zookeeper、consul使用的, 

旧版本的@EnableEurekaServer的源码上面也是也是有@EnableDiscoveryClient注解的。 

2:defaultZone配置eureka的地址,这个如果有多个注册中心,则用逗号隔开。 

3:为了服务注册中心的安全考虑,很多时候我们都会为服务注册中心加入安全认证。通常与SpringSecurity整合,主要是SpringSecurity的内容

欢迎关注摘星族

发布了56 篇原创文章 · 获赞 22 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/weixin_41986096/article/details/93519480