搭建SpringCloud


源码地址:https://github.com/zhangzhangzhang0/spring-cloud

1、搭建一个空的spring cloud项目

  • File —> new —> Project
    在这里插入图片描述

2、创建spring cloud netflix eureka (server/client)项目

  • next 填写项目名称,然后继续next

  • 选择依赖
    在这里插入图片描述

  • ServerApplication.java

package com.example.server;

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

@SpringBootApplication
@EnableEurekaServer
public class ServerApplication {

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

}
  • application.properties
server.port=6161
eureka.instance.hostname=localhost
spring.application.name = eureka
eureka.client.register-with-eureka= true
eureka.client.fetchRegistry = true
eureka.client.service-url.defaultZone=http://localhost:6161/eureka/
  • 运行即可
  • 登录http://localhost:6161/eureka/可进入控制中心
    在这里插入图片描述
  • client module
    新建client的子模块

在这里插入图片描述

  • ClientApplication
package com.example.client;

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

@SpringBootApplication
@EnableEurekaClient
public class ClientApplication {

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

}
  • application.properties

server.port=6162
spring.application.name=client
#制定服务中心
eureka.client.service-url.defaultZone=http://localhost:6161/eureka/

  • 运行完成,可以登录控制中心查看

3、创建spring cloud netflix zuul项目

  • 新建zuul子模块
    在这里插入图片描述
  • ZuulApplication
package com.example.zuul;

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

@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class ZuulApplication {

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

}
  • application.properties

server.port=6163
spring.application.name=zuul
#制定服务中心
eureka.client.service-url.defaultZone=http://localhost:6161/eureka/
#消费者
zuul.routes.consumer.path=/api/test/haha/get
zuul.routes.consumer.service-id=consumer
  • 运行完成,可以登录控制中心查看

4、创建spring cloud config (配置从git获取)项目

  • 新建config子模块
    在这里插入图片描述
  • ConfigApplication
package com.example.config;

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


@EnableDiscoveryClient
@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {

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

}
  • application.properties

部署仓库位置

spring.application.name=config-server
spring.cloud.config.server.git.uri=https://github.com/zhangzhangzhang0/spring-cloud.git

#  Git仓库用户名或邮箱
spring.cloud.config.server.git.username=zhangzhangzhang0
eureka.client.service-url.defaultZone=http://localhost:6161/eureka
#  Git仓库密码
spring.cloud.config.server.git.password=794353281YY
server.port=6164
  • 运行完成

5、创建consumer/producer两个demo项目

  • 与client类似,建立Producer子模块

  • ProducerApplication.java

package com.example.producer;

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

@EnableDiscoveryClient
@SpringBootApplication
public class ProducerApplication {

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

}
  • ProducerController.java
package com.example.producer;

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

@RestController
public class ProducerController {
    @RequestMapping("/producer/haha")
    public String produce(){
        return "成功了";
    }
}
  • application.properties

server.port=6110
#名称
spring.application.name=producer
#指定服务中心
eureka.client.service-url.defaultZone=http://localhost:6161/eureka

  • 与client类似,建立consumer子模块

  • ConsumerApplication.java

package com.example.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
@EnableHystrix
public class ConsumerApplication {

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

}
  • Controller.java
package com.example.consumer;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class Controller {
    @Bean
    public RestTemplate restTemplate(){
        return  new RestTemplate();
    }

    @Autowired(required = false)
    private FeignClientService feignClientService;

 //   @Autowired
   // private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "executeHystrixHandle")
    @RequestMapping("/api/test/haha/get")
    public String consumer(){
       // return this.restTemplate.getForObject("http://localhost:6110/producer/haha", String.class);
        return feignClientService.consumer();
    }

    //服务进入保护时,回调方法

    public String executeHystrixHandle() {
        return  "Hello, the current system has a large number of people, please try again later, please forgive me for the inconvenience! ! !";

    }


}

  • FeignClientService.java
package com.example.consumer;

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

@FeignClient(value = "producer")
public interface FeignClientService {
    @RequestMapping("/producer/demo")
    public String consumer();

}
  • application.properties

server.port=6111
#名称
spring.application.name=consumer
#指定服务中心
eureka.client.service-url.defaultZone=http://localhost:6161/eureka

feign.hystrix.enabled=true
  • 运行完成,可以通过localhost直接访问consumer
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值