一、创建服务注册中心
我们采用eureka作为服务的注册和发现组件
1.1 创建一个父工程
1.1.1首先创建一个工程


1.1.2这里创建一个springcloudparent的module模块

1.1.3 修改pom文件
<?xml version="1.0" encoding="UTF-8"?>
<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>com.zhiyou100</groupId>
<artifactId>springcloudparent</artifactId>
<version>1.0-SNAPSHOT</version>
<!--继承springboot-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<!--定义编码格式-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!--jdk版本-->
<java.version>1.8</java.version>
<!--springcloud版本-->
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<!--引入Springboot测试模块-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--引入Springcloud测试模块-->
<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>
<!--Springboot的一个maven插件,可写可不写-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
需求:创建两个module工程,一个作为服务的注册中心,Eureka Server,一个作为服务的提供者Eureka Client
1.2、创建Eureka Server
1.2.1 创建步骤





1.2.2修改pom.xml文件
修改为依赖父工程即可,然后再引入eureka-server依赖
<?xml version="1.0" encoding="UTF-8"?>
<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>com.zhiyou100</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<!--这里我们依赖自己定义的父工程,不用默认的父工程-->
<parent>
<groupId>com.zhiyou100</groupId>
<artifactId>springcloudparent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
1.2.3启动服务注册中心
只需要在启动类上添加@EnableEurekaService注解即可
1.2.4 修改配置文件application.properties

#端口
server.port=8000
#application.name
spring.application.name=eureka-server
#通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server.
eureka.instance.hostname=localhost
#registerWithEureka表示是否注册自身到eureka服务器
eureka.client.register-with-eureka=false
#fetchRegistry表示是否从eureka服务器获取注册信息。
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
1.2.5启动服务
登录localhost:8000
这里面的8000是配置文件中设置的端口
二、创建服务提供者
我们开发项目需要开发系统对外提供服务,而且服务需要注册到注册中心
2.1、创建服务
创建项目与创建注册中心一样(略)
2.2修改pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>com.zhiyou100</groupId>
<artifactId>springcloudparent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.zhiyou100</groupId>
<artifactId>server-hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>server-hello</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2.3声明服务
我们需要在启动类上使用@EnableEurekaClient注解说明该项目是一个EurekaClient,可以对外提供服务
2.4测试服务
写一个简单的controller层
2.5修改配置文件
#端口
server.port=8001
#spring.application.name非常重要,服务直接的调用是根据这个name值来相互调用的
spring.application.name=server-hello
#提交到这个注册中心
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
2.6启动


三、服务的消费者(ribbon)
ribbon是一个负载均衡客户端,可以很好的控制http和tcp的一些行为。Feign默认集成了ribbon。
3.1准备工作
启动eureka-server 工程;启动service-hello工程,它的端口为8001;将service-hello的配置文件的端口改为8002,并启动,这时你会发现:service-hello在eureka-server注册了2个实例,这就相当于一个小的集群。
说明:idea默认一个工程只能启动一个实例,如果一个工程要启动多个实例,需要修改一下idea的默认配置(修改如下)

3.2创建服务消费者
创建项目和之前一样,创建一个module,项目名:service-ribbon
3.2.1 修改pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>com.zhiyou100</groupId>
<artifactId>springcloudparent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.zhiyou100</groupId>
<artifactId>service-ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service-ribbon</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</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-ribbon</artifactId>
</dependency>
</dependencies>
</project>
3.2.2 修改application.properties文件
#端口
server.port=8010
#spring.application.name非常重要,服务直接的调用是根据这个name值来相互调用的
spring.application.name=server-ribbon
#提交到这个注册中心
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
3.2.3启动类(ServiceRibbonApplication)
package com.zhiyou100.serviceribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
//通过该↓注解向注册中心注册,并向程序的ioc容器中注入一个bean
@EnableDiscoveryClient
//服务的提供者
@EnableEurekaClient
//扫描包
@ComponentScan("com.zhiyou100")
public class ServiceRibbonApplication {
@Bean
//表名restTemplate开启负载均衡功能
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
}
service层
package com.zhiyou100.service;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@Service
public class HelloService {
@Resource
private RestTemplate restTemplate;
public String helloServer(){
//调用server-hello中的方法
return restTemplate.getForObject("http://server-hello/hello.do",String.class);
}
}
controller层
package com.zhiyou100.controller;
import com.zhiyou100.service.HelloService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class HelloController {
@Resource
private HelloService service;
@RequestMapping("helloservice.do")
public String hello(){
return service.helloServer();
}
}
在controller层,return的是service层的方法,而service层调用的是server-hello中的方法hello.do,最后返回到controller
3.2.4 启动并访问
启动两个server-hello时注意修改配置文件的端口号,和返回结果+1(以便于区分),成功结果页面会两个来回跳转,达到负载均衡效果

四、feign
Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。
简而言之:
Feign 采用的是基于接口的注解
Feign 整合了ribbon,具有负载均衡的能力
整合了Hystrix,具有熔断的能力
4.1配置pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>com.zhiyou100</groupId>
<artifactId>springcloud-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.zhiyou100</groupId>
<artifactId>server-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>server-feign</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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>
</dependencies>
</project>
4.2配置文件application.properties文件
#端口
server.port=8011
#spring.application.name非常重要,服务直接的调用是根据这个name值来相互调用的
spring.application.name=server-feign
#提交到这个注册中心
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
4.3启动类
在类上添加@EnableFeignClients注解,开启Feign功能
package com.zhiyou100.servicefeign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
//提供服务
@EnableEurekaClient
//向注册中心注册
@EnableDiscoveryClient
//开启feign功能
@EnableFeignClients(basePackages = "com.zhiyou100.service")
@ComponentScan(basePackages = "com.zhiyou100")
public class ServiceFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceFeignApplication.class, args);
}
}
4.4service接口
package com.zhiyou100.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient("server-hello")
public interface HelloService {
//调用hello.do服务
@RequestMapping("hello.do")
String hello(String name);
}
4.5controller层
package com.zhiyou100;
import com.zhiyou100.service.HelloService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class controller {
@Resource
private HelloService hservice;
@RequestMapping("helloo.do")
public String helloController(@RequestParam(defaultValue = "王鑫") String name){
return hservice.hello(name);
}
}
启动
这里ribbon没有关(懒得关了)
总结:feign相对与ribbon简单,方便,只需要调接口即可,没有那些麻烦事