Spring Cloud快速入门
代码地址:
https://gitee.com/gloryxu/spring-cloud-test
EureKa:服务注册中心
添加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
开启Eureka Server
@EnableEurekaServer @SpringBootApplication public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
配置
#设置tomcat服务端口号 server.port=8101 # 本地调试环境下关闭自我保护机制 eureka.server.enable-self-preservation=false # 清理间隔时间,单位为毫秒 eureka.server.eviction-interval-timer-in-ms=5000 #设置服务名称 spring.application.name=eureka-service eureka.instance.hostname=localhost #注册中心不需要注册自己 eureka.client.register-with-eureka=false #注册中心不需要去发现服务 eureka.client.fetch-registry=false #设置服务注册中心的URL eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
启动成功
2.创建一个服务提供者
添加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
@EnableDiscoveryClient // 声明这是一个Eureka Client @SpringBootApplication public class Server1Application { public static void main(String[] args) { SpringApplication.run(Server1Application.class, args); } }
添加配置
server.port=9090 #设置服务名 spring.application.name=hello-service #设置服务注册中心的URL,本服务要向该服务注册中心注册自己 eureka.client.serviceUrl.defaultZone=http://localhost:8101/eureka
添加Controller
@RestController public class HelloController { Logger logger = LoggerFactory.getLogger(HelloController.class); @RequestMapping("/hello") public String hello() { return "hello"; } }
启动注册成功
3.创建一个消费者
添加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
LoadBalanced 方式可实现负载均衡
@EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class ConsumerApplication { @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
添加配置
server.port=2222 spring.application.name=hello-consumer eureka.client.serviceUrl.defaultZone=http://localhost:8101/eureka/
声明Feign方式 ,value为注册的服务名
@FeignClient(value = "hello-service") public interface HelloService { @RequestMapping(value = "/hello") String hello(); }
以下以两种方式调用服务提供者,一种是以Rest方式,另一种以Feign方式
@RestController public class ConsumerController { Logger logger = LoggerFactory.getLogger(ConsumerController.class); @Autowired private RestTemplate restTemplate; @Autowired HelloService helloService; @GetMapping("/getserver") public String getserver() { String xx=restTemplate.getForObject("http://hello-service/hello", String.class); return "consumer finish result:"+xx; } @GetMapping("/gettest") public String gettest(){ return helloService.hello(); } }
启动
4.创建Zuul路由
添加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency>
加入注解,以便注册到注册中心
@EnableZuulProxy @SpringBootApplication public class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); } }
配置路由,以下是以服务的方式调用
spring.application.name=eureka-zuul server.port=8765 zuul.routes.hello-service.path=/hello-service/** zuul.routes.hello-service.serviceId=hello-service eureka.client.serviceUrl.defaultZone=http://localhost:8101/eureka/
启动 注册成功,调用成功