目录
smart-medical-euraka-server 服务注册中心项目
smart-medical-provider 服务提供者服务
smart-medical-comsumer-ribbon 基于Ribbon 创建服务消费者
smart-medical-consumer-feign 基于Feign 服务消费者
SpringCloud 智慧医疗整体项目结构说明:
smart-medical 项目创建
1、IDEA 创建smart-medical maven项目
2、依次创建子项目:
smart-medical-euraka-server
smart-medical-provider
smart-medical-comsumer-ribbon
smart-medical-common
smart-medical-consumer-feign
smart-medical-zuul
各个子模块的创建参考截图进行创建
3、pom.xml 文件添加SpringBoot 版本和SpringCloud版本信息
<?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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
<modules>
<module>smart-medical-euraka-server</module>
<module>smart-medical-provider</module>
<module>smart-medical-comsumer-ribbon</module>
<module>smart-medical-common</module>
<module>smart-medical-consumer-feign</module>
<module>smart-medical-zuul</module>
</modules>
<groupId>org.example</groupId>
<artifactId>smart-medical</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring-cloud.version>Greenwich.SR5</spring-cloud.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>
</dependencies>
</dependencyManagement>
</project>
smart-medical-common 通用模块创建
项目结构截图:
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">
<parent>
<artifactId>smart-medical</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>smart-medical-common</artifactId>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
2、添加包名:com.zzg.model \com.zzg.service
package com.zzg.model;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
private String id;
private String account;
private String name;
private String passwd;
private Date createDt;
private String createBy;
private Date updateDt;
private String updateBy;
private Byte state;
private static final long serialVersionUID = 1L;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id == null ? null : id.trim();
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account == null ? null : account.trim();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd == null ? null : passwd.trim();
}
public Date getCreateDt() {
return createDt;
}
public void setCreateDt(Date createDt) {
this.createDt = createDt;
}
public String getCreateBy() {
return createBy;
}
public void setCreateBy(String createBy) {
this.createBy = createBy == null ? null : createBy.trim();
}
public Date getUpdateDt() {
return updateDt;
}
public void setUpdateDt(Date updateDt) {
this.updateDt = updateDt;
}
public String getUpdateBy() {
return updateBy;
}
public void setUpdateBy(String updateBy) {
this.updateBy = updateBy == null ? null : updateBy.trim();
}
public Byte getState() {
return state;
}
public void setState(Byte state) {
this.state = state;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", account=").append(account);
sb.append(", name=").append(name);
sb.append(", passwd=").append(passwd);
sb.append(", createDt=").append(createDt);
sb.append(", createBy=").append(createBy);
sb.append(", updateDt=").append(updateDt);
sb.append(", updateBy=").append(updateBy);
sb.append(", state=").append(state);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}
package com.zzg.service;
import com.zzg.model.User;
import java.util.List;
public interface UserService {
List<User> selectAll();
}
smart-medical-euraka-server 服务注册中心项目
1、pom.xml 添加eurake 服务jar 包依赖
<?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">
<parent>
<artifactId>smart-medical</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>smart-medical-euraka-server</artifactId>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<!-- 添加服务注册中心 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--web-->
<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>
</dependencies>
</project>
2、添加Eureka 服务注册中心的SpringBoot 程序入口。
package com.zzg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurakaApplication {
public static void main(String[] args) {
SpringApplication.run(EurakaApplication.class, args);
}
}
3、添加application.properties 配置文件
server.port=8081
# ???? ???????
eureka.instance.hostname=eureka-server
# eureka client ???????eureka client ??????
eureka.client.register-with-eureka=false
# ?????????eureka server ??eureka????
eureka.client.fetch-registry=false
#??????
eureka.server.enable-self-preservation=false
#??????????????????60??
eureka.server.eviction-interval-timer-in-ms=1000
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8081/eureka-server/
smart-medical-provider 服务提供者服务
项目截图:
1、pom.xml 添加eureka 客户端jar 、MyBatis、MySQL8、Alibaba Druid和智慧医疗通用项目模块依赖。
<?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">
<parent>
<artifactId>smart-medical</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>smart-medical-provider</artifactId>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--springboot 与 mybatis 集成 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 数据库连接池druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!--mysql 驱动程序 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<!--项目基础依赖-->
<dependency>
<groupId>org.example</groupId>
<artifactId>smart-medical-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
2、添加Provider SpringBoot程序入口、Mapper 映射接口、Mapper 映射xml和Controller层
package com.zzg;
import org.mybatis.spring.annotation.MapperScan;
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;
@SpringBootApplication
@MapperScan("com.zzg.mapper")
@EnableEurekaClient
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
package com.zzg.mapper;
import com.zzg.model.User;
import java.util.List;
public interface UserMapper {
int deleteByPrimaryKey(String id);
int insert(User record);
User selectByPrimaryKey(String id);
List<User> selectAll();
int updateByPrimaryKey(User record);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zzg.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.zzg.model.User">
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="account" jdbcType="VARCHAR" property="account" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="passwd" jdbcType="VARCHAR" property="passwd" />
<result column="create_dt" jdbcType="TIMESTAMP" property="createDt" />
<result column="create_by" jdbcType="VARCHAR" property="createBy" />
<result column="update_dt" jdbcType="TIMESTAMP" property="updateDt" />
<result column="update_by" jdbcType="VARCHAR" property="updateBy" />
<result column="state" jdbcType="TINYINT" property="state" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String">
delete from house_user
where id = #{id,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="com.zzg.model.User">
insert into house_user (id, account, `name`,
passwd, create_dt, create_by,
update_dt, update_by, `state`
)
values (#{id,jdbcType=VARCHAR}, #{account,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR},
#{passwd,jdbcType=VARCHAR}, #{createDt,jdbcType=TIMESTAMP}, #{createBy,jdbcType=VARCHAR},
#{updateDt,jdbcType=TIMESTAMP}, #{updateBy,jdbcType=VARCHAR}, #{state,jdbcType=TINYINT}
)
</insert>
<update id="updateByPrimaryKey" parameterType="com.zzg.model.User">
update house_user
set account = #{account,jdbcType=VARCHAR},
`name` = #{name,jdbcType=VARCHAR},
passwd = #{passwd,jdbcType=VARCHAR},
create_dt = #{createDt,jdbcType=TIMESTAMP},
create_by = #{createBy,jdbcType=VARCHAR},
update_dt = #{updateDt,jdbcType=TIMESTAMP},
update_by = #{updateBy,jdbcType=VARCHAR},
`state` = #{state,jdbcType=TINYINT}
where id = #{id,jdbcType=VARCHAR}
</update>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
select id, account, `name`, passwd, create_dt, create_by, update_dt, update_by, `state`
from house_user
where id = #{id,jdbcType=VARCHAR}
</select>
<select id="selectAll" resultMap="BaseResultMap">
select id, account, `name`, passwd, create_dt, create_by, update_dt, update_by, `state`
from house_user
</select>
</mapper>
package com.zzg.controller;
import com.zzg.mapper.UserMapper;
import com.zzg.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collection;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserMapper mapper;
@RequestMapping("/list")
public List<User> get() {
List<User> list = mapper.selectAll();
return list;
}
}
3、添加application.properties 配置文件
server.port=8084
server.servlet.context-path=/provider
# ??????????spring.application.name ????
spring.application.name=provider1
eureka.instance.instance-id=provider1
eureka.client.register-with-eureka=true
# ?eureka??????URL
eureka.client.service-url.defaultZone=http://127.0.0.1:8081/eureka/
# ?????????????30??
eureka.instance.lease-renewal-interval-in-seconds=5
# eureka server ?????????????????????client????90??
eureka.instance.lease-expiration-duration-in-seconds=3
# ???????
spring.datasource.url=jdbc:mysql://localhost:3306/house?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# druid ??
# ?????????????
spring.datasource.druid.initial-size=5
# ???????
spring.datasource.druid.max-active=30
# ???????
spring.datasource.druid.min-idle=5
# ????????????????
spring.datasource.druid.max-wait=60000
# ???????????????????????????????
spring.datasource.druid.time-between-eviction-runs-millis=60000
# ????????????????
spring.datasource.druid.min-evictable-idle-time-millis=300000
# ???????????sql??????????
spring.datasource.druid.validation-query=SELECT 1 FROM DUAL
# ?????true?????????????????????????????????timeBetweenEvictionRunsMillis???validationQuery?????????
spring.datasource.druid.test-while-idle=true
# ???????validationQuery?????????????????????
spring.datasource.druid.test-on-borrow=false
# ???????validationQuery?????????????????????
spring.datasource.druid.test-on-return=false
# ????preparedStatement????PSCache?PSCache???????????????????oracle??mysql??????
spring.datasource.druid.pool-prepared-statements=true
# ???PSCache???????0????0??poolPreparedStatements???????true?
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=50
# ?????????filters????????sql????
spring.datasource.druid.filters=stat,wall,slf4j
# ??connectProperties?????mergeSql????SQL??
spring.datasource.druid.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
# ????DruidDataSource?????
spring.datasource.druid.use-global-data-source-stat=true
##### WebStatFilter?? #######
#??StatFilter
spring.datasource.druid.web-stat-filter.enabled=true
#??????
spring.datasource.druid.web-stat-filter.url-pattern=/*
#????????url
spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
#??session????
spring.datasource.druid.web-stat-filter.session-stat-enable=true
#??sessionStatMaxCount?1000?
spring.datasource.druid.web-stat-filter.session-stat-max-count=1000
#spring.datasource.druid.web-stat-filter.principal-session-name=
#spring.datasource.druid.web-stat-filter.principal-cookie-name=
#spring.datasource.druid.web-stat-filter.profile-enable=
##### StatViewServlet?? #######
#?????????
spring.datasource.druid.stat-view-servlet.enabled=true
#?????????
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
#?? Reset All ??
spring.datasource.druid.stat-view-servlet.reset-enable=false
#???????
spring.datasource.druid.stat-view-servlet.login-username=admin
#??????
spring.datasource.druid.stat-view-servlet.login-password=123
#??????allow?????????????????
spring.datasource.druid.stat-view-servlet.allow=127.0.0.1
#????deny???allow????deny???????allow??????????
spring.datasource.druid.stat-view-servlet.deny=
# mybatis ??
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.zzg.model
# ????????
logging.level.com.zzg.mapper=debug
smart-medical-comsumer-ribbon 基于Ribbon 创建服务消费者
1、项目结构截图:
2、pom.xml 添加eureka 客户端jar 包依赖、hystrix 熔断器jar包依赖和智慧医疗通用模块依赖
<?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">
<parent>
<artifactId>smart-medical</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>smart-medical-comsumer-ribbon</artifactId>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--eureka-client-->
<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-netflix-hystrix</artifactId>
</dependency>
<!--项目基础依赖-->
<dependency>
<groupId>org.example</groupId>
<artifactId>smart-medical-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
2、添加hystrix 熔断器 配置、Ribbon 程序入口和服务消费者实现Controller
package com.zzg.config;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RibbonConfig {
@Bean
@LoadBalanced //负载均衡标识
RestTemplate restTemplate() {
return new RestTemplate();
}
}
package com.zzg.controller;
import com.zzg.model.User;
import com.zzg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/list")
public List<User> get() {
List<User> list = userService.selectAll();
return list;
}
}
package com.zzg.service;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zzg.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
RestTemplate template;
@Override
@HystrixCommand(fallbackMethod = "ServiceError")
public List<User> selectAll() {
String url ="http://provider1/provider/list";
return template.getForObject(url, List.class);
}
public List<User> ServiceError() {
System.out.println("熔断机制启动, Provider 服务提供者异常");
return null;
}
}
package com.zzg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
}
3、添加application.properties 配置文件
server.port=8085
server.servlet.context-path=/consumer-ribbon
spring.application.name=consumer-ribbon
eureka.client.service-url.defaultZone=http://127.0.0.1:8081/eureka/
smart-medical-consumer-feign 基于Feign 服务消费者
1、项目结构截图:
2、pom.xml 文件添加 添加eureka 客户端jar 包依赖、hystrix 熔断器jar包依赖和智慧医疗通用模块依赖
<?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">
<parent>
<artifactId>smart-medical</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>smart-medical-consumer-feign</artifactId>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--项目基础依赖-->
<dependency>
<groupId>org.example</groupId>
<artifactId>smart-medical-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
2、添加hystrix 熔断器 配置、Feign程序入口和服务消费者实现Controller
package com.zzg.controller;
import com.zzg.model.User;
import com.zzg.service.UserServiceFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserFeignController {
@Autowired
UserServiceFeign userServiceFeign;
@RequestMapping(value = "/list")
public List<User> selectAll() {
return userServiceFeign.selectAll();
}
}
package com.zzg.hystric;
import com.zzg.model.User;
import com.zzg.service.UserServiceFeign;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class UserServiceHystric implements UserServiceFeign {
@Override
public List<User> selectAll() {
System.out.println("熔断机制启动, Provider 服务提供者异常");
return null;
}
}
package com.zzg.service;
import com.zzg.hystric.UserServiceHystric;
import com.zzg.model.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@FeignClient(value = "provider1",fallback = UserServiceHystric.class)
@Service
public interface UserServiceFeign {
@GetMapping("/provider/list")
List<User> selectAll();
}
package com.zzg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
3、添加application.properties 配置文件
server.port=8086
spring.application.name=consumer-feign
eureka.client.service-url.defaultZone=http://127.0.0.1:8081/eureka/
# ??????
feign.hystrix.enabled=true
smart-medical-zuul 网关服务
1、项目截图:
2、添加zuul 网关服务程序入口
package com.zzg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
3、添加application.properties 配置文件
server.port=8087
spring.application.name=zuul
eureka.client.service-url.defaultZone=http://127.0.0.1:8081/eureka/
Github地址:git@github.com:zhouzhiwengang/SpringCloud-.git