网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

SpringCloud常见模块有哪些

这篇文章主要讲解了“Spring Cloud常见模块有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring Cloud常见模块有哪些”吧!

创新互联专注于高唐企业网站建设,响应式网站建设,商城网站建设。高唐网站建设公司,为高唐等地区提供建站服务。全流程按需网站策划,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务

什么是Spring Cloud :  

  • Spring Cloud 是一系列框架的集合

  • 利用Spring Boot的简化了开发

Spring Cloud 常见模块 :

  • Eureka:注册中心,用于注册所有服务(项目/应用)

  • Ribbon:负载均衡,用于搭建集群的。(同一个功能多个tomcat,ribbon帮着选择一个tomcat)

  • Hystrix:熔断器,与正主断了联系,使用备选方案(备胎)。

  • Feign:服务与服务之间调用。类似HttpClient

  • zuul 网关:确定统一入口,方便进行管理。

Spring Cloud常见模块有哪些

spring cloud版本 : spring cloud 采用 Greenwich版本,对应spring boot 2.1.*版本

Eureka 入门

Eureka职责:

  • 服务注册:服务提供方将服务注册到注册中心

  • 服务发现:服务调用方法,从注册中心中,获得需要的服务

  • 服务检测:注册中心与服务之间采用心跳检测服务状态

Eureka 入门案例

搭建父项目

  • 步骤一:创建父项目 cloud_parent

  • 步骤二:修改pom.xml文件,确定spring cloud版本


    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
    

    
    
        UTF-8
        1.8
        Greenwich.RELEASE
    

    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud-release.version}
                pom
                import
            
        
    

    
    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
            
                false
            
        
    

搭建注册中心

  • 步骤一:创建子项目 eureka_demo

  • 步骤二:修改pom.xml文件,添加web和 eureka service 依赖

  
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
    
  • 步骤三:创建yml文件,配置端口号、服务名、eureka注册地址

#服务端口号
server:
  port: 10086
#服务名称
spring:
  application:
    name: eurekaDemo
#注册中心地址
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;

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

搭建服务提供方

  • 步骤一:创建提供方项目,eureka_service

  • 步骤二:修改pom.xml文件,添加 web、eureka client、spring boot 监控依赖  (监控依赖可加可不加)


        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
    
  • 步骤三:创建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;


@SpringBootApplication
@EnableEurekaClient   //开启eureka客户端
public class ServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceApplication.class,args);
    }
}

步骤五:编写controller , 测试程序

  • 测试路径: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;


@RestController
@RequestMapping("/test")
public class TestController {
    @GetMapping
    public ResponseEntity test(){
        return ResponseEntity.ok("测试数据");
    }
}

搭建 服务调用方

  • 步骤一:创建调用方项目,eureka_client

  • 步骤二:修改pom.xml文件,添加 web、eureka client、spring boot 监控依赖(与eureka_service项目一样)


        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
    
  • 步骤三:创建yml文件,(与eureka_service项目相似,有不同端口和服务名)

#端口号
server:
  port: 9090
#服务名称
spring:
  application:
    name: client
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
  • 步骤四:编写启动类,添加eureka客户端注解,(与eureka_service项目相似,有不同类名)

package com.czxy;

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


@SpringBootApplication
@EnableEurekaClient     //开启eureka客户端
public class ClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class,args);
    }
}


调用方测试数据

Spring Cloud常见模块有哪些

  • 步骤一:编写config配置类,用于配置RestTemplate(远程调用)实例

package com.czxy.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;


@Configuration
public class HttpConfig {
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}
  • 步骤二:编写DataDao,用于进行远程调用

package com.czxy.dao;

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

import javax.annotation.Resource;


@Component
public class DataDao {
    @Resource
    private RestTemplate restTemplate;

    public ResponseEntity data(){ 
                                          //服务提供方地址
        return restTemplate.getForEntity("http://localhost:8080/test",String.class);
    }
}
  • 步骤三:编写DataController,提供接口进行访问

package com.czxy.controller;

import com.czxy.dao.DataDao;
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;


@RestController
@RequestMapping("/data")
public class DataController {
    @Resource
    private DataDao dataDao;\

    @GetMapping
    public ResponseEntity data(){
        return dataDao.data();
    }
}
  •  测试路径:  http://localhost:9090/data

  • 显示结果 "测试数据" 说明就Eureka入门案例就完成了

追加 ----> 配置eureka instance

Spring Cloud常见模块有哪些

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

感谢各位的阅读,以上就是“Spring Cloud常见模块有哪些”的内容了,经过本文的学习后,相信大家对Spring Cloud常见模块有哪些这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


网页标题:SpringCloud常见模块有哪些
文章出自:http://bjjierui.cn/article/jocgcs.html

其他资讯