【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、訪問


注:如果啟動后登陸不了,可以試試添加配置類(必須,不然其他服務發現不了注冊中心)
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文件



