Spring-Cloud
注册中心的三大体系 1.服务注册中心 2.服务消费者(发起请求的服务) 3.服务提供者(受理请求的服务)
1.创建注册中心:
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spring-cloud</groupId>
<artifactId>spring_cloud_eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.7.RELEASE</version>
</parent>
<!-- dependencyManagement dependencies区别 如果在dependencyManagement 设置了版本信息 那在dependencies就不需要设置版本啦 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
</project>

app.class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class ServerApp {
public static void main(String[] args) {
SpringApplication.run(ServerApp.class, args);
}
}
application.properties
#端口名称
server.port=6699
#服务名称
spring.application.name=master-service
#设置为false代表不注册自己服务
eureka.client.register-with-eureka=false
#由于注册中心的只测就是维护服务示例,他并不需要去检索服务,所以设置为false
eureka.client.fetch-registry=false
#地址
eureka.instance.hostname=127.0.0.1
#访问地址
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

2.创建服务端注册到注册中心
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.7.RELEASE</version>
</parent>

<dependencies>
<!-- 添加对Web的支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 要注册的需要引用 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 实现负载均衡引入客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<!-- 加入断路器依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
</dependencies>
<!--依赖管理,用于管理spring-cloud的依赖,其中Camden.SR3是版本号 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
app.class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class CommonApp {
public static void main(String[] args) {
SpringApplication.run(CommonApp.class, args);
}
}
applicationContext.xml
#服务名称
spring.application.name=server-common
#项目端口
server.port=6688
#设置服务器地址
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:6699/eureka/

3.Spring-Cloud服务端添加账号密码保护
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
application.properties
#添加服务端密码
security.user.password=cloudpwd
#添加服务端账号
security.user.name=clouduname
如果注册中心设置了账号密码 那么服务端在设置注册中心的链接上需要以如下格式进行填写
http://账号:密码@127.0.0.1:6699/eureka/


Spring-Cloud-Ribbon实现负载均衡
1.负载均衡的算法:默认是轮询,还有随机,还可自定义算法
2.实现过程
1.引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
2.为RestTemplate添加@loadeBalanced注解
@Bean
@LoadBalanced
public RestTemplate newRestTemplate(){
return new RestTemplate();
}
3.RestTemplate 常用方法
1.Get请求的接口
restTemplate.getForEntity(url,Type.class);

getForEntity的第一个参数为我要调用的服务的地址,这里我调用了服务提供者提供的/hello接口,注意这里是通过服务名调用而不是服务地址,如果写成服务地址就没法实现客户端负载均衡了。
getForEntity第二个参数String.class表示我希望返回的body类型是String

restTemplate.getForEntity("请求地址?name={name}", 返回值类型.class, Map<key,Value>);
Map<String, Object> maps = new HashMap<String,Object>();
maps.put("name", "value1");
ResponseEntity<List> responseEntity = restTemplate.getForEntity("http://server-order/queryAll?name={name}", List.class, maps);

getForEntity的第一个参数为我要调用的服务的地址,这里我调用了服务提供者提供的/hello接口,注意这里是通过服务名调用而不是服务地址,如果写成服务地址就没法实现客户端负载均衡了。
getForEntity第二个参数String.class表示我希望返回的body类型是String
maps 为请求链接内的参数值 ?name={name} 括号里面的name必须与map的key对应才能附上值

getForObject(url,Type.class/Object.class)
请求地址
返回值类型 也可以是一个实体对象


Spring-Cloud-Hystrix 容错保护
1.相当于当出现调用失败时执行默认的其他方法
2.实现过程
1.pom.xml
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
2.在启动类上增加
@EnableCircuitBreaker注解
3.在服务层指定如果请求失败执行的方法
@HystrixCommand(fallbackMethod = "exceptionMethod")exceptionMethod 方法名
第二种实现方法.配合 feign实现
@FeignClient(value = "server-order",fallback = OrderHyStrIxServer.class) 表名呗feign管理fallback = OrderHyStrIxServer.class表明要容错机制的类
@Service
@FeignClient(value = "server-order",fallback = OrderHyStrIxServer.class)
public interface OrderServer {
@RequestMapping(value = "queryAll")
public List<String> orderAllInfo(@RequestParam(value = "name",defaultValue = "defaultValue")String name);
}
实现类
@Component
public class OrderHyStrIxServer implements OrderServer {

public List<String> orderAllInfo(String name) {
// TODO Auto-generated method stub
List<String> strs = new ArrayList<String>();
strs.add("进入异常回调方法!!");
return strs;
}


Spring-Cloud-feign 声明式服务调用(相当于加上一层代理,用户调用代理即可)
1.实现过程
pom.xml:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
app.class
@EnableFeignClients
server
@Service
@FeignClient(name = "server-common")//代理的服务名
public interface FeignServer {
@RequestMapping(value = "SendHystrix", method = RequestMethod.GET)//代理的服务的接口
public List<String> feignSend();
}
Controller
@Autowired
private FeignServer feignServer;
@RequestMapping(value = "FeignSendOrder")
public List<String> feignSendOrder(){
System.out.println("调用feign ---> common --->Order");
return feignServer.feignSend();
}
application.properties
#服务名称
spring.application.name=server-feign
#项目端口
server.port=6644
#注册中心地址
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:6699/eureka/
#开启hystrix声明事服务
feign.hystrix.enabled=true