首先参考以下几个文档顺序完成相应的搭建,注意,除了eureka作为服务server引入jar其它全部作为client引入服务注册和发现的依赖,而对于config依赖只有配置中心的服务作为server,其它应用服务作为client
- 项目搭建参考文档:https://www.jianshu.com/p/65dfbb55779b
- 项目启动参考文档:https://blog.csdn.net/singlekingdom/article/details/91378831
- 服务注册发现保证服务之间不用频繁修改地址:https://www.cnblogs.com/luchangyou/p/7576552.html
- 配置中心:https://blog.51cto.com/12965378/2403913
- rabbitMQ安装(配置的实时刷新):https://www.cnblogs.com/sam-uncle/p/9050242.html 到计算管理中心启动rabbitmq服务 到\rabbitmq_server-3.7.9\sbin 下执行命令启动管理页面 rabbitmq-plugins enable rabbitmq_management
执行完以上步骤后的工程结构如下:
configserver提供配置的配置中心并能够通过rabbitmq广播给各个服务刷新配置
eureka提供给各服务的服务注册和发现,便于服务之间调用不用知道ip端口,只需要知道服务名称即可
springcloudapi在此仅仅是提供服务之间交互的模型对象
springcloudconsumeruser8000服务调用方
springcloudprovideruser8001服务被调用方
以上描述的均是springcloud的子工程
1、父工程依赖如下
<?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.cn.springcloud</groupId>
<artifactId>springcloud</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>springcloudapi</module>
<module>springcloudprovideruser8001</module>
<module>springcloudconsumeruser8000</module>
<module>Eureka</module>
<module>configserver</module>
</modules>
<packaging>pom</packaging>
<properties>
<project.version>1.0-SNAPSHOT</project.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<lombok.version>1.16.18</lombok.version>
<log4j.version>1.2.17</log4j.version>
<junit.version>4.12</junit.version>
<mysql.version>6.0.6</mysql.version>
<druid.version>1.1.10</druid.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- mysql jdbc 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.8.2</version>
<executions>
<execution>
<id>default-site</id>
<phase>site</phase>
<goals>
<goal>site</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--被依赖的公共模块必须加上这个,打包之后启动,选择后面带EXE的启动,依赖该模块的其他模块就不会因为找不到依赖而打包出错-->
<classifier>exe</classifier>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、Eureka工程结构
Eureka依赖
<?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>springcloud</artifactId>
<groupId>com.cn.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Eureka</artifactId>
<dependencies>
<!-- 引入API -->
<dependency>
<groupId>com.cn.springcloud</groupId>
<artifactId>springcloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<!-- web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.3</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
</dependencies>
</project>
application.yml文件配置如下
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761/eureka
eureka启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main( String[] args ) {
SpringApplication.run(EurekaApplication.class, args);
}
}
3、configserver配置管理服务
依赖
<?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>springcloud</artifactId>
<groupId>com.cn.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-server</artifactId>
<dependencies>
<!-- 引入API -->
<dependency>
<groupId>com.cn.springcloud</groupId>
<artifactId>springcloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<!-- web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--将微服务注册到Eureka Service-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--消息服务总线,动态更新配置信息-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.3</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.6</version>
</dependency>
</dependencies>
</project>
启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
dev提供给其它服务用的配置 config-server-dev.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka
instance:
appname: 111网关服务1112修改了哦11166666ask
prefer-ip-address: true
lease-renewal-interval-in-seconds: 2
lease-expiration-duration-in-seconds: 5
application.yml文件
server:
port: 8888
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://user:pass@localhost:8761/eureka
spring:
application:
name: config-server
cloud:
config:
server:
native:
# 必须写绝对路径,否则可能导致bus总线无法及时通过 /actuator/bus-refresh post请求及时刷新配置并通知所有的服务 (Content-Type application/json;charset=utf-8 此值必须设置)
search-locations: E:/svn/springcloud/configserver/src/main/resources/config
bus:
enabled: true
trace:
enabled: true
profiles:
active: native
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
management:
endpoints:
web:
exposure:
include: bus-refresh
配置完之后启动服务即可
如果想要刷新配置,直接修改config-server-dev.yml文件然后调用配置中心接口/actuator/bus-refresh即可刷新所有服务的本地配置(所有服务在启动的时候会优先从远处获取配置然后保存到本地,后续都用本地的配置,所以需要rabbitmq通知每个服务刷新本地配置),调用方式如下(我采用的是idea自带的restful远程调用根据类似于postman):
当刷新配置的时候其它服务会打印出如下日志,表明服务总线配置ok的
4、spring cloud api
依赖
<?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>springcloud</artifactId>
<groupId>com.cn.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-api</artifactId>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
</dependencies>
</project>
5、spring cloud调用方
依赖
<?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>springcloud</artifactId>
<groupId>com.cn.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-consumer-user-8000</artifactId>
<dependencies>
<!-- 引入API -->
<dependency>
<groupId>com.cn.springcloud</groupId>
<artifactId>springcloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<!-- web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--将微服务注册到Eureka Service-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.3</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
</project>
@Configuration
public class ConfigBean {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
@RestController
@RefreshScope
public class UserController_Consumer {
/**
* 服务提供方地址
*/
private static final String url = "http://springcloud-provider/";
/**
* 从配置中心获取配置
*/
@Value("${eureka.client.instance.appname}")
private String config_instance_name;
/**
* RestTemplate 提供了多种便捷访问远程HTTP服务的方法
* 是一种简单便捷的访问restful服务模版类,是Spring提供的用于访问
* Rest服务的客户端模版工具集
*/
@Resource
private RestTemplate restTemplate;
@RequestMapping(value = "/consumer/user/all")
@ResponseBody
public List getUserAll() {
return restTemplate.getForObject(url.concat("user/all"), List.class);
}
@RequestMapping(value = "/consumer/user/{id}")
@ResponseBody
public ResponseEntity<User> getUserById(@PathVariable("id") Long id) {
return restTemplate.getForEntity(url.concat("user/").concat(id.toString()), User.class);
}
@RequestMapping(value = "/consumer/getConfig")
@ResponseBody
public String getConfig( ) {
return config_instance_name ;
}
}
@EnableEurekaClient
@SpringBootApplication
@EnableDiscoveryClient
public class Application_consumer_8000 {
public static void main(String[] args) {
SpringApplication.run(Application_consumer_8000.class, args);
}
@Bean
@LoadBalanced//在注册中心里进行查找微服务,负载均衡
public RestTemplate restTemplate(){
RestTemplate restTemplate=new RestTemplate();
return restTemplate;
}
}
application.yml文件
server:
port: 8200
# 下面这个参数是为解决问题而新增的
tomcat:
max-http-header-size: 81920
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://user:pass@localhost:8761/eureka
spring:
application:
name: springcloud-customer
cloud:
config:
name: config-server
profile: dev
uri: http://localhost:8888
discovery:
enabled: true
service-id: config-server
fail-fast: true
bus:
trace:
enabled: true
management:
endpoints:
web:
exposure:
include: refresh
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
5、springcloud被调用方
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">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.cn.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-provider-user-8001</artifactId>
<dependencies>
<!-- 引入API -->
<dependency>
<groupId>com.cn.springcloud</groupId>
<artifactId>springcloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!-- 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<!-- 数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<!-- springBoot 集成 mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<!--<scope>runtime</scope>-->
</dependency>
<!--将微服务注册到Eureka Service-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.3</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.6</version>
</dependency>
</dependencies>
</project>
@RestController
@RequestMapping
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/user/all")
@ResponseBody
public List<User> getAllUser(){
return userService.findAll();
}
@RequestMapping(value = "/user/{id}")
@ResponseBody
public User getUserById(@PathVariable("id") Long id){
return userService.findById(id);
}
}
@Mapper
public interface UserDao {
@Select("select id,user_name as userName,db_source as dbSource,phone,email,pwd from t_user")
/**
*
*/
List<User> findAll();
@Select("select id,user_name as userName,db_source as dbSource,phone,email,pwd from t_user where id = #{id}")
/**
*
*/
User findById(Long id);
}
@Service
public class UserService {
@Autowired
private UserDao userDao;
public User findById(Long id) {
return userDao.findById(id);
}
public List<User> findAll() {
return userDao.findAll();
}
}
@EnableEurekaClient
@SpringBootApplication
@MapperScan(basePackages = "com.cn.springcloud")
public class Application_provider_8001 extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application_provider_8001.class, args);
}
}
application.yml文件(在此用的postgresql数据库,所以此服务需要引入此依赖)
xserver:
port: 8181
mybatis:
type-aliases-package: com.cn.springcloudapi.entites #所以entity别名类所在路径
spring:
application:
name: springcloud-provider
datasource:
url: jdbc:postgresql://localhost:5433/postgres #数据库连接
username: postgres
password: ***
driver-class-name: org.postgresql.Driver
dbcp2:
min-idle: 5 #数据库连接池的最小维持连接数
initial-size: 5 #初始化连接数
max-total: 5 #最大连接数
max-wait-millis: 200 #等待连接获取的最大超时时间
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://user:pass@localhost:8761/eureka
6、rabbitmq安装,参考以上文档即可