(springCloud -1 服务注册与发现)springboot配置eureka与springboot的注册

一:springboot配置eureka

第一步:
在https://start.spring.io/下载一个springboot的项目,并在pom文件里添加eureka 的相关依赖;
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.eureka</groupId>
	<artifactId>zt-frank-eureka</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>zt-frank-eureka6001-service</name>
	<description>Demo project for Spring Boot</description>
	
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RC2</spring-cloud.version>
    </properties>
    <dependencies>
        <!-- 引入的Eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</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-autoconfigure</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>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

第二步:
yml文件配置:

server:
  port: 6001 #服务注册中心端口号
eureka:
  instance:
    hostname: 127.0.0.1 #服务注册中心IP地址
  client:
    register-with-eureka:: false #是否向服务注册中心注册自己
    fetch-registry: false #是否检索服务
    service-url: #服务注册中心的配置内容,指定服务注册中心的位置
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

第三步:
程序入口上加入 @EnableEurekaServer 注解

/**
 * 启动一个服务注册中心
 */
@EnableEurekaServer
@SpringBootApplication
public class Application {

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

}
第四步:
启动项目,启动成功后访问 http://localhost:6001/(这个地址即yml文件 http:// e u r e k a . i n s t a n c e . h o s t n a m e : {eureka.instance.hostname}: eureka.instance.hostname:{server.port}/eureka/ 的地址)
在这里插入图片描述

二:springboot项目的注册

第一步:
在https://start.spring.io/上下载springboot项目,并添加springcloud 和 eureka的相关依赖

<!--eureka -->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
	<version>2.0.0.RELEASE</version>
</dependency>

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-config-server</artifactId>
	<version>2.0.2.RELEASE</version>
</dependency>

第二部:
配置yml文件,将eureka 的注册地址配置上(注意:这里配置的地址要与eureka 保持一致 即:http:// e u r e k a . i n s t a n c e . h o s t n a m e : {eureka.instance.hostname}: eureka.instance.hostname:{server.port}/eureka/)

#eureka注册中心的地址
eureka:
  client:
   service-url:
     defaultZone: http://localhost:6001/eureka/

第三步
在程序入口上添加 注解 @EnableEurekaClient

	
@SpringBootApplication
@EnableEurekaClient
public class ActivtiApplication {
	
	public static ConfigurableApplicationContext applicationContext = null;

	public static void main(String[] args) {
		applicationContext = SpringApplication.run(ActivtiApplication.class, args);
		System.out.println("*========启动成功========*");
	}

}

第五步:
启动服务浏览器输入http://localhost:6001
在这里插入图片描述
其他:
到这里基本的配置就结束了,接下来就是一些配置的完善 如图:
在这里插入图片描述

eureka的自我保护机制是什么?
eureka的自我保护机制是对异常网络场景的一种解决措施,当网络发生异常,但是服务健康的情况下,eureka 不会盲目的删除已经注册进来的服务,这样微服务的集群就会更加的健壮。

如何取消自我保护机制(不建议)
在eureka端的yml文件配置 如图:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值