【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文件



