5.3.2 修改pom添加依赖
<dependencies>
<!--公共部门-->
<dependency>
<groupId>cn.bdqn</groupId>
<artifactId>springcloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--mybatis-->
<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>
<!--mysql连接-->
<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>
<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.3.3编写yml
server:
port: 8003
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:
type-aliases-package: cn.bdqn.domain
mapper-locations: classpath:mapper/*.xml
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-width-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,
instances:
prefer-ip-address: true #使用ip地址注册
5.3.4编写启动类
@SpringBootApplication
@EnableEurekaClient
public class Payment8003Application {
public static void main(String[] args) {
SpringApplication.run(Payment8003Application.class, args);
}
}
5.3.5编写paymentMapper接口
public interface PaymentServer {
//保存一个支付流水
public void save(Payment payment);
//根据id获取具体的支付信息
public Payment selectById(Integer id);
}
5.3.6编写映射文件
<?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="PaymentResultMap" 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.3.6编写Payment业务接口以及实现类型
public interface PaymentServer {
//保存一个支付流水
public void save(Payment payment);
//根据id获取具体的支付信息
public Payment selectById(Integer id);
}
@Service
public class PaymentServerImpl implements PaymentServer {
@Autowired
private PaymentMapper paymentMapper;
@Override
public void save(Payment payment) {
paymentMapper.insert(payment);
}
@Override
public Payment selectById(Integer id) {
return paymentMapper.selectById(id);
}
}
5.3.8编写paymentController控制器
@RestController
@EnableEurekaClient
public class PaymentController {
@Autowired
private PaymentServer paymentServer;
@GetMapping("/payment/get/{id}")
public ResponseResult selectById(@PathVariable(name="id") Integer id) {
Payment payment = paymentServer.selectById(id);
if (payment != null) {
return new ResponseResult(200, "成功", payment);
} else {
return new ResponseResult(200, "失败", 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(404,"插入失败",null);
}
}
}
5.4查看Eureka server的状态
eureka.com:7001的状态
eureka.com:7002
eureka.com:7003
5.5测试之前的一个修改
8001支付微服务
@RestController
public class PaymentController {
@Autowired
private PaymentService paymentService;
@Value("${server.port}")
private Integer serverPort;
@GetMapping("/payment/get/{id}")
public ResponseResult queryById(@PathVariable(name="id") Integer id){
Payment payment=paymentService.queryById(id);
if(payment!=null){
return new ResponseResult(200, "成功!--port:"+serverPort ,payment);
}else{
return new ResponseResult(404,"没有对应记录,查询ID:"+id, null);
}
}
@PostMapping("/payment/save")
public ResponseResult save(@RequestBody Payment payment){
try {
paymentService.save(payment);
return new ResponseResult(200,"成功",null);
}catch (Exception e){
e.printStackTrace();
return new ResponseResult(500,"失败",null);
}
}
8002支付微服务
@RestController
public class PaymentConroller {
@Autowired
private PaymentServerImpl paymentServer;
@Value("${server.port}")
Integer serverPort;
@GetMapping("/payment/get/{id}")
public ResponseResult queryById(@PathVariable(name="id") Integer id){
Payment payment = paymentServer.queryById(id);
if(payment!=null) {
return new ResponseResult(200,"成功!----port:"+serverPort,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);
}
}
}
8003支付服务
@RestController
@EnableEurekaClient
public class PaymentController {
@Autowired
private PaymentServer paymentServer;
@Value("${server.port}")
Integer serverPort;
@GetMapping("/payment/get/{id}")
public ResponseResult selectById(@PathVariable(name="id") Integer id) {
Payment payment = paymentServer.selectById(id);
if (payment != null) {
return new ResponseResult(200, "成功! ----port:"+serverPort, 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(404,"插入失败",null);
}
}
}
5.5.3 测试
5.5.4.1步骤一
修改订单微服务orderController
@RestController
public class OrderController {
// private static final String PAYMENT_URL="http://localhost:8001";
@Autowired
private RestTemplate restTemplate;
//服务名
private static final String PAYMENT_URL= "http://SPRINGCLOUD-PAYMENT-PROVIDER-SERVICE";
@Autowired
private DiscoveryClient discoveryClient;
//根据id查询
@GetMapping("/consumer/payment/get/{id}")
public ResponseResult queryById(@PathVariable("id") Integer id){
// ResponseResult rs =restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,ResponseResult.class);//第一种方法通过ip
//第二种方法通过服务名
// 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){
//第一种方法 ip
// 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);
// 第三种方法
ResponseResult rs = restTemplate.postForObject(PAYMENT_URL+"/payment/save", payment, ResponseResult.class);
return rs;
}
}
5.5.4.2步骤二
修改applicationcontextConfig类,再去注入RestTemplate的时候,加上一个@loadBalanced注解
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
payment8001 支付微服务的yml文件
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:
instance-id: ${spring.cloud.client.ip-address}:${server.port} #显示 主机名称
prefer-ip-address: true #使用ip地址注册
payment8002 支付微服务的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地址注册
instance-id: ${spring.cloud.client.ip-address}:${server.port}
payment8003 支付微服务的yml文件
server:
port: 8003
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:
type-aliases-package: cn.bdqn.domain
mapper-locations: classpath:mapper/*.xml
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-width-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地址注册
instance-id: ${spring.cloud.client.ip-address}:${server.port}
打开Payment8001支付微服务的yml文件
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:
instance-id: ${spring.cloud.client.ip-address}:${server.port} #显示 主机名称
prefer-ip-address: true #使用ip地址注册
#该实例给服务中心发送心跳的间隔时间,用于表明该服务实例可用
lease-renewal-interval-in-seconds: 2
#服务中心删除此服务实例的等待时间(秒为单位)时间间隔为最后一次服务中心接受到的心跳时间
lease-expiration-duration-in-seconds: 10
打开Payment8002支付微服务的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地址注册
instance-id: ${spring.cloud.client.ip-address}:${server.port}
#该实例给服务中心发送心跳的间隔时间,用于表明该服务实例可用
lease-renewal-interval-in-seconds: 2
#服务中心删除此服务实例的等待时间(秒为单位)时间间隔为最后一次服务中心接受到的心跳时间
lease-expiration-duration-in-seconds: 10
打开Payment8003支付微服务的yml文件
server:
port: 8003
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:
type-aliases-package: cn.bdqn.domain
mapper-locations: classpath:mapper/*.xml
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-width-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地址注册
instance-id: ${spring.cloud.client.ip-address}:${server.port}
#该实例给服务中心发送心跳的间隔时间,用于表明该服务实例可用
lease-renewal-interval-in-seconds: 2
#服务中心删除此服务实例的等待时间(秒为单位)时间间隔为最后一次服务中心接受到的心跳时间
lease-expiration-duration-in-seconds: 10
7.3.2.注册中心Eureka Server7001
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://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
server:
enable-self-preservation: true
eviction-interval-timer-in-ms: 3000
文章来源:https://www.toymoban.com/news/detail-605404.html
客户端payment8001
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:
instance-id: ${spring.cloud.client.ip-address}:${server.port} #显示 主机名称
prefer-ip-address: true #使用ip地址注册
#该实例给服务中心发送心跳的间隔时间,用于表明该服务实例可用
lease-renewal-interval-in-seconds: 2
#服务中心删除此服务实例的等待时间(秒为单位)时间间隔为最后一次服务中心接受到的心跳时间
lease-expiration-duration-in-seconds: 10
文章来源地址https://www.toymoban.com/news/detail-605404.html
到了这里,关于五,Eureka 第五章的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!