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: falsefetch-registry: falseservice-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 {@AutowiredHelloService 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 {@AutowiredRestTemplate 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/**service-id: server-consumer
zuul:sensitive-headers: Set-Cookie,Cookie,Host,Connection,Content-Length,Content-Encoding,Server,Transfer-Encoding,X-Application-Context
# zuul.routes.account.path=/account/**
#zuul.routes.account.service-id=server-consumer
#zuul.routes.online.path=/online/**
#// 需要代理的服务
#zuul.routes.online.service-id=server-consumer
启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class ZuulDemoApplication {public static void main(String[] args) {SpringApplication.run(ZuulDemoApplication.class, args);}}
启动zuul网关后,可以根据网关配置的路径访问了就
eureka注册中心也能看到,这里就不配图了
现在来看下Gateway
依赖:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.5.5 com.example gateway-demo 0.0.1-SNAPSHOT gateway-demo Demo project for Spring Boot 1.8 2020.0.4 org.springframework.cloud spring-cloud-starter-gateway org.springframework.cloud spring-cloud-starter-loadbalancer org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
yml配置:
# 网关配置
spring:application:name: gateway-servercloud:gateway:discovery:locator:lower-case-service-id: true #表示将请求路径的服务名配置改成小写 因服务注册的时候,向注册中心注册服务是将服务名转成大写的了enabled: true #是否与服务注册于发现组件进行结合,通过 serviceId转发到具体的服务实例。 默认为false,设为true便开启通过服务中心的自动根据serviceId创建路由的功能routes:# 路由到本机的9000端口上- id: hello_route #我们自己定义的路由ID,保持唯一性一般与程序名保持一致uri: http://localhost:8764predicates:- Path=/online/**filters:- StripPrefix=1# 集成eureka注册中心的配置示例- id: online_aaa#代表从注册中心获取服务,且以lb(load-balance)负载均衡方式转发uri: lb://server-consumer:8764predicates: #断言- Path=/online1/** #表示将以/online开头的请求转发到uri为lb://server-consumer的地址上 转发地址为uri/server-consumer/**filters: #过滤器- StripPrefix=1 #代表截取路径的个数 当请求匹配到/online/** 会将/online去掉转发。例/onlinea/hello就会变成127.0.0.1:8766/hello(后端请求路径)# 路由到百度页面- id: baidu_routeuri: http://www.baidu.compredicates:- Path=/baidu/**filters:- StripPrefix=1
server:port: 8765#服务注册中心
eureka:client:register-with-eureka: true #是否将自己注册到注册中心fetch-registry: true #是否从注册中心服务注册信息service-url:defaultZone: http://localhost:8761/eurekainstance:prefer-ip-address: true#定义实例ID格式instance-id: ${spring.application.name}:${server.port}
启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;/*
*
* zzs
* */
@EnableDiscoveryClient
@SpringBootApplication
public class GatewayDemoApplication {public static void main(String[] args) {SpringApplication.run(GatewayDemoApplication.class, args);}}
启动服务即可:http://localhost:8765/online/hi?name=ooo
注册中心:
简单的搭建就完毕啦。
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!