前言:
我以前学习springcloud的组件的时候,遇到了很多问题,在网上搜索的时候,也是五花八门,所以我决定写一个springcloud的各大组件的快速入门,和每个配置的讲解,为了让更多的小伙伴学会springcloud
1.什么是eureka
2.eureka的原理图及其讲解
简易的架构图
3.eureka源码分析
3.1 代码结构和功能分析
4.可能会被坑的几处原理
4.1 eureka的几处缓存
Eureka的wiki上一句话,大意就是一个服务启动的最长时间可能需要2分钟,才能被其他服务感知
,这就是由三处缓存+一处延迟造成的
4.2 多网卡环境下的ip选择问题
5.搭建eureka server项目
5.1 pom
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!--eurekaserver 依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
<!--安全依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
5.2 application.yml
server:
port: 10000
#因为当前的 eureka 是单机的,所以我们需要做一些配置
eureka:
client:
register-with-eureka: false # 是否注册自己的信息到EurekaServer,默认是true
fetch-registry: false # 是否拉取其它服务的信息,默认是true
serviceUrl:
# defaultZone: http://localhost:10000/eureka # EurekaServer的地址,现在是自己的地址,如果是集群,需要加上其它Server的地址
defaultZone: http://user:123@localhost:10000/eureka
security:
basic:
enabled: true #开启安全配置,也就是需要密码,如果不需要设置为 false 即可,注意,这个参数必须放在 application.yml 中,不允许放在 bootstrap.yml
user:
name: user
password: 123 #在配置了用户名和密码后,我们可以修改地址的访问风格为 curl 风格
5.3 启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
6.搭建eureka client项目-----服务的提供者
6.1 pom
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
6.2 application.yml
server:
port: 8001
spring:
application:
name: springcloud-privider
eureka:
client:
serviceUrl:
defaultZone: http://user:123@localhost:10000/eureka
instance:
prefer-ip-address: true
ip-address: 127.0.0.1 # 指定自己的ip信息,不指定的话会自己寻找
6.3启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class PrividerApplication {
public static void main(String[] args) {
SpringApplication.run(PrividerApplication.class,args);
}
}
6.4 pojo,controller,service(本案例不打算访问数据库,直接写死的service)
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Long id;
private String name;
}
import cn.carry.shuai.pojo.User;
import cn.carry.servicce.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/getUserById")
public User getUser(){
return userService.getUser();
}
}
import cn.carry.shuai.pojo.User;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public User getUser(){
return new User(1L,"carry");
}
}
7.搭建一个eureka 客户端---服务的消费者
7.1 pom
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
7.2 application.yml
server:
port: 8002
spring:
application:
name: springcloud-consumer
eureka:
client:
serviceUrl:
defaultZone: http://user:123@localhost:10000/eureka
instance:
prefer-ip-address: true
ip-address: 127.0.0.1 # 指定自己的ip信息,不指定的话会自己寻找
7.3 主启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
}
7.4 添加一个配置,注入一个restTemplate
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class Configs {
/*注入一个temp模板*/
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
7.5pojo同上 controller
import cn.carry.shuai.pojo.User;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class UserControllerConsoumer {
@Autowired
private RestTemplate restTemplate;
@Autowired
private EurekaClient eurekaClient;
@GetMapping("/getUser")
public User getUser(){
String url = eurekaClient.getNextServerFromEureka("springcloud-privider", false).getHomePageUrl();
return restTemplate.getForObject(url+"getUserById",User.class);
}
//获取当前地址
@GetMapping("/getEurekeAddress")
public String serverAddress(){
InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("springcloud-privider", false);
return instanceInfo.getHomePageUrl();
}
}
8.测试
用户名和密码在yml文件中已经配置 user 123
调用测试
9.springcloud别的组件
ribbon:https://blog.csdn.net/oldshaui/article/details/86593504