首先整一个父类的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>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.forezp</groupId>
<artifactId>sc-f-chapter1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>springCloud</name>
<description>Demo project for Spring Boot</description>
<!--子模块 -->
<modules>
<module>eureka-server</module>
<module>service-hi</module>
<module>service-ribbon</module>
<module>service-feign</module>
<module>user-centre</module>
<module>plug-centre</module>
<module>service-zuul</module>
<module>config-server</module>
<module>frameDemo-server</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>
<!-- <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> -->
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<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>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
建一个springBoot工程,启动Eureka服务注册中心:
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>com.forezp</groupId>
<artifactId>sc-f-chapter1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.forezp</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<!--Eureka的起步依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
配置文件代码:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eurka-server
启动类有变化:
package com.fanbaolin.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer // 启动一个服务注册中心
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
启动之后,服务注册中心就有了,剩下的就是将其他服务注册进去,进行接口间的相互调用
举例,其中的两个子模块:plug-centre和frameDemo-server之间的调用
建一个plug-centre的springBoot工程,注册的服务注册中心
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>com.forezp</groupId>
<artifactId>sc-f-chapter1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.fanbaolin</groupId>
<artifactId>plug-centre</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>plug-centre</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</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>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<!-- 支持热部署 使用 devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 通用mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.0.0</version>
</dependency>
<!--使用JWT生成token做登录验证 -->
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.4.0</version>
</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
配置文件为:
server:
port: 8767
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: plug-centre
mvc:
view:
prefix: /
suffix: .html
datasource:
name: mysql_test
type: com.alibaba.druid.pool.DruidDataSource
druid:
filters: stat
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8&autoReconnect=true
username: root
password: 123456
initial-size: 1
min-idle: 1
max-active: 20
max-wait: 60000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: SELECT 'x'
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: false
max-pool-prepared-statement-per-connection-size: 20
http:
encoding:
charset: UTF-8
enabled: true
force: true
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.fanbaolin.entity
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
启动类为:
package com.fanbaolin;
import org.springframework.beans.factory.annotation.Autowired;
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;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.fanbaolin.entity.TplugEntity;
import com.fanbaolin.model.PlugUserModel;
import com.fanbaolin.service.PlugCentreService;
import com.github.pagehelper.PageInfo;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@EnableEurekaClient//启动一个服务注册中心
@MapperScan("com.fanbaolin.mapper")
@EnableDiscoveryClient//向服务中心注册
@RestController
public class PlugCentreApplication {
@Autowired
PlugCentreService plugCenterService;
public static void main(String[] args) {
SpringApplication.run(PlugCentreApplication.class, args);
}
@RequestMapping("/p")
public String home(@RequestParam(value = "name", defaultValue = "范保林") String name) {
return "hi"+name+", 我是插件服务";
}
}
启动即可注册到服务注册中心,如图所示
然后再建一个frameDemo-server的springBoot工程:
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>com.forezp</groupId>
<artifactId>sc-f-chapter1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.fanbaolin</groupId>
<artifactId>frameDemo-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>frameDemo-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- 通用mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!-- 支持热部署 使用 devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件为:
server:
port: 8763
spring:
application:
name: frameDemo-server
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
# Feign是自带断路器的,在D版本的Spring Cloud之后,它没有默认打开。需要在配置文件中配置打开它
#feign.hystrix.enabled=true
feign:
hystrix:
enabled: false
启动类为:
package cn.bluethink;
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;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import cn.bluethink.frameDemo.MapFrame;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class FrameDemoServerApplication {
public static void main(String[] args) {
SpringApplication.run(FrameDemoServerApplication.class, args);
System.setProperty("java.awt.headless","false");
}
}
启动之后,这个项目也被注册到服务注册中心
这两个项目都到服务注册中心注册过后,使用FrameDemoServer去调Plug-Centre的接口,
如下实例:写一个service
package cn.bluethink;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import cn.bluethink.entity.ShapeEntity;
@FeignClient(value = "plug-centre") // fallback 错误回滚 断路器功能
public interface SchedualServiceHi {
// 定义一个feign接口,通过
// @FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了plug-centre服务的“/p”接口,代码如下
@RequestMapping(value = "/p", method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name") String name);
@RequestMapping(value = "/line/saveShape", method = RequestMethod.POST)
String saveShapeFromClientOne(@RequestBody ShapeEntity line);
@RequestMapping(value = "/line/openShape", method = RequestMethod.POST)
String openShapeFromClientOne(@RequestParam(value = "name")String name);
}
在controller层调用这个service即可
package cn.bluethink.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.bluethink.SchedualServiceHi;
import cn.bluethink.entity.ShapeEntity;
import cn.bluethink.frameDemo.MapFrame;
@RestController
public class FrameDemoController {
@Autowired
SchedualServiceHi schedualServiceHi;
@Autowired
HttpServletRequest request;
@RequestMapping(value = "/openShape")
public String openShape(@RequestBody ShapeEntity entity) {
// 返回值
String path = schedualServiceHi.openShapeFromClientOne(entity.getPicName());
MapFrame mapFrame = new MapFrame();
mapFrame.setPath(path);
mapFrame.openPic(path);
return path;
}
}
此时,在piug-center服务中有一个/line/openShape这样的接口,接收一个string类型的参数:
package com.fanbaolin.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.fanbaolin.service.LineService;
import cn.bluethink.entity.ShapeEntity;
import cn.bluethink.entity.UserEntity;
@RestController
@RequestMapping("/line")
public class LineController {
@Autowired
LineService lineService;
@RequestMapping("/openShape")
public String openShape(@RequestParam("name") String name) {
String path = lineService.openShape(name);
return path;
}
}
可以相互调用哦