400 028 6601

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

SpringCloud常见模块有哪些

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

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

什么是Spring Cloud :  

Spring Cloud 常见模块 :

Spring Cloud常见模块有哪些

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

Eureka 入门

Eureka职责:

Eureka 入门案例

搭建父项目


    
        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
            
        
    

搭建注册中心

  
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
    
#服务端口号
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           #关闭从注册中心获得列表(不拉去其他服务信息)
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);
    }
}

搭建服务提供方


        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
    
#端口号
server:
  port: 8080
#服务名称
spring:
  application:
    name: service
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
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 , 测试程序

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("测试数据");
    }
}

搭建 服务调用方


        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
    
#端口号
server:
  port: 9090
#服务名称
spring:
  application:
    name: client
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
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常见模块有哪些

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();
    }
}
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);
    }
}
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();
    }
}

追加 ----> 配置eureka instance

Spring Cloud常见模块有哪些

yml文件配置

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地址
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://www.bluegullmedia.com/article/jocgcs.html

其他资讯

让你的专属顾问为你服务

1.5479s