(转)Spring Cloud
Posted free_wings
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(转)Spring Cloud相关的知识,希望对你有一定的参考价值。
(二期)22、微服务框架spring cloud(一)
- 主要使用RestTemplate完成rest调用
开发工具集合,含有多个项目。
简化了分布式开发。
从上图可以看出Spring Cloud各个组件相互配合,合作支持了一套完整的微服务架构。
(心跳检测、健康检查、负载均衡等)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--actuator用于应用监控管理-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
#开启Eurka注册中心服务
@EnableEurekaServer
server:
port: 8761
eureka:
client:
# 表示是否将自己注册到Eureka Servcer。
register-with-eureka: false
# 表示是否从Eureka Server获取注册信息
fetchRegistry: false
serviceUrl:
# 设置Eureka Server交互的地址
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: eureka-server-1
# 开启注册服务发现功能
@EnableDiscoveryClient
# 开启Eureka注册中心服务功能
@EnableEurekaServer
自我保护的条件:
那这个阀值是多少呢?
关闭自我保护模式(生产上不建议)
(关闭了自我保护之后,然后需要几分钟才能把失效的客户端节点删掉)
注册中心节点之间相互注册即可实现高可用部署。
假如3个以上也一样,把其他的都注册到自己的server上面,用逗号隔开。客户端需要配置所有的Eureka server地址。
原理
注册中心地位
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
spring:
security:
user:
name: admin
password: admin
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// 关闭csrf
http.csrf().ignoringAntMatchers("/eureka/**");
super.configure(http);
}
}
eureka:
client:
# 设置Eureka Server交互的地址
service-url:
defaultZone: http://admin:[email protected]:8761/eureka/
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
@Bean
//为RestTemplate整合Ribbon的负载均衡能力
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
ORDER:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
@Autowired
private LoadBalancerClient loadBalancerClient;
LoadBalancerInterceptor
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
import com.itmuch.cloud.microserviceconsumermoviefeign.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
@GetMapping(value = "/{id}")
User findById(@PathVariable("id") Long id);
}
@Autowired
private UserFeignClient userFeignClient;
@GetMapping("/user/{id}")
public User findById(@PathVariable Long id) {
return this.userFeignClient.findById(id);
}
以上是关于(转)Spring Cloud的主要内容,如果未能解决你的问题,请参考以下文章
spring cloud中利用sidecar整合异构语言(转)
Microservices Reference Architecture - with Spring Boot, Spring Cloud and Netflix OSS--转