栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

搭建简单的springcloud项目

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

搭建简单的springcloud项目

springcloud每个模块就是一个服务,每个服务就是一个springboot项目,这里使用了maven子父模块管理项目

第一步 搭建父项目

 

 

 

 父项目 删掉 src目录

 定义父项目pom.xml文件



    4.0.0
    
        eureka-server
        cloud-client
        cloud-provider
    
    
        spring-boot-starter-parent
        org.springframework.boot
        2.7.2
        
    

    com.yzg.test
    springcloud-test
    1.0-SNAPSHOT
    pom
    
        8
        8
        UTF-8
        UTF-8
        1.8
    

二:搭建eureka-service注册中心

 

 

 

 定义eureka-service的pom.xml



    4.0.0
    
    
        com.yzg.test
        springcloud-test
        1.0-SNAPSHOT
    
     com.yzg.test
    eureka-server
    0.0.1-SNAPSHOT
    eureka-server
    Demo project for Spring Boot
    
        1.8
        2021.0.3
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
        
    

 启动 加注解

@EnableEurekaServer

 配置 application.yml文件

 

server:
  port: 8080

spring:
  application:
    #应用名称(在注册中显示的)
    name: eureka-server
eureka:
  client:
    #此客户端是否获取eureka服务器注册表上的注册信息,默认为true
    fetch-registry: false
    #实例是否在eureka服务器上注册自己的信息以供其他服务发现,默认为true,即自己注册自己。
    register-with-eureka: true
    #与Eureka注册服务中心的通信zone和url地址
    serviceUrl:
      #http://localhost:8080/eureka/eureka
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
  #服务注册中心实例的主机名
  instance:
    hostname: 127.0.0.1
    prefer-ip-address: true
    instance-id: 127.0.0.1:8080
  server:
    #设为false,关闭自我保护,即Eureka server在云心光器件会去统计心跳失败比例在15分钟之内是否低于85%,如果低于85%,EurekaServer
    #会将这些事例保护起来,让这些事例不会过期,但是在保护器内如果刚哈这个服务提供者非正常下线了,此时服务消费者会拿到一个无效的服务
    #实例,此时调用会失败,对于这个问题需要服务消费者端有一些容错机制,如重试、断路器等;
    enable-self-preservation: false
    #扫描失效服务的间隔时间(单位是毫秒,摩恩是60*1000),即60s
    eviction-interval-timer-in-ms: 10000

测试一下

 三 :搭建提供者服务

 

 

 

 定义pom.xml文件



    4.0.0
    
    
        com.yzg.test
        springcloud-test
        1.0-SNAPSHOT
    
     com.yzg.test
    cloud-provider
    0.0.1-SNAPSHOT
    cloud-provider
    Demo project for Spring Boot
    
        1.8
        2021.0.3
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
        
    

修改配置 文件application.yml

spring:
  application:
    name: cloud-provider
server:
  port: 8081

eureka:
  client:
    #此客户端是否获取eureka服务器注册表上的注册信息,默认为true
    fetch-registry: false
    #实例是否在eureka服务器上注册自己的信息以供其他服务发现,默认为true,即自己注册自己。
    register-with-eureka: true
    service-url:
      #defaultZone 这个是不会提示的,此处需要自己写
      #实际上属性应该是service-url,这个属性是个map(key-value)格式;当key是defaultZone的时候才能被解析;所以这里没有提示,
      #但是自己还需要写一个defaultZone;
      defaultZone: http://localhost:8080/eureka
  #服务注册中心实例的主机名
  instance:
    hostname: 127.0.0.1
    prefer-ip-address: true
    instance-id: 127.0.0.1:8081

启动类 添加注解

@EnableEurekaClient

 四 :搭建 消费者

 

 

 定义 pom.xml文件



    4.0.0
    
    
        com.yzg.test
        springcloud-test
        1.0-SNAPSHOT
    
    com.yzg.test
    demo
    0.0.1-SNAPSHOT
    cloud-client
    Demo project for Spring Boot
    
        1.8
        2021.0.3
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
        
    

 修改配置文件application.yml

server:
    #定义端口号
  port: 8082
spring:
  application:
    #定义应用名称,即服务名称
    name: cloud-client
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8080/eureka
  #服务注册中心实例的主机名
  instance:
    hostname: 127.0.0.1
    prefer-ip-address: true
    instance-id: 127.0.0.1:8082

启动类注解 

@EnableEurekaClient

 测试

 这样 三个服务都注册上来了

在父项目上

    
        eureka-server
        cloud-provider
        cloud-client
    

 五:实现服务之间的调用

在cloud-provider创建controller和service,servieimpl

 controller层

 

package com.yzg.test.cloudprovider.controller;

import com.yzg.test.cloudprovider.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HellerController {
   @Autowired
    private HelloService helloService;
   @GetMapping("/getHello")
    public String getHello(){
       return helloService.getHello();
   }
}

service层

 

package com.yzg.test.cloudprovider.service;

public interface HelloService {
    String getHello();
}

impl实现层

 

package com.yzg.test.cloudprovider.service.impl;

import com.yzg.test.cloudprovider.service.HelloService;
import org.springframework.stereotype.Service;

@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public String getHello() {
        return "你好菜狗!!!!";
    }
}

测试

 在cloud-client中创建 controller和service

 controller

 

package com.yzg.test.demo.controller;

import com.yzg.test.demo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("hello")
public class HelloClient {
    @Autowired
    private HelloService helloService;

    @GetMapping("/getClient")
    public String getClient(){
        return helloService.getProduct();
    }
}

service层

 

package com.yzg.test.demo.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

// name 为product项目中application.yml配置文件中的 application.name
// path 为product项目中application.yml配置文件中的 application.path
@FeignClient(name="cloud-provider",path = "/hello")
//@Component 注解最好 加上,不加idea会显示错误,但是不影响系统运行;
@Component
public interface HelloService {
    @RequestMapping(value = "getHello")
    String getProduct();
}

启动类 加上 @EnableFeignClients

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1032847.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号