搭建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