SpringCloud(二)服务治理

时间:2020-03-23
本文章向大家介绍SpringCloud(二)服务治理,主要包括SpringCloud(二)服务治理使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

上一篇文章,已经解释了各个专有名词,并且确定了后面会使用的框架:eureka、feign、hystrix、gateway,现在开始搭建完整的服务器。

SpringBoot与SpringCloud的版本号分别是: 2.1.13.RELEASE、Greenwich.SR5

SpringBoot与SpringCloud版本号是相互对应的,不了解的话,不要随意搭配,最新的SpringBoot已经是2.2,但是之前刚刚升级的2.1的SpringBoot,不准备再去折腾。

EurekaServer

就像Spring需要一个Bean的容器,管理全部的代码,显然,Cloud也需要一个注册中心,管理住全部的服务。

完整的创建流程如下:

 代码:

package cn.seaboot.server;

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

@EnableEurekaServer
@SpringBootApplication
public class ServerApplication {

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

Yml配置:

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Maven依赖:

<?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.1.13.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.seaboot</groupId>
    <artifactId>server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR5</spring-cloud.version>
    </properties>

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

这样就创建了一个具备最基础功能的注册中心。访问:http://localhost:8761/ ;即可查看到Eureka的管理界面。

EurekaClient

创建过程与Server相同

修改Maven依赖:

        <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>

修改主函数的注解:

package cn.seaboot.client;

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

@EnableEurekaClient
@SpringBootApplication
public class ClientApplication {

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

Yml配置:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8762
spring:
  application:
    name: client-a

写两个接口用于测试,常规代码:

package cn.seaboot.client.ctrl;

import cn.seaboot.client.service.HelloService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Mr.css
 * @date 2020-03-05
 */
@RestController
public class HelloController {

  @Resource
  HelloService helloService;

  @GetMapping(value = "/hi")
  public String hi(@RequestParam String name) {
    return helloService.hiService( name );
  }

  @GetMapping(value = "/hello")
  public String sayHello(@RequestParam String name) {
    return helloService.sayHello(name);
  }
}


package cn.seaboot.client.service;

import org.springframework.stereotype.Service;

/**
 * @author Mr.css
 * @date 2020-03-05
 */
@Service
public class HelloService {

  public String hiService(String name) {
    return "参数是:" + name;
  }

  public String sayHello(String name) {
    try {
      Thread.sleep(5000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return "say hello";
  }
}

测试接口:

管理页面:http://localhost:8761/
接口测试:http://localhost:8762/hello?name=1

原文地址:https://www.cnblogs.com/chenss15060100790/p/12554784.html