Spring Cloud快速入門git
代碼地址:spring
https://gitee.com/gloryxu/spring-cloud-testtomcat
EureKa:服務註冊中心app
添加依賴 負載均衡
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
開啓Eureka Serverfetch
@EnableEurekaServer @SpringBootApplication public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
配置spa
#設置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.建立一個服務提供者rest
添加依賴code
<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/
啓動 註冊成功,調用成功