理论的话不多说,直接进入主题(项目结构安排):
作用: 作为父级module,子级module继承父级中的依赖
pom.xml:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.2.RELEASE
com.shunyi
springcloud-config
0.0.1-SNAPSHOT
springcloud-config
Demo project for Spring Boot
1.8
Hoxton.SR1
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.cloud
spring-cloud-starter-openfeign
2.1.2.RELEASE
org.springframework.cloud
spring-cloud-starter-netflix-zuul
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
作用: springcloud的服务注册中心,服务的注册和发现
启动类加注解:@EnableEurekaServer
pom.xml:(下面的moudle的pom.xml和此一样)
4.0.0
com.shunyi
springcloud-config
0.0.1-SNAPSHOT
com.shunyi
springcloud-eureka
0.0.1-SNAPSHOT
springcloud-eureka
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-maven-plugin
application.yml:
server:
port: 1000
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
spring:
application:
name: springcloud-eureka-service
启动类加注解:@EnableEurekaClient
作用:两个登录的微服务,服务提供
springcloud-login的yml:
server:
port: 2000
eureka:
client:
service-url:
defaultZone: http://localhost:1000/eureka/
spring:
application:
name: springcloud-login-service
---------------------------------------------
springcloud-login01的yml:
server:
port: 2001
eureka:
client:
service-url:
defaultZone: http://localhost:1000/eureka/
spring:
application:
name: springcloud-login-service
创建两个class:
Login01Controller:
package com.shunyi.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("user")
public class Login01Controller {
@RequestMapping("/login")
public String login(){
return "我TM是个Login01Controller登录";
}
}
--------------------------------
Login02Controller:
package com.shunyi.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("user")
public class Login02Controller {
@RequestMapping("/login")
public String login(){
return "我TM是个Login02Controller登录";
}
}
此时可以启动服务:先启动springcloud-eureka 再启动 springcloud-login或者springcloud-login01
作用:主要是用于微服务之间的访问,fegin封装了Robbin,自动默认负载均衡轮询方式
启动类注解: @EnableEurekaClient @EnableFeignClients
application.xml:
server:
port: 3000
eureka:
client:
service-url:
defaultZone: http://localhost:1000/eureka/
spring:
application:
name: springcloud-fegin-service
Service接口类:
package com.shunyi.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(value = "springcloud-login-service")
public interface FeginService {
@GetMapping("/user/login")
public String login();
}
controller类:
package com.shunyi.controller;
import com.shunyi.service.FeginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FeginController {
@Autowired
private FeginService feginService;
@RequestMapping("/fegin/login")
public String login(){
return feginService.login();
}
}
此时可以启动服务:先启动springcloud-eureka 再启动 springcloud-login和springcloud-login01 再启动spring-fegin
作用:用户客户端通过浏览器访问,通过zuul网关来转发到对应的服务,在对应的服务进行负载
启动类注解:@EnableEurekaClient @EnableZuulProxy
application.xml:
server:
port: 4000
eureka:
client:
service-url:
defaultZone: http://localhost:1000/eureka/
spring:
application:
name: springcloud-zuul
zuul:
routes:
login-service: #自定义名称
path: /abcd/** #自定义路径
serviceId: springcloud-login-service #对应服务的 spring.application.name
此时可以启动服务:先启动springcloud-eureka 再启动 springcloud-login和springcloud-login01 再启动spring-fegin 在启动springcloud-zuul