1.项目调用关系
shopcart直接调用goods服务
user服务直接调用goods服务,user服务调用shopcart服务,shopchart调用goods服务
shopcart当作调方,也当作被调方
<modules>
<module>eureka</module>
<module>pojo</module>
<module>config</module>
<module>goods</module>
<module>user</module>
<module>shopcart</module>
<module>zuul</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<dependencyManagement>
<dependencies>
<!-- springcloud的pom依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.GIT配置环境
1.首先在github上面创建了一个文件夹config-repo用来存放配置文件
2.添加依赖(和本地依赖相同)
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
3.配置
server:
port: 9000
spring:
application:
name: config
#标注配置中心使用本地的方式
#profiles:
#active: native
cloud:
config:
server:
#native:
#search-locations: classpath:properties/
git:
uri: https://github.com/471604821/SpringBoot-config.git
search-paths: SpringBoot-config
default-label: main
eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka
4.配置自动刷新
谁调用,谁刷新
<!-- 开启配置文件刷新-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
#开启配置自动刷新
management:
endpoints:
web:
exposure:
include: refresh
实体类里面
@RefreshScope//开启配置刷新 用post请求http://localhost:8003/actuator/refresh刷新配置
3.实例JPA版
1.eureka
1.1 pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka服务端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
1.2 application.yml
server:
port: 8888
spring:
application:
name: eureka
eureka:
client:
#标注当前工程是否注册到eureka
register-with-eureka: false
#标注当前工程是否从eureka获取注册信息
fetch-registry: false
service-url:
defaultZone: http://localhost:8888/eureka
server:
#关闭自我保护机制
enable-self-preservation: false
1.3 启动类
@SpringBootApplication
//标注当前工程是eureka的服务端
@EnableEurekaServer
2.config
2.1 pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入config配置中心服务端的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- eureka 客户端的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.2 application.yml
server:
port: 9000
spring:
application:
name: config
#标注配置中心使用本地的方式
#profiles:
#active: native
cloud:
config:
server:
#native:
#search-locations: classpath:properties/
git:
uri: https://github.com/471604821/SpringBoot-config.git
search-paths: SpringBoot-config
default-label: main
eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka
2.3 启动类
@SpringBootApplication
//标注当前为eureka客户端
@EnableDiscoveryClient
//标注当前为Config服务端
@EnableConfigServer
3.pojo
3.1pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--JPA-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
3.2 pojo(日期2个注解)
@Data
@Entity
@Table(name = "goods")
public class Goods {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd")
private Date time;
private Double price;
}
@Data
public class BaseResp {
private Long code;
private Object message;
}
@Entity
@Table(name="tb_sys_user")
@Data
public class TrUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer userid;
@Column(name = "login_name")
private String loginName;
private String password;
}
4.goods
4.1 pom
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.wo</groupId>
<artifactId>pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- Hystrix 依赖 熔断降级-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- 添加springcloudconfig配置中心的客户端的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
4.2 bootstrap.yml
server:
port: 8001
spring:
application:
name: goods
cloud:
config:
name: me-config
profile: dev
discovery:
enabled: true
service-id: config
eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka
4.3 启动类
@SpringBootApplication
//标注为eureka,config的客户端
@EnableDiscoveryClient
//熔断
@EnableCircuitBreaker
4.4 controller(dao,service省略)
@RestController
public class GoodsController {
@Autowired
GoodsService goodsService;
//标注当前接口开启熔断,如果出现问题,走HystrixCommand中的findAllFallBack方法
@HystrixCommand(fallbackMethod = "findAllFallBack")
@RequestMapping("/findAll")
public List<Goods> findAll(){
//int i=1/0;
return goodsService.findAll();
}
//如果findAll出现问题,请求该方法,该方法返回值类型必须和findAll相同
public List<Goods> findAllFallBack(){
System.out.println("Goods熔断。。。。。");
return null;
}
5.shopcart
5.1 pom
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.wo</groupId>
<artifactId>pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- Hystrix 依赖 熔断降级-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- 添加springcloudconfig配置中心的客户端的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
5.2 bootstrap.yml
server:
port: 8002
spring:
application:
name: shopcart
cloud:
config:
name: me-config
profile: dev
discovery:
enabled: true
service-id: config
eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka
feign:
hystrix:
enabled: true #开启降级
5.3 启动类
//标注工程是启动类
@SpringBootApplication
//是eureka,config的客户端
@EnableDiscoveryClient
//开启熔断
@EnableCircuitBreaker
//开启降级
@EnableHystrix
//开始feign远程调用
@EnableFeignClients
5.4 client
//标注当前接口远程调用的某个服务
@FeignClient(serviceId = "goods",fallback = GoodsFallBackMethod.class)
public interface GoodsClient {
@RequestMapping("/findAll")
public List<Goods> findAll();
}
//交给Spring管理
@Component
public class GoodsFallBackMethod implements GoodsClient{
@Override
public List<Goods> findAll() {
System.out.println("当前方法调用不到,降级处理");
return null;
}
}
5.5 controller
@RestController
public class ShopCartController {
@Autowired
GoodsClient goodsClient;
//shopcart调用goods的findAll方法 shopcart->goods
@RequestMapping("/shopcart/findAll")
public List<Goods> findAll(){
return goodsClient.findAll();
}
//shopcart调用goods的findAll方法,user调用shopcart的findAndfind方法 user->shopcart->goods
//标注当前接口开启熔断,如果出现问题,走HystrixCommand中的findAllFallBack方法
@HystrixCommand(fallbackMethod = "findAndfindFallBack")
@RequestMapping("/findAndfind")
public String findAndfind(){
List<Goods> all = goodsClient.findAll();
int i=1/0;
System.out.println(all);
return "shopcart调goods成功!";
}
//如果findAndfind出现问题,请求该方法,该方法返回值类型必须和findAndfind相同
public String findAndfindFallBack(){
return "shopcart熔断。。。。。";
}
}
6.user
6.1pom
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- eureka 客户端的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- Hystrix 依赖 熔断降级-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- 添加springcloudconfig配置中心的客户端的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>com.wo</groupId>
<artifactId>pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- 开启配置文件刷新-->
<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>
</dependency>
6.2 bootstrap.yml
server:
port: 8003
spring:
application:
name: user
cloud:
config:
name: me-config
profile: dev
discovery:
enabled: true
service-id: config
feign:
hystrix:
enabled: true #开启降级
eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka
#开启配置自动刷新
management:
endpoints:
web:
exposure:
include: refresh
6.3 启动类
@SpringBootApplication
//开启降级
@EnableHystrix
//eureka,config的客户端
@EnableDiscoveryClient
//标注当前工程使用fegin来进行远程调用
@EnableFeignClients
6.4 client
1.goods相关
//标注当前接口远程调用的某个服务
@FeignClient(serviceId = "goods",fallback = GoodsFallBackMethod.class)
public interface GoodsClient {
@RequestMapping("/findAll")
public List<Goods> findAll();
}
//交给Spring管理
@Component
public class GoodsFallBackMethod implements GoodsClient{
@Override
public List<Goods> findAll() {
System.out.println("当前Goods的findAll方法调用不到,降级处理");
return null;
}
}
2.shopcart相关
//标注当前接口远程调用的某个服务
@FeignClient(serviceId = "shopcart",fallback = ShopCartFallBackMethod.class)
public interface ShopCartClient {
@RequestMapping("/findAndfind")
public String findAndfind();
}
//交给Spring管理
@Component
public class ShopCartFallBackMethod implements ShopCartClient{
@Override
public String findAndfind() {
System.out.println("当前Shopcart的findAndfind方法调用不到,降级处理");
return null;
}
}
6.5 controller
@RestController
@RefreshScope//开启配置刷新 用post请求http://localhost:8003/actuator/refresh刷新配置
public class UserController {
@Autowired
GoodsClient goodsClient;
@Autowired
ShopCartClient shopCartClient;
// @Value("${me.publickey}")
// private String key;
//user直接调用goods的findAll方法 user->goods
@RequestMapping("/user/findAll")
public List<Goods> findAll(){
// System.out.println("********************************"+key);//测试配置刷新
return goodsClient.findAll();
}
//shopcart调用goods的findAll方法,user调用shopcart的findAndfind方法 user->shopcart->goods
@RequestMapping("/user/findAndfind")
public String findAndfind(){
return shopCartClient.findAndfind()+"****user调用shopcart的findAndfind方法成功";
}
}