因为要研究分布式事务 所以搭建一个简单的SpringCloud的项目 来进行事务相关的测试 中间出现了一些小问题 在这里记录一下创建项目的整个过程
- 创建EurekServer
创建一个springboot项目 并引入依赖web
eurekserver
hystrix-dashboard
,'security'
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.cloud
spring-cloud-starter-netflix-hystrix-dashboard
org.springframework.boot
spring-boot-starter-security
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-security
修改配置文件 更改端口号 以及配置security
server:
port: 8761
spring:
application:
name: registry
security:
user:
name: zhou
password: 12345678
eureka:
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://zhou:12345678@localhost:${server.port}/eureka/
然后修改启动类
@SpringBootApplication
@EnableEurekaServer
@EnableHystrixDashboard
public class RegistryApplication {
public static void main(String[] args) {
SpringApplication.run(RegistryApplication.class, args);
}
}
然后建立一个zuul的网关 引入pom文件
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-zuul
对yml文件进行配置 修改端口号 增加application name
server:
port: 8888
spring:
application:
name: proxy
eureka:
client:
serviceUrl:
defaultZone: http://zhou:12345678@localhost:8761/eureka
然后写一个user的项目来进行测试 使用的是h2
和jpa
建立domain
类
@Entity(name = "customer")
public class Customer {
@Id
@GeneratedValue
private Long userId;
private String password;
private String userName;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
建立CustomerRepositry
因为就是一个测试 所以只是继承了JpaRepository
public interface CustomerRepositry extends JpaRepository {
}
service
@Service
public class CustomerService {
@Resource
private CustomerRepositry customerRepositry;
//根据id 查询用户
public Optional getCustomerById(Long id){
return customerRepositry.findById(id);
}
//查询用户信息
public List getAllCustomer(){
return customerRepositry.findAll();
}
//保存用户信息
public void save(Customer customer){
customerRepositry.save(customer);
}
}
controller
@RestController
@RequestMapping("/user/test")
public class CustomerController {
@Resource
private CustomerService customerService;
@Resource
private OrderClient orderClient;
@PostConstruct
public void init(){
Customer customer = new Customer();
customer.setUserId(1L);
customer.setUserName("KrisWu");
customer.setPassword("123456");
customerService.save(customer);
}
@RequestMapping("getCustomer")
public List getCustomer(){
return customerService.getAllCustomer();
}
@RequestMapping("getOrder")
@HystrixCommand
public Map getOrder(){
Optional customer = customerService.getCustomerById(1L);
String orderDetail = orderClient.getMyOrder(1L);
Map map = new HashMap();
map.put("customer",customer);
map.put("orderDetail",orderDetail);
return map;
}
启动项目 出现了报错
ERROR 11612 --- [tbeatExecutor-0] com.netflix.discovery.DiscoveryClient : *****:***** - was unable to send heartbeat!
这个错误是在2.0以上会出现的。是因为security默认启用了csrf检验,要在eurekaServer端配置security的csrf检验为false。
所以在repostory
中新建一个config文件
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
super.configure(http);
}
}
然后通过proxy的地址来访问user的项目
访问地址(http://localhost:8888/user/user/test/getCustomer)
然后发现可以显示增加的用户信息
然后再新建一个order的项目 想要达到的想过是可以在user的项目中 直接调用order中的方法 这里和user基本上是差不多的 就不贴代码了
重要的地方是在user的项目中 新建一个类OrderClient
@FeignClient(value = "order",path = "/order/test")
public interface OrderClient {
@GetMapping("/{id}")
String getMyOrder(@PathVariable(name = "id")Long id);
}
主要就是通过这个类进行连接
然后启动项目 进行测试