首页 > 其他分享 >【Spring】SpringCloudの環境構築(restTemplate+ribbon)

【Spring】SpringCloudの環境構築(restTemplate+ribbon)

时间:2023-11-22 17:12:30浏览次数:30  
标签:spring 構築 restTemplate springframework eureka Spring org import cloud

参考URL:<https://zhuanlan.zhihu.com/p/272663162?utm_id=0>

■紹介

 SpringCloudの初心者に向け、簡単な手順を作成する。Eurekaサーバを利用して、「server」を立って。それにして、

提供者(provider)と消費者(consumer)を全て「server」に導入して、管理する。消費者(consumer)の内部に、ribbonの技術を利用して、

提供者(provider)を呼びます。三つのプロジェクトを別々に構築できるし、「server」で管理できる。

■構造

 

 ■説明

・spring-cloud-eureka-server:子項目(Module)、Eurekaのサービスを提供する

・spring-cloud-service-provider:子項目(Module)、Eurekaに導入し、提供者としてサービスを提供

・spring-cloud-client-ribbon:子項目(Module)、Eurekaに導入し、消費者として、内部で提供者に呼ぶ

 

■「POM.xml」の配置

・spring-cloud-eureka-server

<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>org.example</groupId>
  <artifactId>spring-cloud-eureka-server</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>spring-cloud-server</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <!-- SpringBoot1.5.x -->
    <spring.cloud.dependencies.version>Edgware.SR5</spring.cloud.dependencies.version>
  </properties>

  <!-- 引入SpringBoot-Parent项目 -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
  </parent>

  <dependencies>
    <!-- Cloud Server依赖 -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
  </dependencies>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring.cloud.dependencies.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

</project>
pom.xml

・spring-cloud-service-provider

<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>org.example</groupId>
  <artifactId>spring-cloud-service-provider</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>spring-cloud-service</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <spring.cloud.dependencies.version>Edgware.SR5</spring.cloud.dependencies.version>
  </properties>

  <!-- 引入SpringBoot-Parent项目 -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
  </parent>

  <dependencies>
    <!-- Cloud Client依赖 -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
  </dependencies>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring.cloud.dependencies.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>
pom.xml

・spring-cloud-client-ribbon

<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>org.example</groupId>
  <artifactId>spring-cloud-client-consumer</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>spring-cloud-client-ribbon</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <spring.cloud.dependencies.version>Edgware.SR5</spring.cloud.dependencies.version>
  </properties>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
  </parent>

  <dependencies>
    <!-- Cloud Client依赖 -->
    <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-ribbon</artifactId>
    </dependency>
  </dependencies>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring.cloud.dependencies.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>
pom.xml

 

■「application.xml」の配置

・spring-cloud-eureka-server

server:
  port: 8700

eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
#如果需要开通多个端口时,使用下面方式
#      defaultZone: http://localhost:8699/eureka,http://localhost:8698/eureka
spring:
  application:
    name: eureka-server
application.xml

・spring-cloud-service-provider

server:
  port: 8701

eureka:
  instance:
    hostname: localhost

  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:8700/eureka

spring:
  application:
    name: eureka-provider
application.xml

・spring-cloud-client-ribbon

server:
  port: 8702

eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:8700/eureka

spring:
  application:
    name: eureka-consumer

eureka-provider:
  ribbon:
    ActiveConnectionsLimit: 1000
application.xml

 

■「XXXApplication.java」の配置

・spring-cloud-eureka-server

package org.example;

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

@SpringBootApplication
@EnableEurekaServer
public class CloudServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(CloudServerApplication.class, args);
    }
}
CloudServerApplication.java

・spring-cloud-service-provider

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class CloudServiceProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(CloudServiceProviderApplication.class,args);
    }
}
CloudServiceProviderApplication.java

・spring-cloud-client-ribbon

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class CloudClientRibbonApplication {
    public static void main(String[] args) {
        SpringApplication.run(CloudClientRibbonApplication.class,args);
    }
}
CloudClientRibbonApplication.java

 

■「controller」の作成

・spring-cloud-service-provider(提供者側)

package org.example.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/provider")
public class ProviderController {

    @RequestMapping("/hello")
    public String helloWorld(String str){
        System.out.println("str:"+str);
        return "parameter:"+str;
    }
}
ProviderController.java

 ・spring-cloud-client-ribbon(消費者側)

package org.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/hello")
    public String helloWorld(@RequestParam String str){
        System.out.println("str:"+str);
        String forObject = "";
        try{
            //第一种(需要bean注入,通过用Service名调用)
            forObject = restTemplate.getForObject("http://eureka-provider/provider/hello?str="+str,String.class);

            //第二种(只能通过IP+端口调用)
            //forObject = new RestTemplate().getForObject("http://localhost:8701/provider/hello?str="+str,String.class);

            //第三种(通过解析Service名转换IP+端口调用)
            //ServiceInstance serviceInstance =loadBalancerClient.choose("eureka-provider");
            //forObject = new RestTemplate().getForObject("http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/provider/hello?str="+str, String.class);

        }catch (Exception ex){
            throw ex;
        }
        return forObject;
    }
}
ConsumerController.java
package org.example.beans;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class Beans {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}
Beans.java

 

■入口

S1:Eurekaのサーバを確認する(http://localhost:8700) 

S2:提供者(provider)のサーバを確認する(http://localhost:8701/provider/hello?str=こんにちは)

S3:消費者(consumer)を確認する(http://localhost:8702/consumer/hello?str=天気予報)

 

ここまで終わり

标签:spring,構築,restTemplate,springframework,eureka,Spring,org,import,cloud
From: https://www.cnblogs.com/lnsylt/p/17849675.html

相关文章

  • Spring Authorization Server
    SpringAuthorizationServer是SpringSecurityOAuth的进化版本,SpringSecurityOAuth官方已经宣布“EndofLife”了。SpringSecurityOAuth使用的是OAuth2.0标准而SpringAuthorizationServe引入了对OAuth2.1和OpenIDConnect1.0规范的支持,并提供了更多功能和改进。它提供......
  • 【Spring】SpringCloudの環境構築
    ■説明SpringCloudの初心者に向け、詳しくの配置流れを紹介します。 ■ライブラリ・OracleのJDK:17・SpringCloud:2021.0.8・SpringBoot:2.7.17・MySql:8.0.33・Druid:1.2.20・MyBatis:2.3.1・LogbackCore:1.3.11・Lombok:1.18.22・Log4j:1.2.17・Junit:5.9.1・Gradle:8 ......
  • Spring5学习随笔-基础注解编程
    学习视频:【孙哥说Spring5:从设计模式到基本应用到应用级底层分析,一次深入浅出的Spring全探索。学不会Spring?只因你未遇见孙哥】注解编程-第一章、注解基础概念1.什么是注解编程指的是在类或方法上加入特定的注解(@XXX),完成特定功能的开发.2.为什么要讲解注解编程注解开发......
  • Spring Framework 6.1正式版发布
    主要特性:支持JDK21LTS支持虚拟线程,tomcat一键开启虚拟线程支持恢复JVMCheckpoint引入「资源生命周期管理」引入「数据绑定和验证」新增RestClient和JdbcClientAPI,链式渐近式api,更优雅丝滑原文:https://github.com/spring-projects/spring-framework/wiki/What'......
  • springboot去除内嵌tomcat
    springboot去除内嵌tomcat步骤在pom文件中加入以下代码点击查看代码<!--多模块排除内置tomcat--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> ......
  • SpringCloud项目使用nacos配置
    SpringCloud项目pom.xml注意SpringCloud和SpringCloudAlibaba的版本对应。SpringCloudAlibabaVersionSpringCloudVersionSpringBootVersion2022.0.0.0*SpringCloud2022.0.03.0.22022.0.0.0-RC2SpringCloud2022.0.03.0.22022.0.0.0-RC1Spring......
  • SpringBoot + Vue实现分页查询
    后端在controller层修改SpringBoot自带分页查询方法,只需要修改关键代码就可以@GetMapping("/findAll/{page}/{size}")//获取url输入的页码publicPage<Users>findAll(@PathVariable("page")intpage,@PathVariable("size")intsize){//将页码取到方法内P......
  • 基于springboot的校园失物招领系统-计算机毕业设计源码+LW文档
    校园失物招领系统介绍在现代大学校园中,失物招领系统是一个至关重要的组成部分,旨在为学生、教职员工和访客提供便捷的失物招领服务。本文将介绍一个基于SpringBoot的校园失物招领系统,该系统结合了现代技术和用户友好的界面,提供了高效、安全和快速的失物招领流程。系统架构该系统采......
  • 基于Springboot教学管理系统-计算机毕业设计源码+LW文档
    摘 要传统办法管理信息首先需要花费的时间比较多,其次数据出错率比较高,而且对错误的数据进行更改也比较困难,最后,检索数据费事费力。因此,在计算机上安装教学管理系统软件来发挥其高效地信息处理的作用,可以规范信息管理流程,让管理工作可以系统化和程序化,同时,教学管理系统的有效运用......
  • SpringBoot使用RedisTemplate
    SpringBoot使用RedisTemplate目录1.Redis五种基础数据结构2.SpringBoot连接Redis1引入依赖2配置redis连接3编写测试类3.详解RedisTemplate的API1常用数据操作2.几种数据结构操作的具体用法1.Redis五种基础数据结构参考链接:Redis入门-数据类型:5种基础数据类......