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

idea简单搭建springCloud项目包含(gateway与zuul)

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

idea简单搭建springCloud项目包含(gateway与zuul)

idea简单搭建springCloud项目包含(gateway与zuul) 搭建过程中主要遇到的问题就是依赖版本问题。阅读者项目如果没有启动,可以看下你的依赖版本。 一.先搭建eureka项目父工程

依赖:


      
          org.springframework.cloud
          spring-cloud-starter-netflix-eureka-server
      

      
          org.springframework.boot
          spring-boot-starter-test
          test
      
  

yml配置:

server:
port: 8761
eureka:
instance:
  hostname: localhost
client:
  register-with-eureka: false
  fetch-registry: false
  service-url:
    defaultZone: http://localhost:8761/eureka/
spring:
application:
  name: online

启动类:

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaDemoApplication {

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

}

浏览器访问:http://localhost:8761/

二.搭建生产者

依赖:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.5
         
    
    com.example
    product-demo
    0.0.1-SNAPSHOT
    product-demo
    Demo project for Spring Boot
    
        1.8
        2020.0.4
    
    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        


        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
        
        
        
        
            com.alibaba
            fastjson
            1.2.72
        
        
            io.github.openfeign
            feign-httpclient
            10.2.0
        

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

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



yml配置:

server:
  port: 8763
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: server-product

controller类:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProductDemoController {

    @Value("${server.port}")
    String port ;

    @GetMapping("/hi")
    public String home(@RequestParam String name){
        return "hi ... " + name + ",i am from port:" + port;
    }
}

启动类:

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

@SpringBootApplication
@EnableEurekaClient
public class ProductDemoApplication {

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

}

访问:http://localhost:8763/hi?name=xxx

并且注册中心界面也可以看到

三.消费方(这里我加了熔断不用可以去掉@EnableHystrix与 @HystrixCommand(fallbackMethod = “hiError”)):

依赖:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.5
         
    
    com.example
    product-demo
    0.0.1-SNAPSHOT
    product-demo
    Demo project for Spring Boot
    
        1.8
        2020.0.4
    
    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        


        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
        
        
        
        
            com.alibaba
            fastjson
            1.2.72
        
        
            io.github.openfeign
            feign-httpclient
            10.2.0
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework
            spring-web
            5.3.10
        


        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
            2.2.9.RELEASE
        


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

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



yml配置:

server:
  port: 8764
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: server-consumer

controller类:

import com.example.consumerdemo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloControler {

    @Autowired
    HelloService helloService;

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

}

service层:

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HelloService {
    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")
    public String hiService(String name) {
        return restTemplate.getForObject("http://server-product/hi?name=" + name, String.class);
    }

    public String hiError(String name) {
        return "key-->" + name + ", this is error page..didi ..." ;
    }

}

启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient
@SpringBootApplication
@EnableHystrix
public class ConsumerDemoApplication {

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

    @Bean
    @LoadBalanced
        // 标注此注解后,RestTemplate就具有了客户端负载均衡能力
    RestTemplate template (){
        return new RestTemplate();
    }

}

访问:http://localhost:8764/hi?name=oop(熔断机制可以停掉生产方服务触发)

四.网关(zuul与gateway选一个就可)

1)zuul
依赖:



  4.0.0
  
      org.springframework.boot
      spring-boot-starter-parent
      2.1.3.RELEASE
       
  
  com.example
  zuul-demo
  0.0.1-SNAPSHOT
  zuul-demo
  Demo project for Spring Boot
  
      1.8
      Greenwich.RELEASE
  
  
      
          org.springframework.boot
          spring-boot-starter-webflux
      

      
      
          org.springframework.cloud
          spring-cloud-starter-netflix-eureka-client
          2.1.0.RELEASE
      

      
          org.springframework.boot
          spring-boot-starter-test
          test
      
      
          io.projectreactor
          reactor-test
          test
      

      
      
          org.springframework.cloud
          spring-cloud-starter-netflix-zuul
          2.1.0.RELEASE
      

      
      
          org.springframework.cloud
          spring-cloud-starter-zuul
          1.4.7.RELEASE
      



      
          org.springframework.cloud
          spring-cloud-starter-eureka
          1.4.7.RELEASE
      
      
          org.junit.jupiter
          junit-jupiter-api
          5.7.2
          test
      

  
  
      
          
              org.springframework.cloud
              spring-cloud-dependencies
              Greenwich.RELEASE
              pom
              import
          
      
  

  
      
          
              org.springframework.boot
              spring-boot-maven-plugin
          
      
  




yml:

#端口号
server:
port: 8765
#配置应用名称
spring:
application:
 name: zuul-server
#eureka
eureka:
client:
 service-url:
   defaultZone: http://localhost:8761/eureka
#zuul
#前缀
#zuul.prefix=/tour
zuul:
routes:
 online:
   path: /online
@EnableDiscoveryClient
@SpringBootApplication
public class GatewayDemoApplication {

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

}

启动服务即可:http://localhost:8765/online/hi?name=ooo

注册中心:

简单的搭建就完毕啦。

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

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

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