社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
1.配置本地环境 host-->例如:192.168.1.100 jmenv.tbsite.net
2.idea工具开发---》创建Springboot项目
分模块开发---》Spring Initializr--->一直点下一步---》完成
作为主框架不需要配置任何jar---》修改打包方式为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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/>
</parent>
<groupId>top.lolcl</groupId>
<artifactId>hsfdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>hsfdemo</name>
<description>Demo project for Spring Boot</description>
<modules>
<module>hsfdemo-api</module>
<module>hsfconsumer</module>
<module>hsfprovide</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
</project>
创建服务API接口Module--->注意创建springboot选打包方式为war
如果忘记---》修改打包方式
接口对应的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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>top.lolcl</groupId>
<artifactId>hsfdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>top.lolcl</groupId>
<artifactId>hsfdemo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>hsfdemo-api</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<!-- 简化代码-->
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<!--MybatisPlus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!-- freemarker 模板引擎 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
编写用户接口
package top.lolcl.hsfdemoapi.user.service;
import top.lolcl.hsfdemoapi.user.entity.User;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* <p>
* 服务类
* </p>
*
* @author jobob
* @since 2019-02-11
*/
public interface IUserService {
String echo(String string);
}
application.properties配置文件
#访问相关配置
server.port=8099
server.tomcat.uri-encoding=utf-8
#项目访问名称,如果不配置直接访问bean就可用
server.servlet.context-path=/demo
#数据库连接
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
#spring boot视图配置
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp
logging.level.cn.piesat.mapper=debug
#设定ftl文件路径
spring.freemarker.template-loader-path=classpath:/templates
#设定静态文件路径,js,css等
spring.mvc.static-path-pattern=/static/**
#设置mapper文件的位置
mybatis-plus.mapper-locations=classpath:mapper.user/*.xml
mybatis-plus.type-aliases-package=top.lolcl.hsfdemoapi.user.entity;
#mybatis.mapper-locations=classpath*:com/example/demo/mapper/*.xml
#mybatis.mapperLocations=classpath*:com/example/demo/mapper/*.xml
#mybatis.type-aliases-package=com.example.demo.bean
#热部署生效
spring.devtools.restart.enabled: true
#设置重启的目录
#spring.devtools.restart.additional-paths: src/main/java
#classpath目录下的WEB-INF文件夹内容修改不重启
spring.devtools.restart.exclude: WEB-INF/**
创建服务提供者Module---》注意创建springboot选打包方式为war
提供方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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>top.lolcl</groupId>
<artifactId>hsfdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>top.lolcl</groupId>
<artifactId>hsfprovide</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>hsfprovide</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>top.lolcl</groupId>
<artifactId>hsfdemo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sgp.travel</groupId>
<artifactId>travel-baseapp-edas</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>pandora-hsf-spring-boot-autoconfigure</artifactId>
<version>2017-10-stable</version>
</dependency>
<dependency>
<groupId>com.alibaba.middleware</groupId>
<artifactId>sdk</artifactId>
<version>999-not-exist-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<scope>provided</scope>
</dependency>
<!-- springboot依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<!-- 简化代码-->
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<!--MybatisPlus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!-- freemarker 模板引擎 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<!-- 整合redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- poi插件 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>
<!-- pagehelper分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.7</version>
</dependency>
<!-- JSON lib 开发包 以及它的依赖包 -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.api</groupId>
<artifactId>travel-ecarcenter-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork><!--r热加载用-->
<warName>demo</warName>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>travelmaven</id>
<url>http://39.104.15.87/nexus/content/groups/public</url>
</repository>
</repositories>
</project>
实现接口类
package top.lolcl.hsfprovide.user.service;
//import com.taobao.hsf.app.spring.util.annotation.HSFProvider;
import top.lolcl.hsfdemoapi.user.service.IUserService;
import com.alibaba.boot.hsf.annotation.HSFProvider;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.taobao.hsf.app.spring.util.annotation.HSFProvider;
//import com.taobao.hsf.app.spring.util.annotation.HSFProvider;
import org.springframework.stereotype.Service;
import top.lolcl.hsfdemoapi.user.entity.User;
import top.lolcl.hsfdemoapi.user.service.IUserService;
/**
* <p>
* 服务实现类
* </p>
*
* @author jobob
* @since 2019-02-11
*/
@HSFProvider(serviceInterface = IUserService.class, serviceGroup = "HSF", serviceVersion = "1.0.0", clientTimeout = 3000)
public class UserServiceImpl implements IUserService {
@Override
public String echo(String string) {
return string+"****额外的信息。。。。。。";
}
}
application.properties配置文件
spring.application.name=hsf-provider
server.port=18081
spring.hsf.version=1.0.0
spring.hsf.timeout=3000
#访问相关配置
server.tomcat.uri-encoding=utf-8
#项目访问名称,如果不配置直接访问bean就可用
server.servlet.context-path=/demo
#数据库连接
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
#spring boot视图配置
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp
logging.level.cn.piesat.mapper=debug
#设定ftl文件路径
spring.freemarker.template-loader-path=classpath:/templates
#设定静态文件路径,js,css等
spring.mvc.static-path-pattern=/static/**
#设置mapper文件的位置
#mybatis-plus.mapper-locations=classpath:mapper.user/*.xml
#mybatis-plus.type-aliases-package=top.lolcl.hsfdemoapi.user.entity;
#mybatis.mapper-locations=classpath*:com/example/demo/mapper/*.xml
#mybatis.mapperLocations=classpath*:com/example/demo/mapper/*.xml
#mybatis.type-aliases-package=com.example.demo.bean
#热部署生效
spring.devtools.restart.enabled: true
#设置重启的目录
#spring.devtools.restart.additional-paths: src/main/java
#classpath目录下的WEB-INF文件夹内容修改不重启
spring.devtools.restart.exclude: WEB-INF/**
注意如果扫描不到controller/serivice包---》需要到app主函数下配置扫描注解
package top.lolcl.hsfprovide;
import javafx.application.Application;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication(scanBasePackages = {"top.lolcl"})
@EnableCaching
@ComponentScan("top.lolcl.*")
public class HsfprovideApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(HsfprovideApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder application) {
return application.sources(HsfprovideApplication.class);
}
}
注意虽然是springboot项目但需要额外配置tomcat-->淘宝的tomcat
对应war包
需要先启动EDAS服务注册中心--》再启动提供者tomcat--->可以再EDAS配置中心网页端看到提供者servie接口--》说明发布成功
如果不启动EDAS注册服务中心直接启动提供者的tomcat-,就会报错。
创建服务消费者Module---》注意创建springboot选打包方式为war
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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>top.lolcl</groupId>
<artifactId>hsfdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>top.lolcl</groupId>
<artifactId>hsfconsumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>hsfconsumer</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>top.lolcl</groupId>
<artifactId>hsfdemo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sgp.travel</groupId>
<artifactId>travel-baseapp-edas</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>pandora-hsf-spring-boot-autoconfigure</artifactId>
<version>2017-10-stable</version>
</dependency>
<dependency>
<groupId>com.alibaba.middleware</groupId>
<artifactId>sdk</artifactId>
<version>999-not-exist-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<scope>provided</scope>
</dependency>
<!-- springboot依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<!-- 简化代码-->
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<!--MybatisPlus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!-- freemarker 模板引擎 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<!-- 整合redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- poi插件 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>
<!-- pagehelper分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.7</version>
</dependency>
<!-- JSON lib 开发包 以及它的依赖包 -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.api</groupId>
<artifactId>travel-ecarcenter-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork><!--r热加载用-->
<warName>demo</warName>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>travelmaven</id>
<url>http://39.104.15.87/nexus/content/groups/public</url>
</repository>
</repositories>
</project>
测试是否可以调用接口
package top.lolcl.hsfconsumer.user.controller;
import com.alibaba.boot.hsf.annotation.HSFConsumer;
//import com.taobao.hsf.app.spring.util.annotation.HSFConsumer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.lolcl.hsfdemoapi.user.service.IUserService;
@RestController
@RequestMapping("/abc")
public class UserController {
//consumerBeanName=, includeRouters=[], enableRRLoadLoadbalancer=, proxyStyle=jdk, serviceGroup=HSF, clientTimeout=3000, includeFilters=[], generic=false, methodSpecials=[], serviceVersion=1.0.0, secureKey=, addressWaitTime=3000, configServerCenters=[], futureMethods=[]
@HSFConsumer
IUserService service;
@RequestMapping("/")
public String index(){
String str = service.echo("lalalalalla");
return "测试一下----"+str;
}
}
application.properties配置文件
spring.application.name=hsf-consumer
server.port=18082
spring.hsf.version=1.0.0
spring.hsf.timeout=1000
#访问相关配置
server.tomcat.uri-encoding=utf-8
#项目访问名称,如果不配置直接访问bean就可用
server.servlet.context-path=/demo
#数据库连接
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
#spring boot视图配置
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp
logging.level.cn.piesat.mapper=debug
#设定ftl文件路径
spring.freemarker.template-loader-path=classpath:/templates
#设定静态文件路径,js,css等
spring.mvc.static-path-pattern=/static/**
#设置mapper文件的位置
#mybatis-plus.mapper-locations=classpath:mapper.user/*.xml
#mybatis-plus.type-aliases-package=top.lolcl.hsfdemoapi.user.entity;
#mybatis.mapper-locations=classpath*:com/example/demo/mapper/*.xml
#mybatis.mapperLocations=classpath*:com/example/demo/mapper/*.xml
#mybatis.type-aliases-package=com.example.demo.bean
#热部署生效
spring.devtools.restart.enabled: true
#设置重启的目录
#spring.devtools.restart.additional-paths: src/main/java
#classpath目录下的WEB-INF文件夹内容修改不重启
spring.devtools.restart.exclude: WEB-INF/**
注意如果扫描不到controller---》需要到app主函数下配置扫描注解
package top.lolcl.hsfconsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication(scanBasePackages = {"top.lolcl"})
@EnableCaching
@ComponentScan("top.lolcl.*")
public class HsfconsumerApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(HsfconsumerApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder application) {
return application.sources(HsfconsumerApplication.class);
}
}
配置服务消费者tomcat--->不使用springboot自带tomcat,使用hsf提供的淘宝tomcat,及配置jvm环境运行参数
同服务提供者
运行消费者服务端---》最终可以看到调用到注册中心提供数据--》完成
源码下载地址:https://download.csdn.net/download/weixin_39209728/10954263
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!