2.1.3 增加依赖
<!--添加依赖-->
<dependencies>
<!--Eureka Server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--增加公共方法-->
<dependency>
<groupId>cn.bdqn</groupId>
<artifactId>springcloud-api-commons</artifactId>
<version>${project.version}</version>
</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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.1.4 yml
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务器端的
client:
#false 表示不向注册中心注册自己
register-with-eureka: false
#false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-register: false
server-url:
#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
2.1.5编写主启动类
@EnableEurekaServer
@SpringBootApplication
public class EurakeServer7001Application {
public static void main(String[] args) {
SpringApplication.run(EurakeSever7001Application.class,args);
}
}
2.2.2修改pom添加依赖
<!--添加Eureka client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.2.3修改yml:添加Eureka的配置
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true 单节点无所谓,集群必须设置true 才能配合ribbon 使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
instance:
prefer-ip-address: true #使用ip地址注册
2.2.4修改主启动类:标注Eureka客户端
@SpringBootApplication
@EnableEurekaClient
public class PaymentApplication {
public static void main(String[] args) {
SpringApplication.run(PaymentApplication.class,args);
}
}
2.3.2修改pom添加依赖
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.3.3 修改yml:添加Eureka的配置
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true 单节点无所谓,集群必须设置true 才能配合ribbon 使用负载均衡
fetch-registry: true
server-url:
defaultZone: http://localhost:7001/eureka
instance:
prefer-ip-address: true #使用ip地址注册
2.3.4修改启动类 标注Eureka客户端
@SpringBootApplication
@EnableEurekaClient
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
}
2.3.4修改主启动类 标注为Eureka客户端
2.3.6补充
// private static final String PAYMENT_URL="http://localhost:8001";
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
//根据id查询
@GetMapping("/consumer/payment/get/{id}")
public ResponseResult queryById(@PathVariable("id") Integer id){
List<ServiceInstance> serviceInstances = discoveryClient.getInstances("SPRINGCLOUD-PAYMENT-PROVIDER-SERVICE");
ServiceInstance instance = serviceInstances.get(0);
ResponseResult rs = restTemplate.getForObject("http://"+instance.getHost()+instance.getPort()+"/payment/get/"+id,ResponseResult.class);
// ResponseResult rs =restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,ResponseResult.class);
return rs;
}
//创建订单
@GetMapping("/consumer/payment/save")
public ResponseResult save(Payment payment){
// ResponseResult rs =restTemplate.postForObject(PAYMENT_URL+"/payment/save", payment,ResponseResult.class);
List<ServiceInstance> serviceInstances = discoveryClient.getInstances("SPRINGCLOUD-PAYMENT-PROVIDER-SERVICE");
ServiceInstance instance = serviceInstances.get(0);
ResponseResult rs = restTemplate.postForObject("http://"+instance.getHost()+instance.getPort()+"/payment/save",payment,ResponseResult.class);
return rs;
}
}
3.2.1修改pom添加依赖
<!--依赖-->
<dependencies>
<!--eureka server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--公共方法-->
<dependency>
<groupId>cn.bdqn</groupId>
<artifactId>springcloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--热部署工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3.2.2编写yml
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #eureka服务器端的
client:
#false 表示不向注册中心注册自己
register-with-eureka: false
#false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3.2.5 编写启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer7002Application {
public static void main(String[] args) {
SpringApplication.run(EurekaServer7002Application.class,args);
}
}
3.3.3修改pom添加依赖
<dependencies>
<!--eureka server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--公共类-->
<dependency>
<groupId>cn.bdqn</groupId>
<artifactId>springcloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3.3.4编写yml
server:
port: 7003
eureka:
instance:
hostname: eureka7003.com #eureka服务器端的
client:
#false 表示不向注册中心注册自己
register-with-eureka: false
#false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3.3.5编写启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer7003Application {
public static void main(String[] args) {
SpringApplication.run(EurekaServer7003Application.class,args);
}
}
3.4.2 修改后的配置文件
springcloud-eureka-sever-7001
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务器端的
client:
#false 表示不向注册中心注册自己
register-with-eureka: false
#false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-register: false
service-url:
#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
defaultZone: http://eureka7001.com:7002/eureka/,http://eureka7003.com:7002/eureka/
springcloud-eureka-sever-7001
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #eureka服务器端的
client:
#false 表示不向注册中心注册自己
register-with-eureka: false
#false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
springcloud-eureka-sever003
server:
port: 7003
eureka:
instance:
hostname: eureka7003.com #eureka服务器端的
client:
#false 表示不向注册中心注册自己
register-with-eureka: false
#false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7001.com:7001/eureka/
4.1支付微服务发布到 Eureka Sever中
server:
port: 8001
spring:
application:
name: springcloud-payment-provider-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springcloud_db?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT
username: root
password: xiaoduo456new
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: cn.bdqn.domain # 所有Entity别名类所在包
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true 单节点无所谓,集群必须设置true 才能配合ribbon 使用负载均衡
fetch-registry: true
service-url:
#defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka,
http://eureka7002.com:7002/eureka,
http://eureka7003.com:7003/eureka,
instance:
prefer-ip-address: true #使用ip地址注册
4.2订单微服务发布到Eureka Server中
server:
port: 8001
spring:
application:
name: springcloud-payment-provider-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springcloud_db?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT
username: root
password: xiaoduo456new
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: cn.bdqn.domain # 所有Entity别名类所在包
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true 单节点无所谓,集群必须设置true 才能配合ribbon 使用负载均衡
fetch-registry: true
service-url:
#defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka,
http://eureka7002.com:7002/eureka,
http://eureka7003.com:7003/eureka,
instance:
prefer-ip-address: true #使用ip地址注册
5.2.2修改pom 添加依赖
<!--依懒-->
<dependencies>
<!--添加Eureka client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--公共的部分-->
<dependency>
<groupId>cn.bdqn</groupId>
<artifactId>springcloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</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-actuator</artifactId>
</dependency>
<!--数据库-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--阿里云数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!--驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
5.2.3编辑yml
server:
port: 8002
spring:
application:
name: springcloud-payment-provider-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springcloud_db?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT
username: root
password: xiaoduo456new
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: cn.bdqn.domain
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true 单节点无所谓,集群必须设置true 才能配合ribbon 使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,
http://eureka7002.com:7002/eureka,
http://eureka7003.com:7003/eureka,
instance:
prefer-ip-address: true #使用ip地址注册
5.24编写启动类
@SpringBootApplication
@EnableEurekaClient
public class Payment8002Application {
public static void main(String[] args) {
SpringApplication.run(Payment8002Application.class,args);
}
}
5.25编写PaymentMapper接口
@Mapper
public interface PaymentMapper {
//保存一个支付流水
public void insert(Payment payment);
//根据id获取具体的支付信息
public Payment selectById(@Param("id") Integer id);
}
5.2.6编写PaymentMapper.xml映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "--//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.bdqn.Mapper.PaymentMapper">
<resultMap id="PaymentReusltMap" type="cn.bdqn.domain.Payment">
<id column="id" property="id"></id>
<id column="flow_number" property="flowNumber"></id>
</resultMap>
<insert id="insert" parameterType="cn.bdqn.Mapper.PaymentMapper">
insert into t_payment(flow_number) values(#{flowNumber})
</insert>
<select id="selectById" resultMap="PaymentResultMap">
select id,flow_number from t_payment where id=#{id};
</select>
</mapper>
5.
5.2.7编写payment业务接口以及实现类
public interface PaymentServer {
//保存一个支付流水
public void save(Payment payment);
//根据id获取具体的支付信息
public Payment queryById(Integer id);
}
@Service
public class PaymentServerImpl implements PaymentServer {
@Autowired
private PaymentMapper paymentMapper;
@Override
public Payment queryById(Integer id) {
return paymentMapper.selectById(id);
}
@Override
public void save(Payment payment) {
paymentMapper.insert(payment);
}
}
5.2.8 编写paymentController控制器
@RestController
public class PaymentConroller {
@Autowired
private PaymentServerImpl paymentServer;
@GetMapping("/payment/id/{id}")
public ResponseResult queryById(@PathVariable(name="id") Integer id){
Payment payment = paymentServer.queryById(id);
if(payment!=null) {
return new ResponseResult(200,"成功",payment);
}else{
return new ResponseResult(404,"没有对应的记录,查询id"+id,null);
}
}
@PostMapping("/payment/save")
public ResponseResult save(@RequestBody Payment payment){
try {
paymentServer.save(payment);
return new ResponseResult(200,"插入成功",null);
}catch (Exception e){
e.printStackTrace();
return new ResponseResult(200,"插入失败",null);
}
}
}