1. 程式人生 > 其它 >搭建springcloud專案

搭建springcloud專案

【springcloud】簡單建立一個springcloud專案

簡單的springcloud搭建

註冊中心:eureka

介面服務:server

web服務:web

閘道器:zuul

配置中心:config

pom引數統一管理:common

1、建立一個空的maven專案為springcloud

2、修改pom.xml檔案

pom.xml檔案

3、建立註冊中心Eureka

3.1、配置註冊中心eureka的配置檔案application.yml

 1 server:
 2   port: 9900
 3 
 4 eureka:
 5   instance:
 6     hostname: localhost
 7   client:
 8     #     宣告是否將自己的資訊註冊到 Eureka 伺服器上
 9     registerWithEureka: false
10     #     是否到 Eureka 伺服器中抓取註冊資訊
11     fetchRegistry: false
12     serviceUrl:
13       defaultZone: http://@eureka.user.name@:@eureka.user.password@@${eureka.instance.hostname}:${server.port}/eureka/
14 
15 
16 spring:
17   application:
18     name: eurka-service
19   security:
20     user:
21       name: @eureka.user.name@
22       password: @eureka.user.password@
eureka的pom.xml檔案

3.2、新增@EnableEurekaServer註解

3.3、啟動註冊中心

注:如果啟動報讀取不了yml檔案,可能是編碼問題。把編碼設定成utf-8就可以了。

3.4、訪問

http://localhost:9900/

注:如果啟動後登陸不了,可以試試新增配置類(必須,不然其他服務發現不了註冊中心)

 1 package com.xiaostudy.eureka.config;
 2 
 3 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
 4 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
 5 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
 6 
 7 @EnableWebSecurity
 8 public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
 9 
10     @Override
11     protected void configure(HttpSecurity http) throws Exception {
12         //關閉csrf
13         http.csrf().disable();
14         super.configure(http);
15     }
16 }

4、建立一個server模組,模擬釋出介面

4.1、添加註解@EnableDiscoveryClient和@EnableFeignClients

server的yml檔案

 1 server:
 2   port: 9901
 3 
 4 eureka:
 5   instance:
 6     preferIpAddress: true
 7     instance-id: ${spring.cloud.client.ip-address}:${server.port}
 8   client:
 9     serviceUrl:
10       defaultZone: http://@eureka.user.name@:@eureka.user.password@@localhost:9900/eureka/
11 
12 spring:
13   application:
14     name: server-service
15   cloud:
16     loadbalancer:
17       retry:
18         enabled: false
server的pom.xml檔案

4.2、建立services及實現類、controller和介面

services介面

package com.xiaostudy.server.services;

public interface TestServices {

    public String get(String name);
}

services實現類

package com.xiaostudy.server.services.impl;

import com.xiaostudy.server.services.TestServices;
import org.springframework.stereotype.Service;

@Service
public class TestServicesImpl implements TestServices {

    @Override
    public String get(String name) {
        return "引數name:" + name;
    }
}

controller

package com.xiaostudy.server.controller;

import com.xiaostudy.server.services.TestServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private TestServices testServices;

    @RequestMapping("/get")
    public String get(String name) {
        return testServices.get(name);
    }
}

介面

package com.xiaostudy.server.apis;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(name = "server-service", contextId = "TestServicesApis")
public interface TestServicesApis {

    @RequestMapping("/test/get")
    public String get(String name);
}

4.3、啟動server

5、新增一個web模組,通過介面呼叫服務

web的pom.xml檔案

web的配置檔案application.yml

server:
  port: 9902

eureka:
  instance:
    preferIpAddress: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
  client:
    serviceUrl:
      defaultZone: http://@eureka.user.name@:@eureka.user.password@@localhost:9900/eureka/

spring:
  application:
    name: web-service
  cloud:
    loadbalancer:
      retry:
        enabled: false

web的controller

package com.xiaostudy.web.controller;

import com.xiaostudy.server.apis.TestServicesApis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class TestWebController {

    @Autowired
    private TestServicesApis testServicesApis;

    @RequestMapping("/getTest")
    public String getTest() {
        return testServicesApis.get("通過web呼叫的");
    }
}

啟動web

6、新增閘道器zuul

zuul的pom.xml檔案 zuul的application.yml檔案

添加註解@EnableDiscoveryClient和@EnableZuulProxy

啟動zuul

7、新增配置中心

config的application.yml

server:
  port: 9904

eureka:
  instance:
    preferIpAddress: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
  client:
    serviceUrl:
      defaultZone: http://@eureka.user.name@:@eureka.user.password@@localhost:9900/eureka/

spring:
  application:
    name: config-service
  cloud:
    loadbalancer:
      retry:
        enabled: false
  profiles:
    active: native

添加註解@EnableDiscoveryClient和@EnableConfigServer

或者

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
</dependency>
web的bootstrap.yml檔案

啟動順序,註冊中心(eureka)、配置中心(config)、server、web、zuul(後面三個沒有順序)

8、新增一個common模組,用於各個模組的父模組,統一管理pom.xml的引數配置

隨便找一個模組的來複制

common的pom.xml檔案