搭建SpringCloud框架
1.使用版本
这里我使用的springboot版本为2.3.3.RELEASE,springcloud版本为Hoxton.SR1
一定要注意,使用springcloud时版本一定要和springboot兼容,不然会出现很多你意向不到的错误
详细的版本对应可以查看:https://start.spring.io/actuator/info
java版本是1.8
2.框架搭建使用的组件
这里 主要使用的cloud组件有:
- eureka 服务注册中心,将所有的服务都注册到这里来
- Feign 声明式的web service客户端,让各个服务之间的调用变得简单
- zuul 解决跨域问题
3.服务创建
-
创建springboot项目做service1,加入需要的依赖
<properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
-
创建controller
@RestController public class HelloController { @RequestMapping("hello1") public void hello1(HttpServletResponse response) throws IOException { response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); response.getWriter().println("这里是service1"); } }
-
在复制一份代码做service2
4.注册中心 eureka
-
新建springboot项目,导入依赖
<properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
-
启动类增加
@EnableEurekaServer
注解@SpringBootApplication @EnableEurekaServer public class SpringcloudeurekaApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudeurekaApplication.class, args); } }
-
添加配置application.properties
server.port=8090 eureka.instance.hostname=localhost #是否向服务注册中心注册自己,默认为true eureka.client.registerWithEureka=false #是否检索服务 eureka.client.fetchRegistry=true eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
-
启动项目,浏览http://localhost:8090/
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jYgRblnI-1598952430057)(D:\Typora\Image\image-20200901144930323.png)]
图示表示eureka创建成功,现在我们来把service1和service2注册进eureka里面
-
service1和service2添加配置
##service1 server.port=8091 eureka.client.service-url.defaultZone=http://localhost:8090/eureka/ spring.application.name=springcloudwervice1
##service2 server.port=8092 eureka.client.service-url.defaultZone=http://localhost:8090/eureka/ spring.application.name=springcloudwervice2
-
启动类添加
@EnableEurekaClient
注解(做测试时启动类没有添加@EnableEurekaClient
注解也是可以被eureka发现的) -
启动service1和service2,刷新http://localhost:8090/
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EFOe9QyH-1598952430060)(D:\Typora\Image\image-20200901145654206.png)]
到这里就将service1和service2注册进去了
5.使用Feign做服务之间的调用
这里我们准备通过service1调用service2
//service2的controller
@RestController
public class HelloController {
@RequestMapping("hello2")
public void hello2(HttpServletResponse response) throws IOException {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().println("这里是service2");
}
@RequestMapping("rest")
public String rest(){
return "这里是service2";
}
}
//service1的feign接口
@Service
@FeignClient(name = "springcloudwervice2")
public interface CloudService2Interface {
@RequestMapping("/rest")
public String rest();
}
//service1的controller
@RestController
public class HelloController {
@Autowired
CloudService2Interface cloudService2Interface;
@RequestMapping("hello1")
public void hello1(HttpServletResponse response) throws IOException {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().println("这里是service1");
}
//通过调用这里的rest调用serveice2
@RequestMapping("/rest")
public void rest(HttpServletResponse response) throws IOException {
String rest = cloudService2Interface.rest();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().println(rest);
}
}
然后application.properties添加ribbon.eureka.enabled=true
,开启负载均衡
启动类添加@EnableFeignClients
注解开启feign
测试:浏览http://localhost:8091/rest应该显示”这里是service2“表示调用成功
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-r7X9E8Pe-1598952430062)(D:\Typora\Image\image-20200901151719725.png)]
6.zuul网关
-
新建springboot项目,添加依赖
<properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
-
启动类添加注解
@EnableEurekaClient
,@EnableZuulProxy
@SpringBootApplication @EnableEurekaClient @EnableZuulProxy//开启网关 public class SpringcloudzuulApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudzuulApplication.class, args); } }
-
修改配置文件
server.port=9000 eureka.client.service-url.defaultZone=http://localhost:8090/eureka/ spring.application.name=springcloudzuul zuul.routes.springcloudwervice1.path = /springcloudwervice1/** zuul.routes.springcloudwervice2.path = /springcloudwervice2/**
-
启动项目,浏览http://localhost:9000/springcloudwervice1/rest,应该会返回“这里是service2”,表示成功
-
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-avXWdgNi-1598952430064)(D:\Typora\Image\image-20200901163355624.png)]
7.Hystrix熔断
-
Service1添加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
-
启动类添加
@EnableCircuitBreaker
注解 -
service1的controller
@RestController public class HelloController { @Autowired CloudService2Interface cloudService2Interface; @RequestMapping("/rest") @HystrixCommand(fallbackMethod = "defaultMethod") public void rest(HttpServletResponse response) throws IOException { String rest = cloudService2Interface.rest(); response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); response.getWriter().println(rest); } public void defaultMethod(HttpServletResponse response) throws IOException { response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); response.getWriter().println("看到这就已经熔断了"); } }
-
然后重新所有服务,调用service1的rest显示“这里是service2”是正常的,然后关掉service2服务,再次调用service1的rest
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-m3fMkdYN-1598952430065)(D:\Typora\Image\image-20200901172619775.png)]
熔断成功