Spring Boot(2)加强学习Eureka

1.1 Eureka的职责

	  Eureka职责
	  - 服务注册:服务提供方将服务注册到注册中心
	  -  服务发现:服务调用方法,从注册中心中,获得需要的服务
	  - 服务检测:注册中心与服务之间采用心跳检测服务状态

1.2 Eureka案例

在这里插入图片描述

2.1 搭建父项目

 -  步骤一:创建项目 cloud_parent(略)
 -  步骤二:修改pom.xml文件,确定spring cloud版本
 <!--1 确定spring boot的版本-->
     <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>

    <!--2  确定版本-->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <spring-cloud-release.version>Greenwich.RELEASE</spring-cloud-release.version>
    </properties>

    <!-- 3 锁定sprig cloud版本-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud-release.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- 4 确定spring cloud私有仓库-->
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

2.2 搭建注册中心

- 步骤一:创建项目 eureka_demo (略)
- 步骤二:修改pom.xml文件,添加web和 eureka  service 依赖
 <dependencies>
        <!--web起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Eureka服务端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
步骤三:创建yml文件,配置端口号、服务名、eureka注册地址
#服务端口号
server:
  port: 10086
#服务名称
spring:
  application:
    name: eureka_demo
#注册中心地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:${server.port}/eureka   #eureka服务注册地址
    register-with-eureka: false     #关闭将自己注册到注册中心中
    fetch-registry: false           #关闭从注册中心获得列表(不拉去其他服务信息)
步骤四:创建启动类,添加开启 eureka service 注@EnableEurekaService
package com.czxy;

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

/**
 * Created by cjc
 */
@SpringBootApplication
@EnableEurekaServer       //开启eureka服务端
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class,args);
    }
}

2.3 搭建服务提供方

- 步骤一:创建提供方项目,eureka_service
- 步骤二:修改pom.xml文件,添加eureka client依赖
<dependencies>
        <!--web起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Eureka客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--spring boot监控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
步骤三:创建application.yml文件,配置端口号、服务名、eureka注册中心位置
#端口号
server:
  port: 8080
#服务名称
spring:
  application:
    name: service
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
步骤四:编写启动类,添加启动客户端注解 @EnableEurekaClient
package com.czxy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
 * Created by cjc
 */
@SpringBootApplication
@EnableEurekaClient
public class ServicApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServicApplication.class,args);
    }
}
步骤五:测试程序
测试路径:

http://localhost:8080/test

package com.czxy.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by cjc
 */
@RestController
@RequestMapping("/test")
public class ServicController {
    @GetMapping
    public ResponseEntity<String> test(){
        return ResponseEntity.ok("测试数据");
    }
}

3.1 搭建 服务调用方

- 步骤一:创建调用方项目,eureka_client
- 步骤二:修改pom.xml文件,添加 web、eureka client、spring boot 监控依赖
<dependencies>
        <!--web起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Eureka客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--spring boot监控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
步骤三:创建yml文件
#端口号
server:
  port: 9090
#服务名称
spring:
  application:
    name: client
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
步骤四:启动类,添加eureka客户端注解
package com.czxy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
 * Created by cjc
 */
@SpringBootApplication
@EnableEurekaClient          //开启eureka客户端
public class ClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class,args);
    }
}

3.2 调用方测试数据

在这里插入图片描述

步骤一:编写config配置类,用于配置RestTemplate实例
package com.czxy.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
 * Created by cjc
 */
@Configuration
public class HttpConfig {
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}
步骤二:编写DataDao,用于进行远程调用
package com.czxy.client;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
/**
 * Created by cjc
 */
@Component
public class TestClient {
    @Resource
    private RestTemplate restTemplate;

    public ResponseEntity<String> test() {
        return restTemplate.getForEntity("http://localhost:8080/test",String.class);
    }
}
步骤三:编写TestController,提供接口进行访问
package com.czxy.controller;

import com.czxy.client.TestClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created by cjc
 */
@RestController
@RequestMapping("/test")
public class ClientController {
    @Resource
    private TestClient testC;
    @GetMapping
    public ResponseEntity<String> test(){
      return  userClient.test();
    }
}

测试路径
http://localhost:9090/test

4.1 配置eureka instance

在这里插入图片描述

	yml文件配置
	- instance-id : 用于配置可视化页面中,显示的服务名称
	-  - 默认服务名称:计算机名称:服务名:端口号
	-  - 自定义服务名称:
	-  - ${spring.application.name}  获得服务名
	-     - ${spring.cloud.client.ip-address} 获得ip地址
	-     - ${server.port} 获得端口号
	- - prefer-ip-address:用于配置可视化页面中,访问时是否显示ip地址
	-   - 默认显示的是:计算机名称:端口号/
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
  instance:
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true   #注册中心可视化中显示IP地址

properties文件配置(不建议)

eureka.client.service-url.defaultZone=http://localhost:10086/eureka
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
eureka.instance.prefer-ip-address=true
发布了31 篇原创文章 · 获赞 11 · 访问量 836

猜你喜欢

转载自blog.csdn.net/Eros1onz/article/details/103423627