1、微服务架构基于Spring boot项目
2、构建工具Idea
3、步骤:
a)服务中心启动
b)各个应用注册,可以使用基于http或rpc等形式调用
一、创建基于maven项目的spring boot项目
a)创建springboot项目
b)删除目录下的src等一系列文件,只留.idea,XXX.iml,pom.xml文件
二、创建Eureka服务中心
在项目内创建modules
成功后在启动方法中添加注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class RegistServerApplication {
public static void main(String[] args) {
SpringApplication.run(RegistServerApplication.class, args);
}
}
在application.yml加入
#测试环境
server:
port: 8989
eureka:
instance:
hostname: localhost
lease-expiration-duration-in-seconds: 30
lease-renewal-interval-in-seconds: 10
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
#关闭安全检查
healthcheck:
enabled: true
server:
waitTimeInMsWhenSyncEmpty: 0
#取消自我保护
enable-self-preservation: false
eviction-interval-timer-in-ms: 4000
启动项目
访问 http://localhost:8989 即可看到
三、利用先前方法构建ribbon 或者 openFeign
修改父目录 pom.xml文件
<modules>
<module>regist-server</module>
<module>ribbon</module>
<module>ribbon-consumer</module>
<module>feign</module>
</modules>
各个子目录下pom.xml对应需要修改
<parent>
<groupId></groupId>
<artifactId>cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
注:所有子module 需要指向注册中心的端口,子module 中port端口号不能重名
#测试环境
server:
port: 8990
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:8989/eureka/
ribbon-0项目
主方法需要加入
@SpringBootApplication
@EnableDiscoveryClient
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
@Bean
@Autowired
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
service层
@Service
public class HelloService {
public String sayHello() {
return "hello World";
}
}
controller层
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("/hello")
public String say(String name) {
return helloService.sayHello() + " " + name;
}
}
ribbon-1项目
@SpringBootApplication
@EnableDiscoveryClient
public class RibbonConsumerApplication {
@Bean
@Autowired
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
}
}
controller层
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("/hello")
public String say(String name) {
return helloService.sayHello() + " " + name;
}
}
service层
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String sayHello() {
return restTemplate.getForObject("http://ribbon-provider/hello?name=zhang",String.class);
}
}
ribbon-provider 是 ribbon-0中的 spring.application.name 在 ribbon-0中的application.yml中有写
基于rpc的OpenFeign
feign项目
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
接口
@FeignClient(value="ribbon-consumer")
public interface GetHello {
@RequestMapping(value = "/hello?name=1",method = RequestMethod.GET)
public String sayHello();
}
service层
@Service
public class HelloService {
@Autowired
private GetHello getHello;
public String sayHello() {
return getHello.sayHello();
}
}
controller层
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("/hello")
public String say(String name) {
return helloService.sayHello() + " " + name;
}
}
报错: