1.前言
博客推荐
介绍以下优秀的springcloud博客,感谢他们的分享
组件介绍
spring-cloud主要就是将组件组装在一起,这篇文章我们介绍Eureka,Ribbon,Fegin这三个组件
- Eureka:注册中心,类似zookeeper
- ribbon:负载均衡
- fegin:简化前两者的使用
2.Eureka-Server
1.添加依赖
既然我们使用idea,当然要用简单的方法了,不需额外再添加依赖了
new->Module->Spring initializr->填写group等信息->选择框架Euraka Server 大功告成
2.配置文件
server.port=10086
spring.application.name=eureka-server
eureka.client.register-with-eureka=false #是否把自己注册Eureka-server
eureka.client.fetch-registry=false #是否拉取其它服务的信息
eureka.client.service-url.defaultZone=http://127.0.0.1:${server.port}/eureka
3.启动类
只添加一个@EnableEurekaServer注解即可
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
4.启动
现在我们已经完成了Eureka注册中心的配置,赶紧启动它吧
图片下面我们看到No instances avaliable,这是因为我们还没有发布服务的原因
5.Eureka高可用
server.port=10086
eureka.client.service-url.defaultZone=http://127.0.0.1:10087/eureka
spring.application.name=eureka-server
server.port=10087
eureka.client.service-url.defaultZone=http://127.0.0.1:10086/eureka
spring.application.name=eureka-server
6.Eureka自我保护
我们关停一个服务,就会看到:
这是触发了Eureka的自我保护机制。当一个服务未按时进行心跳续约时,Eureka会统计最近15分钟心跳失败的服务实例的比例是否超过了85%。在生产环境下,因为网络延迟等原因,心跳失败实例的比例很有可能超标,但是此时就把服务剔除列表并不妥当,因为服务可能没有宕机。Eureka就会把当前实例的注册信息保护起来,不予剔除。生产环境下这很有效,保证了大多数服务依然可用
但是这给我们的开发带来了麻烦, 因此开发阶段我们都会关闭自我保护模式:
eureka.server.enable-self-preservation=false
eureka.server.eviction-interval-timer-in-ms=1000
3.创建book-producer
1.前言
因为只是初学,所以做的比较简单,你可以选择自己的数据库来查询数据,我相信你早已对ssm框架了解
2.创建实体类
创建一个公用的Module来存放我们的实体类,这样我们在这后的模块中,只需导入我们的实体类模块即可
import lombok.Getter;
import lombok.Setter;
import java.util.Date;
@Getter
@Setter//使用lombok提供的get,set方法注解
public class Book {
private int id;
private String name;
private String author;
private String press;
private String img;
private int page;
private int booktype;
private String feature;
private Date pubdate;
}
3.建立依赖
和eureka注册中心不同,我们选择的是Eureka Discovery框架,当然web,mysql和mybatis不要忘记了
4.修改配置文件
连接数据库已经是常规操作,记得根据本机做一定的修改
server.port=8081
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc\:mysql\://localhost\:3306/数据库?characterEncoding\=utf8&useSSL\=false&serverTimezone\=UTC&allowPublicKeyRetrieval\=true
spring.datasource.username=root
spring.datasource.password=密码
spring.application.name=book-producer
spring.main.allow-bean-definition-overriding=true
eureka.client.service-url.defaultZone=http://127.0.0.1:10086/eureka
eureka.instance.prefer-ip-address=true
eureka.instance.ip-address=127.0.0.1
5.创建mapper
由于只是demo,所以简单粗暴一点,直接使用注解select查询,同时也不再写service层,简单,实现功能就好
@Mapper
public interface BookMapper {
@Select("select * from bookinfo where id=#{id}")
Book selectById(int id);
}
6.创建contoller
@RestController
@RequestMapping("book")
public class BookController {
@Autowired
BookMapper bookMapper;
@RequestMapping("/{id}")
public Book book(@PathVariable("id") int id){
return bookMapper.selectById(id);
}
}
7.开启程序
我们通过输入网址已经能获得json数据,接下来我们要做的就是在其他程序上也能获得这串数据
8.创建多个服务
右上角打勾号,我们就可以开启多个服务
4.创建book-consumer1
1.前言
此程序使用了Eureka,ribbon的相关组件
2.导入依赖
由于Eureka组件已经集成了ribbon组件,所以我们加上Eureka Discovery和web就足够了
3.配置文件
server.port=8085
spring.main.allow-bean-definition-overriding=true
spring.application.name=book-consumer1
eureka.client.service-url.defaultZone=http://127.0.0.1:10086/eureka
eureka.instance.prefer-ip-address=true
eureka.instance.ip-address=127.0.0.1
BOOK-PRODUCER.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule #ribbon的负载均衡算法,当有多个服务提供者时,默认的算法为轮询,这里可以改为随机
4.启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableEurekaClient
public class Consumer1Application {
public static void main(String[] args) {
SpringApplication.run(Consumer1Application.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
5.controller
@RestController
public class BookController {
@Autowired
RestTemplate restTemplate;
@Autowired
LoadBalancerClient loadBalancerClient;
@RequestMapping("test")
public void test(){ //此方法输出获得的端口号,可以借此检验ribbon的负载均衡功能
for (int i = 0; i < 100; i++) {
ServiceInstance instance = this.loadBalancerClient.choose("BOOK-PRODUCER");
System.out.println(instance.getHost() + ":" + instance.getPort());
}
}
@RequestMapping("book/{id}")
public String book(@PathVariable("id") int id){
return restTemplate.getForObject("http://BOOK-PRODUCER/book/"+id,String.class);
}
}
6.启动
5.创建book-consumer2
1.导入依赖
此次集成了fegin,fegin对前面进行了简化,这次我们需要加入fegin框架
2.修改配置文件
server.port=8086
spring.main.allow-bean-definition-overriding=true
spring.application.name=book-consumer2
eureka.client.service-url.defaultZone=http://127.0.0.1:10086/eureka
eureka.instance.prefer-ip-address=true
eureka.instance.ip-address=127.0.0.1
3.修改启动类
加入两个注解:@EnableEurekaClient ,@EnableFeignClients就可以了
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class Consumer2Application {
public static void main(String[] args) {
SpringApplication.run(Consumer2Application.class, args);
}
}
4.创建接口
创建web包,创建FeginService接口
package spring_cloud_test.consumer2.web;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import spring_cloud_test.common.entity.Book;
@FeignClient("BOOK-PRODUCER")
public interface FeignService{
@RequestMapping("/book/{id}")
Book queryById(@PathVariable("id") int id);
}
5.创建controller
@RestController
public class BookController {
@Autowired
FeignService feignService;
@RequestMapping("/booktest/{id}") //做以区别
public Book book(@PathVariable("id") int id){
return feignService.queryById(id);
}
}
6.实现