大致思路:指定springcloud和spingboot版本,先搭建注册中心,再次搭建提供者和消费者的服务,注册到注册中心,配置fegin 再配置降级
1,创建maven 项目 ,pom.xml中 配置springcloud和springboot的版本,和jdk和字符集版本
<!--指定springcloud和springboot的版本要一致,才可也搭配使用-->
<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.SR1</spring-cloud.version>
<springboot.version>2.0.5.RELEASE</springboot.version>
</properties>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${springboot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
1.1配置 Eureka注册中心 配置 application.yml 文件 这里做两个注册中心集群
server:
port: 1020
eureka:
instance:
hostname: eureka1 #显示的注册中心的实列名字 不能一样
client:
registerWithEureka: false #是否要注册到eureka
fetchRegistry: false #表示是否从Eureka Server获取注册信息
serviceUrl:
defaultZone: http://eureka1:1020/eureka,http://eureka2:1030/eureka
spring:
application:
name: eureka1 #给注册中心取个名字 名字不能一样
profiles: e1 #用于run启动可以根据 此名字加载对应的配置
---
server:
port: 1030
eureka:
instance:
hostname: eureka1 #显示的注册中心的实列名字 不能一样
client:
registerWithEureka: false #是否要注册到eureka
fetchRegistry: false #表示是否从Eureka Server获取注册信息
serviceUrl:
defaultZone: http://eureka1:1020/eureka,http://eureka2:1030/eureka
spring:
application:
name: eureka2 #给注册中心取个名字 名字不能一样
profiles: e2 #用于run启动可以根据 此名字加载对应的配置
配置电脑的 host文件 路径 C:\Windows\System32\drivers\etc 加以下内容 ,启动的时候会自动以eureka1找到代理的地址127.0.0.1 IP
--本机地址 映射域名
127.0.0.1 eureka1
127.0.0.1 eureka2
配置 启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer//启用eureka注册服务
public class EurekaApp {
public static void main(String[] args) {
SpringApplication.run(EurekaApp.class);
}
}
2搭建 服务提供者。 导入依赖
<!--导入eureak客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--导入web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--链接数据库依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- spring-boot整合mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- 单元测试的包 -->
<!-- Spring测试要求junit为4.12及以上:SpringJUnit4ClassRunner requires JUnit 4.12 or higher-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--导入hytrix依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
配置提供者启动类
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient//启用eureka客户端
@MapperScan("cn.*.mapper")//扫描mapper
public class ServerApp {
public static void main(String[] args) {
SpringApplication.run(ServerApp.class);
}
}
配置提供者yml文件
server:
port: 8084
spring:
datasource:
url: jdbc:mysql://localhost:3306/itsource-course?useUnicode=true&characterEncoding=UTF-8
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
mybatis:
# mapper-locations: classpath*:cn/itsouce/mapper/*Mapper.xml
type-aliases-package: cn.itsource.domain
application:
name: course-server #不要有下划线
profiles: s1
eureka:
client:
service-url:
defaultZone: http://eureka1:1020/eureka,http://eureka2:1030/eureka
instance:
prefer-ip-address: true # 当调用getHostname获取实例的hostname时,返回ip而不是host名称
ip-address: 127.0.0.1 # 指定自己的ip信息,不指定的话会自己寻找
instance-id: course-server1
---
server:
port: 8083
spring:
application:
name: course-server #不要有下划线 集群的名字大家都要一样,因为调用的时候要通过名字找
profiles: s2
eureka:
client:
service-url:
defaultZone: http://eureka1:1020/eureka,http://eureka2:1030/eureka
instance:
prefer-ip-address: true # 当调用getHostname获取实例的hostname时,返回ip而不是host名称
ip-address: 127.0.0.1 # 指定自己的ip信息,不指定的话会自己寻找
instance-id: course-server2
logging: #日志打印
level:
root: info
cn:
wl: debug
3搭建服务消费者
@FeignClient(value = "course-server" ,fallbackFactory = MyBack.class)
@RequestMapping("/course")
public interface IFeginServer {
///此处代码从提供者复制
//MyBack为此接口的实现类
}
server:
port: 7071
spring:
datasource:
url: jdbc:mysql://localhost:3306/itsource-buy?useUnicode=true&characterEncoding=UTF-8
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
mybatis:
# mapper-locations: classpath*:cn/itsouce/mapper/*Mapper.xml
type-aliases-package: cn.itsource.domain #配置别名
application:
name: buy-server #不要有下划线
eureka:
client:
service-url:
defaultZone: http://eureka1:1020/eureka,http://eureka2:1030/eureka
instance:
prefer-ip-address: true # 当调用getHostname获取实例的hostname时,返回ip而不是host名称
ip-address: 127.0.0.1 # 指定自己的ip信息,不指定的话会自己寻找
instance-id: buy-server1
logging: #日志打印
level:
root: info
cn:
wl: debug
feign:
hystrix:
enabled: true #开启熔断支持
client:
config:
remote-service: #服务名,为course-server熔断
connectTimeout: 3000
readTimeout: 3000
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 3000
创建 服务消费者 启动类
package cn.itsouce;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient//启用eureka客户端
@MapperScan("cn.*.mapper")//扫描mapper
@EnableFeignClients//启用feign调用
public class BuyApp {
public static void main(String[] args) {
SpringApplication.run(BuyApp.class);
}
@Bean
@LoadBalanced
public RestTemplate getRestTemplat(){
System.out.println("在启动类加上@Bean注解,spring" +
"容器就加载此对象");
return new RestTemplate();
}
@Bean//注入随机调用
public IRule getRandom(){
System.out.println("返回值为IRule,运行的确是RandomRule对象。这里用多态。实现随机调用");
return new RandomRule();
}
}