首先是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.zenithink</groupId>
   <artifactId>zenithink-iot-front</artifactId> 
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>zenithink-iot-front</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.10.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <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>Edgware.RELEASE</spring-cloud.version>
   </properties>

   <dependencies>

      <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-autoconfigure</artifactId>
         <version>RELEASE</version>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>

      <dependency>
         <groupId>com.openiot.cloud</groupId>
         <artifactId>sdk</artifactId>
         <version>0.0.1</version>
         <classifier>window</classifier>
      </dependency>

      <dependency>
         <groupId>com.baidu.aip</groupId>
         <artifactId>java-sdk</artifactId>
         <version>4.1.1</version>
      </dependency>


      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>springloaded</artifactId>
         <version>1.2.7.RELEASE</version>
      </dependency>

      <dependency>
         <groupId>org.apache.httpcomponents</groupId>
         <artifactId>httpclient</artifactId>
         <version>4.5.5</version>
      </dependency>

      <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>fastjson</artifactId>
         <version>1.2.24</version>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>

      <dependency>
         <groupId>net.sf.json-lib</groupId>
         <artifactId>json-lib</artifactId>
         <version>2.4</version>
         <classifier>jdk15</classifier>
      </dependency>
      <!--引入负载均衡依赖-->
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-eureka</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-feign</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-zuul</artifactId>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>

   <!--依赖管理,用于管理spring-cloud的依赖,其中spring-cloud.version是版本号-->
   <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>

下面是application.properties中的一些配置

server.port=8008

spring.application.name=zcloud-gateway
//注册中心 注册与发现 eureka地址, zcloud-host-1 是host文件中配置的映射IP
eureka.client.service-url.defaultZone=http://zcloud-host-1:8000/eureka/,http://zcloud-host-2:8000/eureka/

hystrix.command.default.execution.timeout.enabled:false
//若请求路径为163就会转到163.com
zuul.routes.163.path=/163
zuul.routes.163.url=http://www.163.com/
//若请求路径满足响应规则转发到相应的服务提供方
#zuul.routes.api-a.path=/accept/**
zuul.routes.proxy.path=/proxy/**
zuul.routes.proxy.serviceId=zenithink-weixin-proxy

#zuul.routes.api-b.path=/weixinTokenController/**
zuul.routes.token.path=/token/**
zuul.routes.token.serviceId=zenithink-weixin-token

这里是后台入口类,也可以初始化自定义的过滤器,对一些请求进行拦截,比如验证用户请求是否携带有效的token,如果验证不通过就将请求转到登录服务上,通过了就转达服务提供方进行提供服务

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;

@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class ZcloudGatewayApplication {

   public static void main(String[] args) {
      SpringApplication.run(ZcloudGatewayApplication.class, args);
   }

   /**
    * 创建过滤器对象交给spring容器
    * @return 过滤器对象
    */
   /*@Bean
   public AccessFilter accessFilter(){
      return new AccessFilter();
   }*/
}

下面是服务消费方

 
@SpringBootApplication
@EnableDiscoveryClient //开启服务注册
@EnableFeignClients //开启负载均衡
public class ZenithinkWeixinProxyApplication {
   protected final static Logger logger = LoggerFactory.getLogger(ZenithinkWeixinProxyApplication.class);

   public static void main(String[] args) {

      SpringApplication.run(ZenithinkWeixinProxyApplication.class, args);
      logger.info("Application is sussess!");

   }


}
application.properties中的一些配置
server.port=8073
server.context-path=/

spring.profiles.active=dev
#向注册中心注册名称
spring.application.name=zenithink-weixin-proxy
#注册中心地址
eureka.client.service-url.defaultZone=http://zcloud-host-1:8000/eureka/,http://zcloud-host-2:8000/eureka/

下面是服务提供方

 
@SpringBootApplication
@EnableDiscoveryClient //开启向服务中心注册功能
public class ZenithinkWeixinTokenApplication {

   protected final static Logger logger = LoggerFactory.getLogger(ZenithinkWeixinTokenApplication.class);

   public static void main(String[] args) {
      SpringApplication.run(ZenithinkWeixinTokenApplication.class, args);
      logger.info("Application is sussess!");
   }
}

application.properties中的一些配置

 
server.port=8072
#向注册中心注册名称
spring.application.name=zenithink-weixin-token
#注册中心地址
eureka.client.service-url.defaultZone=http://zcloud-host-1:8000/eureka/,http://zcloud-host-2:8000/eureka/

这样就实现不同系统间的相互调用,跟DUBBO的使用差不多,只是springCloud和spring 整合更方便所以最近挺火的,希望我的代码可以帮到朋友们;



Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐