Java 杂谈SpringBootJava技术分享

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

2019-06-24  本文已影响8人  墨迹嘿嘿
首发.png

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

image

**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中引入依赖:

<?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>

    <groupId>com.zhaixingzu</groupId>
    <artifactId>yyc</artifactId>
    <version>1.1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>yyc</name>
    <description>yyc-pom</description>

    <modules>
        <module>yyc-registry</module>
        <module>yyc-test</module>
    </modules>

    <!-- 集中定义版本号 -->
    <properties>
        <spring-boot.version>2.1.3.RELEASE</spring-boot.version>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
        <spring-boot-admin.version>2.1.3</spring-boot-admin.version>
    </properties>

    <dependencies>

        <!--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-test</artifactId>
            <scope>test</scope>
        </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>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <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>

</project>

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

   <!--监控-->
        <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:admin@127.0.0.1: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中具体结构

image

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

以此为:

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

成功访问页面:

image

注意事项:

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的内容

欢迎关注摘星族:


摘星族.jpg
上一篇 下一篇

猜你喜欢

热点阅读