springcloud项目搭建如何实现?示例

2020-05-02 08:00:56 java常见问答 9554

现在在java软件开发行业,springcloud可谓是很流行了,那么你了解springcloud项目吗?知道springcloud项目如何去搭建吗?有兴趣的朋友可以跟小编一起来看看。

创建EurekServer

创建一个springboot项目 并引入依赖web eurekserver hystrix-dashboard,'security'

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

修改配置文件 更改端口号 以及配置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文件:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>

对yml文件进行配置 修改端口号 增加application name:

server:
  port: 8080
spring:
  application:
    name: proxy
eureka:
  client:
    serviceUrl:
      defaultZone:  http://zhou:12345678@localhost:8761/eureka

然后写一个user的项目来进行测试 使用的是h2和jpa,建立domain类:

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;
    }
}

service:

@Service
public class CustomerService
{
    @Resource
    private CustomerRepositry customerRepositry;
    //根据id 查询用户
    public Optional < Customer > getCustomerById(Long id)
    {
        return customerRepositry.findById(id);
    }
    //查询用户信息
    public List < Customer > 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(1 L);
        customer.setUserName("KrisWu");
        customer.setPassword("123456");
        customerService.save(customer);
    }
    @RequestMapping("getCustomer")
    public List < Customer > getCustomer()
    {
        return customerService.getAllCustomer();
    }
    @RequestMapping("getOrder")
    @HystrixCommand
    public Map getOrder()
    {
        Optional < Customer > customer = customerService.getCustomerById(1 L);
        String orderDetail = orderClient.getMyOrder(1 L);
        Map map = new HashMap();
        map.put("customer", customer);
        map.put("orderDetail", orderDetail);
        return map;
    }

然后通过proxy的地址来访问user的项目

访问地址(http://localhost:8080/user/user/test/getCustomer)

然后发现可以显示增加的用户信息:

[{“userId”:1,”password”:”123456”,”userName”:”Shawnee”}]

好了,以上就是本篇文章的所有内容了,还想了解更多java架构师相关的信息,记得来关注本站消息哦。