• SpringCloud IDEA 教学 (三) Eureka Client


    写在前头

    本篇继续介绍基于Eureka的SpringCloud微服务搭建,回顾一下搭建过程,

    第一步:建立一个服务注册中心; 

    第二步:建立微服务并注入到注册中心; 

    第三步:建立client端来访问微服务。

    正文开始

    使用IDEA创建Client项目,重复创建项目过程此处不赘述,详情参见第一章

    Web用于提供@RestController注解功能,该功能提供了Restful接口定义功能

    Eureka Discovery用于提供@EnableDiscoveryClient注解功能,该功能提供了Eureka Client端的定义

    Ribbon用于提供RestTemplate+Ribbon功能,该功能提供了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.cloud</groupId>
        <artifactId>client</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>client</name>
        <description>Demo project for Spring Cloud Client</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.3.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>
            <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.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </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>

     修改项目

     1 修改配置文件,内容如下

    #将client端注册到服务注册中心
    eureka: client: service
    -url: default-zone: http://localhost:8761/eureka server: port: 8003 spring: application: name: cloud-client1

    2Application 增加注解@EnableDiscoveryClient用于声明该项目为Eureka Client端

    3Application 增加注解@Configuration 和下述代码,这部分用于配置Client调用服务端时,使服务端采用负载均衡策略。

       @Bean
        @LoadBalanced
        RestTemplate getRestTemplate() {
            return new RestTemplate();
        }

    最终Application

    @SpringBootApplication
    @EnableDiscoveryClient
    @Configuration
    public class ClientApplication {

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

    @Bean
    @LoadBalanced
    RestTemplate getRestTemplate() {
    return new RestTemplate();
    }

    }

    增加Controller,此处@Autowired引用了Application中注入的RestTemplate Bean,

    restTemplate提供了getForObject方法用于调用微服务接口

    @RestController
    public class CloudClientController {
    
      @Autowired
      private RestTemplate restTemplate;
    
      @RequestMapping(value = "/invokeService", method = RequestMethod.GET)public String invokeService(@RequestParam(value = "name") String name) {
        System.out.println("入参name:" + name);
        String json = restTemplate.getForObject("http://CLOUD-SERVICE/getServiceNameAndPort?name=" + name, String.class);
        System.out.println(json);
        return json;
      }
    }

    综上,client端已经搭建完毕。

    启动截图~~~~~

     

    下面测试接口 

    接口请求 http://localhost:8003/invokeService?name=tianmh 

    结果交替出现,代表负载均衡启动成功,

      Hello tianmh servicePort 8001

      Hello tianmh servicePort 8002

    负载均衡使用的两个服务器分别为端口8001和8002的cloud-service服务

    写在最后

    至此,Eureka框架搭建完毕,接下来章节介绍SpringCloud的其他特性

      

    以上,亲测。

    欢迎在评论区指正,

    如果感觉本教程对您有所帮助,希望可以为笔者打Call

  • 相关阅读:
    可图性判定HavelHakimi定理
    并查集入门
    js数组和函数应用
    DOM用法及应用
    javascript基础知识
    表单
    PHP变量
    30天自制操作系统开发笔记——IDT中断描述符表
    《30天自制操作系统》学习笔记——汇编程序磁盘BIOS调用
    汇编指令: LGDT、LIDT、LLDT、LMSW、LOADALL、LOADALL286、LOCK、LODSB、LODSW、LODSD
  • 原文地址:https://www.cnblogs.com/tianmh/p/9198130.html
Copyright © 2020-2023  润新知