IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> Spring Cloud实战(六)-服务提供者快速实现 -> 正文阅读

[Java知识库]Spring Cloud实战(六)-服务提供者快速实现

接着上一篇??Spring Cloud实战(五)-声明式接口模块 现在开始快速实现服务提供者

一.用户服务

1.pom.xml

<?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>cloud-action</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>user-server</artifactId>
    
    <properties>
        <cloud-action.api.version>0.0.1-SNAPSHOT</cloud-action.api.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>api</artifactId>
            <version>${cloud-action.api.version}</version>
        </dependency>
    </dependencies>

</project>

2.application.yml?

spring:
  application:
    name: user-server
  profiles:
    active: single

---
#单机版
spring:
  config:
    activate:
      on-profile: single
    import: optional:configserver:http://localhost:3333
  rabbitmq:
    virtual-host: /mqbus
    host: localhost
    port: 5672
    username: mqbus
    password: mqbus
server:
  port: 4444
#监控配置
management:
  endpoints:
    web:
      exposure:
        #公开所有端点 对于生产,您应该仔细选择要公开的端点.
        include: "*"
  endpoint:
    health:
      show-details: always
  info:
    env:
      enabled: true
eureka:
  instance:
    lease-renewal-interval-in-seconds: 30 #服务续约(心跳检测)时间,默认30秒
    lease-expiration-duration-in-seconds: 90 #服务剔除时间,默认90秒
    health-check-url-path: /actuator/health
    metadata-map:
      startup: ${random.int}    #需要在重启后触发信息和端点更新
  client:
    service-url:
      defaultZone: http://localhost:1111/eureka/ #eureka-client设置eureka-server的地址

---
spring:
  config:
    activate:
      on-profile: peer1
    import: optional:configserver:http://localhost:3333
  rabbitmq:
    virtual-host: /mqbus
    host: localhost
    port: 5672
    username: mqbus
    password: mqbus
server:
  port: 4445
#监控配置
management:
  endpoints:
    web:
      exposure:
        include: "*" #公开所有端点 对于生产,您应该仔细选择要公开的端点.
  endpoint:
    health:
      show-details: always
  info:
    env:
      enabled: true
eureka:
  instance:
    lease-renewal-interval-in-seconds: 30 #服务续约(心跳检测)时间,默认30秒
    lease-expiration-duration-in-seconds: 90 #服务剔除时间,默认90秒
    health-check-url-path: /actuator/health
    metadata-map:
      startup: ${random.int}    #需要在重启后触发信息和端点更新
  client:
    service-url:
      defaultZone: http://peer1:1112/eureka/,http://peer2:1113/eureka/ #eureka-client设置eureka-server的地址

---
spring:
  config:
    activate:
      on-profile: peer2
    import: optional:configserver:http://localhost:3333
  rabbitmq:
    virtual-host: /mqbus
    host: localhost
    port: 5672
    username: mqbus
    password: mqbus
server:
  port: 4446
#监控配置
management:
  endpoints:
    web:
      exposure:
        include: "*" #公开所有端点 对于生产,您应该仔细选择要公开的端点.
  endpoint:
    health:
      show-details: always
  info:
    env:
      enabled: true
eureka:
  instance:
    lease-renewal-interval-in-seconds: 30 #服务续约(心跳检测)时间,默认30秒
    lease-expiration-duration-in-seconds: 90 #服务剔除时间,默认90秒
    health-check-url-path: /actuator/health
    metadata-map:
      startup: ${random.int}    #需要在重启后触发信息和端点更新
  client:
    service-url:
      defaultZone: http://peer1:1112/eureka/,http://peer2:1113/eureka/ #eureka-client设置eureka-server的地址

3. 启动类

package com.example.user;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author 86188
 */
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class UserApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class, args);
    }
}

4.业务类?

package com.example.user.controller;

import com.example.api.user.User;
import com.example.api.user.UserServiceApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * @author 86188
 */
@RestController
@RefreshScope
public class UserController implements UserServiceApi {

    @Override
    public User createUserByPhone(String phone) {
        User user = new User();
        user.setId(UUID.randomUUID().toString().replace("-",""));
        user.setCreated(LocalDateTime.now());
        user.setUsername("泰斯特");
        user.setPassword(Long.toHexString(System.currentTimeMillis()));
        user.setPhone(phone);
        user.setToken("");
        user.setTokenExpire(LocalDateTime.now().plusSeconds(30));
        return user;
    }

    @Value("${book.name}")
    private String bookName = "";
    @RequestMapping("/ping")
    public String ping() {
        return bookName;
    }
}

二.订单服务

1.pom.xml

<?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>cloud-action</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>order-server</artifactId>

    <properties>
        <cloud-action.api.version>0.0.1-SNAPSHOT</cloud-action.api.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>api</artifactId>
            <version>${cloud-action.api.version}</version>
        </dependency>
    </dependencies>

</project>

2.application.yml?

spring:
  application:
    name: order-server
  profiles:
    active: single

---
#单机版
spring:
  config:
    activate:
      on-profile: single
    import: optional:configserver:http://localhost:3333
  rabbitmq:
    virtual-host: /mqbus
    host: localhost
    port: 5672
    username: mqbus
    password: mqbus
server:
  port: 5555
#监控配置
management:
  endpoints:
    web:
      exposure:
        #公开所有端点 对于生产,您应该仔细选择要公开的端点.
        include: "*"
  endpoint:
    health:
      show-details: always
  info:
    env:
      enabled: true
eureka:
  instance:
    lease-renewal-interval-in-seconds: 30 #服务续约(心跳检测)时间,默认30秒
    lease-expiration-duration-in-seconds: 90 #服务剔除时间,默认90秒
    health-check-url-path: /actuator/health
    metadata-map:
      startup: ${random.int}    #需要在重启后触发信息和端点更新
  client:
    service-url:
      defaultZone: http://localhost:1111/eureka/ #eureka-client设置eureka-server的地址

---
spring:
  config:
    activate:
      on-profile: peer1
    import: optional:configserver:http://localhost:3333
  rabbitmq:
    virtual-host: /mqbus
    host: localhost
    port: 5672
    username: mqbus
    password: mqbus
server:
  port: 5556
#监控配置
management:
  endpoints:
    web:
      exposure:
        include: "*" #公开所有端点 对于生产,您应该仔细选择要公开的端点.
  endpoint:
    health:
      show-details: always
  info:
    env:
      enabled: true
eureka:
  instance:
    lease-renewal-interval-in-seconds: 30 #服务续约(心跳检测)时间,默认30秒
    lease-expiration-duration-in-seconds: 90 #服务剔除时间,默认90秒
    health-check-url-path: /actuator/health
    metadata-map:
      startup: ${random.int}    #需要在重启后触发信息和端点更新
  client:
    service-url:
      defaultZone: http://peer1:1112/eureka/,http://peer2:1113/eureka/ #eureka-client设置eureka-server的地址

---
spring:
  config:
    activate:
      on-profile: peer2
    import: optional:configserver:http://localhost:3333
  rabbitmq:
    virtual-host: /mqbus
    host: localhost
    port: 5672
    username: mqbus
    password: mqbus
server:
  port: 5557
#监控配置
management:
  endpoints:
    web:
      exposure:
        include: "*" #公开所有端点 对于生产,您应该仔细选择要公开的端点.
  endpoint:
    health:
      show-details: always
  info:
    env:
      enabled: true
eureka:
  instance:
    lease-renewal-interval-in-seconds: 30 #服务续约(心跳检测)时间,默认30秒
    lease-expiration-duration-in-seconds: 90 #服务剔除时间,默认90秒
    health-check-url-path: /actuator/health
    metadata-map:
      startup: ${random.int}    #需要在重启后触发信息和端点更新
  client:
    service-url:
      defaultZone: http://peer1:1112/eureka/,http://peer2:1113/eureka/ #eureka-client设置eureka-server的地址

3. 启动类

package com.example.order;

        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
        import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
        import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author 86188
 */
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class OrderApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }
}

4.业务类

package com.example.order.controller;

import com.example.api.order.OrderServiceApi;
import com.example.api.order.ProductOrder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author 86188
 */
@RestController
public class OrderController implements OrderServiceApi {

    @Override
    public ProductOrder createOrder(ProductOrder order) {
        if (Objects.isNull(order)) {
            throw new RuntimeException("业务异常");
        }
        return order;
    }
}

三.库存服务

1.pom.xml

<?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>cloud-action</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>repository-server</artifactId>

    <properties>
        <cloud-action.api.version>0.0.1-SNAPSHOT</cloud-action.api.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>api</artifactId>
            <version>${cloud-action.api.version}</version>
        </dependency>
    </dependencies>
</project>

2.application.yml?

spring:
  application:
    name: repository-server
  profiles:
    active: single

---
#单机版
spring:
  config:
    activate:
      on-profile: single
    import: optional:configserver:http://localhost:3333
  rabbitmq:
    virtual-host: /mqbus
    host: localhost
    port: 5672
    username: mqbus
    password: mqbus
server:
  port: 6666
#监控配置
management:
  endpoints:
    web:
      exposure:
        #公开所有端点 对于生产,您应该仔细选择要公开的端点.
        include: "*"
  endpoint:
    health:
      show-details: always
  info:
    env:
      enabled: true
eureka:
  instance:
    lease-renewal-interval-in-seconds: 30 #服务续约(心跳检测)时间,默认30秒
    lease-expiration-duration-in-seconds: 90 #服务剔除时间,默认90秒
    health-check-url-path: /actuator/health
    metadata-map:
      startup: ${random.int}    #需要在重启后触发信息和端点更新
  client:
    service-url:
      defaultZone: http://localhost:1111/eureka/ #eureka-client设置eureka-server的地址

---
spring:
  config:
    activate:
      on-profile: peer1
    import: optional:configserver:http://localhost:3333
  rabbitmq:
    virtual-host: /mqbus
    host: localhost
    port: 5672
    username: mqbus
    password: mqbus
server:
  port: 6667
#监控配置
management:
  endpoints:
    web:
      exposure:
        include: "*" #公开所有端点 对于生产,您应该仔细选择要公开的端点.
  endpoint:
    health:
      show-details: always
  info:
    env:
      enabled: true
eureka:
  instance:
    lease-renewal-interval-in-seconds: 30 #服务续约(心跳检测)时间,默认30秒
    lease-expiration-duration-in-seconds: 90 #服务剔除时间,默认90秒
    health-check-url-path: /actuator/health
    metadata-map:
      startup: ${random.int}    #需要在重启后触发信息和端点更新
  client:
    service-url:
      defaultZone: http://peer1:1112/eureka/,http://peer2:1113/eureka/ #eureka-client设置eureka-server的地址

---
spring:
  config:
    activate:
      on-profile: peer2
    import: optional:configserver:http://localhost:3333
  rabbitmq:
    virtual-host: /mqbus
    host: localhost
    port: 5672
    username: mqbus
    password: mqbus
server:
  port: 6668
#监控配置
management:
  endpoints:
    web:
      exposure:
        include: "*" #公开所有端点 对于生产,您应该仔细选择要公开的端点.
  endpoint:
    health:
      show-details: always
  info:
    env:
      enabled: true
eureka:
  instance:
    lease-renewal-interval-in-seconds: 30 #服务续约(心跳检测)时间,默认30秒
    lease-expiration-duration-in-seconds: 90 #服务剔除时间,默认90秒
    health-check-url-path: /actuator/health
    metadata-map:
      startup: ${random.int}    #需要在重启后触发信息和端点更新
  client:
    service-url:
      defaultZone: http://peer1:1112/eureka/,http://peer2:1113/eureka/ #eureka-client设置eureka-server的地址

3. 启动类

package com.example.repository;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author 86188
 */
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class RepositoryApplication {
    public static void main(String[] args) {
        SpringApplication.run(RepositoryApplication.class, args);
    }
}

4.业务类

package com.example.repository.controller;

import com.example.api.repository.RepositoryServiceApi;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 86188
 */
@RestController
public class RepositoryController implements RepositoryServiceApi {

    @Override
    public Boolean reduceProduct() {
        return true;
    }
}

四.支付服务

1.pom.xml

<?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>cloud-action</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>pay-server</artifactId>

    <properties>
        <cloud-action.api.version>0.0.1-SNAPSHOT</cloud-action.api.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>api</artifactId>
            <version>${cloud-action.api.version}</version>
        </dependency>
    </dependencies>
</project>

2.application.yml?

spring:
  application:
    name: pay-server
  profiles:
    active: single

---
#单机版
spring:
  config:
    activate:
      on-profile: single
    import: optional:configserver:http://localhost:3333
  rabbitmq:
    virtual-host: /mqbus
    host: localhost
    port: 5672
    username: mqbus
    password: mqbus
server:
  port: 7777
#监控配置
management:
  endpoints:
    web:
      exposure:
        #公开所有端点 对于生产,您应该仔细选择要公开的端点.
        include: "*"
  endpoint:
    health:
      show-details: always
  info:
    env:
      enabled: true
eureka:
  instance:
    lease-renewal-interval-in-seconds: 30 #服务续约(心跳检测)时间,默认30秒
    lease-expiration-duration-in-seconds: 90 #服务剔除时间,默认90秒
    health-check-url-path: /actuator/health
    metadata-map:
      startup: ${random.int}    #需要在重启后触发信息和端点更新
  client:
    service-url:
      defaultZone: http://localhost:1111/eureka/ #eureka-client设置eureka-server的地址

---
spring:
  config:
    activate:
      on-profile: peer1
    import: optional:configserver:http://localhost:3333
  rabbitmq:
    virtual-host: /mqbus
    host: localhost
    port: 5672
    username: mqbus
    password: mqbus
server:
  port: 7778
#监控配置
management:
  endpoints:
    web:
      exposure:
        include: "*" #公开所有端点 对于生产,您应该仔细选择要公开的端点.
  endpoint:
    health:
      show-details: always
  info:
    env:
      enabled: true
eureka:
  instance:
    lease-renewal-interval-in-seconds: 30 #服务续约(心跳检测)时间,默认30秒
    lease-expiration-duration-in-seconds: 90 #服务剔除时间,默认90秒
    health-check-url-path: /actuator/health
    metadata-map:
      startup: ${random.int}    #需要在重启后触发信息和端点更新
  client:
    service-url:
      defaultZone: http://peer1:1112/eureka/,http://peer2:1113/eureka/ #eureka-client设置eureka-server的地址

---
spring:
  config:
    activate:
      on-profile: peer2
    import: optional:configserver:http://localhost:3333
  rabbitmq:
    virtual-host: /mqbus
    host: localhost
    port: 5672
    username: mqbus
    password: mqbus
server:
  port: 7779
#监控配置
management:
  endpoints:
    web:
      exposure:
        include: "*" #公开所有端点 对于生产,您应该仔细选择要公开的端点.
  endpoint:
    health:
      show-details: always
  info:
    env:
      enabled: true
eureka:
  instance:
    lease-renewal-interval-in-seconds: 30 #服务续约(心跳检测)时间,默认30秒
    lease-expiration-duration-in-seconds: 90 #服务剔除时间,默认90秒
    health-check-url-path: /actuator/health
    metadata-map:
      startup: ${random.int}    #需要在重启后触发信息和端点更新
  client:
    service-url:
      defaultZone: http://peer1:1112/eureka/,http://peer2:1113/eureka/ #eureka-client设置eureka-server的地址

3. 启动类

package com.example.pay;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author 86188
 */
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class PayApplication {
    public static void main(String[] args) {
        SpringApplication.run(PayApplication.class, args);
    }
}

4.业务类

package com.example.pay.controller;

import com.example.api.pay.PayOrder;
import com.example.api.pay.PayServiceApi;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * @author 86188
 */
@RestController
public class PayController implements PayServiceApi {

    @Override
    public PayOrder createPayOrder() {
        PayOrder payOrder = new PayOrder();
        payOrder.setId(Long.toHexString(System.currentTimeMillis()));
        payOrder.setPayAmount(new BigDecimal(100.00));
        payOrder.setResult("ok");
        return payOrder;
    }
}

五.依次启动服务

java -jar??register-server-0.0.1-SNAPSHOT.jar

java -jar?monitor-server-0.0.1-SNAPSHOT.jar

java -jar?config-server-0.0.1-SNAPSHOT.jar

java -jar??user-server-0.0.1-SNAPSHOT.jar

java -jar order-server-0.0.1-SNAPSHOT.jar

java -jar?repository-server-0.0.1-SNAPSHOT.jar

java -jar??pay-server-0.0.1-SNAPSHOT.jar

查看效果

?顺便测试一下配置中心是否可以自动刷新配置,由于本地环境git无法触发内网地址,这里每次修改配置文件之后,手动调一下http://localhost:3333/actuator/busrefresh

?

?

?

?修改git配置文件

?手动调用 http://localhost:3333/actuator/busrefresh

刷新页面

?好了,服务提供者到这里已经全部完成了,接下来快速实现消费者

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2022-04-06 16:04:04  更:2022-04-06 16:06:33 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 7:19:12-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码