脚手架搭建项目,版本的降级
服务方:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR4</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--security校验-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
消费方:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR4</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</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>
<dependency>
<groupId>com.springcloud</groupId>
<artifactId>dm-common-model</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
服务方在主启动类上进行如下调整:
@SpringBootApplication
@EnableEurekaServer
public class DmUserProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DmUserProviderApplication.class, args);
}
}
消费方在主启动上进行如下调整:
@SpringBootApplication
@EnableDiscoveryClient
public class DmUserProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DmUserProviderApplication.class, args);
}
}
服务在提供方提供好之后,我们要通过FeignClient去调用服务
-
1、服务提供方提供的接口(假如是dm-user-provider):
@RequestMapping(value = "/loginForm",method = RequestMethod.POST) public Boolean formLogin(@RequestBody User user){ if("admin".equals(user.getUsername()) && "admin".equals(user.getPwd())) return true; return false; }
2、消费方去连接这个服务
//使用feignClient必须在pom文件引入相关依赖
//主启动类上要加入@EnableFeignClients注解
//当被调用者服务挂了之后,会自动进入fallback类中指定方法并返回给前端其中的内容
@FeignClient(name = "dm-user-provider",fallback = UserFeignFallback.class)
public interface UserFeignClient {
@RequestMapping(value = "/loginForm",method = RequestMethod.POST)
public Boolean formLogin(@RequestBody User user);
}
2、fallback 熔断机制
@Component//加入到spring组件
public class UserFeignFallback implements UserFeignClient {
@Override
public Boolean formLogin(User user) {
return "admin".equals(user.getUsername()) && "123456".equals(user.getPwd());
}
}
eurekaServer的application.yml
server:
port: 7776 # 指定server端口
eureka:
client:
fetch-registry: false # 是否从eureka服务端抓取其他服务
register-with-eureka: false # 是否把自己飞服务注册到eureka上
service-url:
defaultZone: http://localhost:7776/eureka # eureka服务器地址
security:
basic:
enabled: true # 是否启用security校验
user:
name: admin # 为安全校验设置用户名和密码
password: 123456
eurekaClient的application.yml
server:
port: 8081 # 指定server端口
spring:
application:
name: dm-user-consumer # 指定application名称
eureka:
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://admin:123456@localhost:7776/eureka # admin:123456这是服务端设置的安全校验用户名与密码
feign:
hystrix:
enabled: true # feign组件中包含了hystrix组件,是否启用熔断器
将其他项目的文件导入本地库中
-
1、将需要导入的项目通过idea进行install;
-
2、在需要调用本文件的地方先引入pom坐标;
<dependency> <groupId>com.springcloud</groupId> <artifactId>dm-common-model</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency>
-
3、点击File -> Project Structure -> Modules -> 在右侧点击Dependcies -> 点击下方的"+"号 -> JARS in directions -> 选择你要导入的jar文件 -> 勾选导入的jar -> Apply -> Close