Eureka概念以及初步搭建(一)

在这里插入图片描述

传统的服务架构

不管是采用http还是nginx还是host都需要将服务端或者客服端的ip保存起来通过IP去关联,如果IP变化
或者增加服务模块传统HTTP就会在写死的对应调用端去修改其和增加IP。nginx则不管是关联服务端或者是客户端都需要将新增和修改的IP进行更新。
在这里插入图片描述

Eureka

在这里插入图片描述
当客户端也就是模块注册到Eureka上就会彼此感知存在(通过客户名具体IP等信息由server端进行解析)就可以通过服务名进行调用,每个Client都会从server当中获取到其他Client的信息

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

新建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.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cx</groupId>
    <artifactId>spring-cloud-eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-eureka</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR6</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-server</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

spring:
  application:
    name: service-register-center 
  mandatory-file-encoding: UTF-8
  http:
    encoding:
      enabled: true
      charset: UTF-8

logging: #日志文件
  level:
    root: info
  file:
    max-size: 15MB
    path: service-register-center-log
  pattern:
    dateformat: yyyy-MM-dd HH:mm:ss.SSS
server:
  port: 10099
  undertow:
    accesslog:
      enabled: true
      dir: undertow-access-logs
      pattern: common
      prefix: service_register_center_access_log.
      suffix: log

management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    shutdown:
      enabled: true

eureka:
  instance:
    hostname: localhost 
  ## 表示为Erureka客户端
  client:
    ## 单台server为服务器不需要注册到服务器上
    register-with-eureka: false
    ## 获取注册列表的信息  道理同上 基本两者参数是一样的
    fetch-registry: false
    service-url: # Eureka服务提供地址
      defaultZone: http://localhost:10099/eureka/

在springboot启动器上加上@EnableEurekaServer 注解标识为服务器

@SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaApplication {

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

}

新建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 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.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cx</groupId>
    <artifactId>spring-cloud-eureka-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-eureka-client</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <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-web</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

server:
  port: 9090

spring:
  application:
    name: eureka-client

eureka:
  ## 表示为Erureka客户端
  client:
    ## 单台server为服务器不需要注册到服务器上
    register-with-eureka: true
    ## 获取注册列表的信息  道理同上 基本两者参数是一样的
    fetch-registry: true
    service-url: # Eureka服务提供地址
      defaultZone: http://localhost:10099/eureka/
student:
  information:
    address: shanghai
    name: zhangsan

zai springBoot启动器上标识Client为客户端@EnableEurekaClient

@SpringBootApplication
@EnableEurekaClient
public class SpringCloudEurekaClientApplication {

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

}

依次启动server 和client 访问
http://localhost:10099 如图
在这里插入图片描述

测试使用

在client中编写Controller

@RestController
public class Mycontroller {

    @Value("${student.information.address}")
    private String address;

    @Value("${student.information.name}")
    private String name;

    @GetMapping("info")
    public String info() {
        String result = "welcome:" + this.address + ",your address is " + this.address;
        return result;
    }
}

访问http://localhost:9090/info
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值