Spring Cloud Netflix(Eureka)配置与基本说明

Eureka Server

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.4.RELEASE</version>
		<relativePath/><!-- lookup parent from repository -->
	</parent>
	<groupId>com.teachayu</groupId>
	<artifactId>netfix-eureka</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>netfix-eureka</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
	</properties>
	<dependencies>
		<!-- eureka server  -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>
		<!-- test  -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</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>
</project>

yml配置

server:
  port: 8081   #springboot默认启动8080端口
#eureka默认是8761 演示修改了8081 端口
eureka:
  instance:
    # 默认${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}}}
    instance-id: ${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}}}
    #: 例子 somnus.lan:eureka-client:8080
  client:
    #Indicates whether or not this instance should register its information with eureka server for discovery by others.
    #In some cases, you do not want your instances to be discovered whereas you just want do discover other instances.
    register-with-eureka: true #默认: true: 自己也注册到eureka service
    fetch-registry: false #Indicates whether this client should fetch eureka registry information from eureka server.
    #fetch-registry=true 客户端使用,默认为true,
    #如果该项目是eureka server且fetch-registry=true,启动会报一次错误,因该项目没有启动起来之前,进行了一次fecth
    #如果是不是默认端口,需要配置service-url-> defaultZone,否则fecth拉取信息一直报错,因拉取地址默认localhost:8761
    #(com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect)
    service-url:
      #"http://localhost:8761" + DEFAULT_PREFIX + "/"
      defaultZone: http://localhost:8081/eureka/  #如果端口为8761且是本机,可以不用填写该配置

程序启动

package com.teachayu.netfixeureka;

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

@SpringBootApplication
@EnableEurekaServer
public class NetfixEurekaApplication {

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

}

访问eureka server 页面

启动程序之后

http://localhost:8081/

eureka启动页面显示信息Application 显示为UNKNOWN应为没有配置spring.application.nam
UP (1)eureka-server:8081 UP:表示状态,以及实例ID:配置属性eureka.instance.instance-id

eureka-server默认开启了actuator ,详细参看

https://blog.csdn.net/somnus252/article/details/104410676

Eureka Client

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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.example</groupId>
    <artifactId>eureka-client</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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>

</project>

yml配置

info:
  name: eureka_client
server:
  port: 8080   #springboot默认启动8080端口
#eureka默认是8761
eureka:
  client:
    #Indicates whether or not this instance should register its information with eureka server for discovery by others.
    #In some cases, you do not want your instances to be discovered whereas you just want do discover other instances.
    register-with-eureka: true
    fetch-registry: true #Indicates whether this client should fetch eureka registry information from eureka server.
    service-url:
      #"http://localhost:8761" + DEFAULT_PREFIX + "/"
      defaultZone: http://localhost:8081/eureka/   #如果端口为8761且是本机,可以不用填写该配置
spring:
  application:
    name: eureka-client  # 不配置,将在 Instances currently registered with Eureka ->   Applicaiton显示为UNKNOWN

程序启动

启动程序之前需要保证已经启动eureka-server 并且配置了eureka.client.service-url

package com.teachayu.netfixeureka;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaClientApplication {
	@Autowired
	private EurekaClient discoveryClient;
	public static void main(String[] args) {
		//new SpringApplicationBuilder(EurekaClientApplication.class).web(WebApplicationType.SERVLET).run(args);
		SpringApplication.run(EurekaClientApplication.class, args);
	}

	/**
	 * 显示如何使用EurekaClient
	 * 返回信息:http://somnus.lan:8080/------192.168.67.54
	 * @return
	 */
	@RequestMapping("discoveryClient")
	public String client(){
		//参考文档
		//https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.2.1.RELEASE/reference/html/#using-the-eurekaclient
		InstanceInfo instance = discoveryClient.getNextServerFromEureka("eureka-client", false);
		return instance.getHomePageUrl() +"------"+ instance.getIPAddr();
	}
}

访问eureka-server页面 查看客户端是否已经注册

http://localhost:8081

eureka-client已经注册注意 UP (1) somnus.lan:eureka-client:8080 点击报错,因eureka-client未引入spring-boot-starter-actuator 包

测试

访问的地址:显示

http://localhost:8080/discoveryClient
在这里插入图片描述

标题参考文档来源官网

https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.2.1.RELEASE/reference/html/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值