<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.0modelVersion>
<packaging>pompackaging>
<modules>
<module>../spring-cloud02-apimodule>
<module>../spring-cloud-03-providermodule>
<module>../spring-cloud04-consumermodule>
<module>../springEurekaServermodule>
<module>../springEurekaServer02module>
<module>../spring-cloud-03-provider02module>
modules>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.7.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<junit.version>4.12junit.version>
<spring-cloud.version>Finchley.SR2spring-cloud.version>
properties>
<groupId>cn.liyanggroupId>
<artifactId>spring-cloud-01artifactId>
<version>1.0-SNAPSHOTversion>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.2version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.12version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.13version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit.version}version>
<scope>testscope>
dependency>
dependencies>
dependencyManagement>
<build>
<finalName>spring-cloud-01finalName>
<resources>
<resource>
<directory>src/main/resourcesdirectory>
<filtering>truefiltering>
resource>
resources>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-resources-pluginartifactId>
<configuration>
<delimiters>
<delimit>$delimit>
delimiters>
configuration>
plugin>
plugins>
build>project>
package cn.liyang.springCloud.entities;
import java.io.Serializable;
/**
* @author liyang
* @date 2019/8/27 10:52
* @description:
*/
public class Product implements Serializable {
private Long pid;
private String productName;
private String dbSource;
public Product(String productName) {
this.productName = productName;
}
public Product(Long pid, String productName, String dbSource) {
this.pid = pid;
this.productName = productName;
this.dbSource = dbSource;
}
public Product() {
}
public Long getPid() {
return pid;
}
public void setPid(Long pid) {
this.pid = pid;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getDbSource() {
return dbSource;
}
public void setDbSource(String dbSource) {
this.dbSource = dbSource;
}
}
pom.xml
<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>spring-cloud-01artifactId>
<groupId>cn.liyanggroupId>
<version>1.0-SNAPSHOTversion>
<relativePath>../springcloud01/pom.xmlrelativePath>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>spring-cloud-03-providerartifactId>
<dependencies>
<dependency>
<groupId>cn.liyanggroupId>
<artifactId>spring-cloud-02-apiartifactId>
<version>${project.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.13version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
dependencies>
project>
application.yml
server:
port: 8001
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
type-aliases-package: cn.liyang.springCloud.entities # 所有Entity别名类所在包
mapper-locations: classpath:mybatis/mapper/*.xml # mapper映射文件
spring:
application:
name: microservice-product #这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: com.mysql.cj.jdbc.Driver # mysql驱动包
url: jdbc:mysql://127.0.0.1:3306/springdata?serverTimezone=GMT%2B8 # 数据库名称
username: root
password: root
dbcp2:
min-idle: 5 # 数据库连接池的最小维持连接数
initial-size: 5 # 初始化连接数
max-total: 5 # 最大连接数
max-wait-millis: 150 # 等待连接获取的最大超时时间
eureka:
instance:
instance-id: microservice-provider8001 #微服务显示的名字
prefer-ip-address: true #鼠标放在微服务名字上左下角显示Ip地址 http://192.168.200.1:8001/info
client:
#通过设置fetch-registry与register-with-eureka 表明自己是一个eureka服务
fetch-registry: true
register-with-eureka: true
# register-with-eureka: false
#注册register-salve1,register-salve2做注册中心集群
service-url:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
info:
app.name: microservicecloud-provider
company.name: liyang
build.artifactId: $project.artifactId$
build.version: $project.version$
productMapper.java
@Repository
@Mapper
public interface ProductMapper {
Product findById(Long pid);
List<Product> findAll();
boolean addProduct(Product product);
}
productMapper.xml
<mapper namespace="cn.liyang.springCloud.mapper.ProductMapper">
<select id="findById" resultType="Product" parameterType="Long">
SELECT pid,product_name,db_source FROM product where pid=#{pid};
select>
<select id="findAll" resultType="Product">
SELECT pid,product_name,db_source FROM product;
select>
<insert id="addProduct" parameterType="Product">
INSERT INTO product(product_name,db_source) VALUES(#{productName},#{dbSource})
insert>
mapper>
ProductServiceImpl.java
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductMapper productMapper;
@Override
public Product findById (Long pid) {
return productMapper.findById( pid );
}
@Override
public List<Product> findAll () {
return productMapper.findAll();
}
@Override
public boolean addProduct (Product product) {
return productMapper.addProduct( product );
}
}
ProductController.java
@RestController
public class ProductController {
@Autowired
private ProductService productService;
@RequestMapping(value = "/product/add",method = RequestMethod.POST)
public boolean add(@RequestBody Product product){
return productService.addProduct(product);
}
@RequestMapping(value = "/product/get/{id}",method = RequestMethod.GET)
public Product get(@PathVariable("id")Long id){
return productService.findById(id);
}
@RequestMapping(value = "/product/get/list",method = RequestMethod.GET)
public List<Product> getAll(){
return productService.findAll();
}
}
启动类productService8001.java
@EnableEurekaClient
@SpringBootApplication
public class productService8001 {
public static void main (String[] args) {
SpringApplication.run( productService8001.class, args );
}
}
server:
port: 8002
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
type-aliases-package: cn.liyang.springCloud.entities # 所有Entity别名类所在包
mapper-locations: classpath:mybatis/mapper/*.xml # mapper映射文件
spring:
application:
name: microservice-product #这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: com.mysql.cj.jdbc.Driver # mysql驱动包
url: jdbc:mysql://127.0.0.1:3306/springdata01?serverTimezone=GMT%2B8 # 数据库名称
username: root
password: root
dbcp2:
min-idle: 5 # 数据库连接池的最小维持连接数
initial-size: 5 # 初始化连接数
max-total: 5 # 最大连接数
max-wait-millis: 150 # 等待连接获取的最大超时时间
eureka:
instance:
instance-id: microservice-provider8002 #微服务显示的名字
prefer-ip-address: true #鼠标放在微服务名字上左下角显示Ip地址 http://192.168.200.1:8001/info
client:
#通过设置fetch-registry与register-with-eureka 表明自己是一个eureka服务
fetch-registry: true
register-with-eureka: true
# register-with-eureka: false
#注册register-salve1,register-salve2做注册中心集群
service-url:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
info:
app.name: microservicecloud-provider8002
company.name: liyang
build.artifactId: $project.artifactId$
build.version: $project.version$
其他目录和文件与provider01一样
pom.xml
<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>spring-cloud-01artifactId>
<groupId>cn.liyanggroupId>
<version>1.0-SNAPSHOTversion>
<relativePath>../springcloud01/pom.xmlrelativePath>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>spring-cloud04-consumerartifactId>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>cn.liyanggroupId>
<artifactId>spring-cloud-02-apiartifactId>
<version>${project.version}version>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
dependencies>
project>
application.yaml
server:
port: 80
spring:
application:
name: microservice-consumer
eureka:
instance:
instance-id: micorService-consumer80
client:
#通过设置fetch-registry与register-with-eureka 表明自己是一个eureka服务
fetch-registry: true
# register-with-eureka: false
#注册register-salve1,register-salve2做注册中心集群
service-url:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
register-with-eureka: true
配置类ConfigBean.java
@Configuration
public class ConfigBean {
@LoadBalanced//开启负载均衡
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
controller/consumerController.java
@RestController
public class consumerController {
private static final String REST_URL_PREFIX = "http://MICROSERVICE-PRODUCT";
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/consumer/product/get/list")
public List<Product> getAll(){
List<Product> list = restTemplate.getForObject( REST_URL_PREFIX + "/product/get/list", List.class );
System.out.println(list);
return list;
}
}
程序启动类Consumer80
@EnableEurekaClient
@SpringBootApplication
public class Consumer80 {
public static void main (String[] args) {
SpringApplication.run( Consumer80.class, args );
}
}
pom.xml
<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>spring-cloud-01artifactId>
<groupId>cn.liyanggroupId>
<version>1.0-SNAPSHOTversion>
<relativePath>../springcloud01/pom.xmlrelativePath>
parent>
<modelVersion>4.0.0modelVersion>
<groupId>cn.liyanggroupId>
<artifactId>springEurekaServerartifactId>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
project>
application.yml
server:
port: 8761
eureka:
instance:
#单机hostname: localhost
hostname: eurekaServer01
client:
service-url:
#单机设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#Eureka高复用时设置其他的Eureka之间通信
#defaultZone: http://eureka7003.com:7003/eureka/,http://eureka7004.com:7004/eureka/
defaultZone: http://eurekaServer02:8762/eureka/
spring:
application:
name: springEureka01
启动类EurekaServer.java
@EnableEurekaServer
@SpringBootApplication
public class EurekaServer {
public static void main (String[] args) {
SpringApplication.run( EurekaServer.class,args );
}
}
除了yaml文件其他文件同EurekaServer01\
server:
port: 8762
eureka:
instance:
#单机hostname: localhost
hostname: eureka02
client:
service-url:
#单机设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#Eureka高复用时设置其他的Eureka之间通信
#defaultZone: http://eureka7003.com:7003/eureka/,http://eureka7004.com:7004/eureka/
defaultZone: http://eurekaServer01:8761/eureka/
spring:
application:
name: springEureka02