接着上一篇??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
刷新页面
?好了,服务提供者到这里已经全部完成了,接下来快速实现消费者
|