1.eureka 注册中心
eureka 分为服务端server和服务端client。所有其他微服务eureka-client都要在注册中心eureka-server注册,告诉注册中心自己在哪个位置哪个端口。这样微服务之间的互相调用通过询问注册中心得知其他服务位置就能去请求其他服务了。
eureka-server
新建一个springboot项目eureka-server 作为eureka服务端
maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
application.properties
server.port=8761
#是否将自身注册
eureka.client.register-with-eureka=false
#如果为true,启动时报警.
eureka.client.fetch-registry=false
启动类加个注解@EnableEurekaServer表示自己是注册中心
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
ok 启动一下 去访问http://localhost:8761 出现下面页面就成功了
eureka-client
新建一个springboot项目eureka-client作为eureka客户端 也就是微服务
maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
application.properties
#客户端在注册中心里的名字 服务之间相互调用一般根据这个name
spring.application.name=service-first
#自身端口号
server.port=8081
#指定注册中心的地址
eureka.client.service-url.default=http://localhost:8761/eurka/
这里启动类加个注解@EnableEurekaClient表示自己是erueka客户端
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
启动注册中心之后,再启动客户端eureka-client 然后去访问http://localhost:8761 ,你就会发现,服务已经注册到了eureka-server.
2.Fegin 服务调用
上面说了服务在注册中心注册是方便调用其他服务,那么服务之间是怎么来互相调用的呢?
服务之间调用时需要构建连接 发送复杂的请求,Fegin封装了这些http请求,让我们通过注解就能使用这些底层功能,这就是Fegin干的事了。
新建一个springboot项目 Fegin
maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
application.properties
server.port=9002
spring.application.name=service-fegin
eureka.client.service-url.default= http://localhost:8761/eurela/
启动类上再加上注解@EnableFeignClients
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
那么如何来调用其他服务呢?
首先在之前的eureka-client项目写个controller方便调用 该方法就是打印出自身服务的端口号
@RestController
public class TestController {
@RequestMapping("/test")
public String test(){
return "我是服务service-first;
}
}
然后在Fegin项目中写个接口ServiceFegin加上注解@FeignClient
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
@Component
@FeignClient(value="service-first")//这里指定要调用服务的注册名 就是spring.application.name
public interface ServiceFegin {
//下面就是接口方法,一定要和被调用服务里的controller一样
@RequestMapping("/test")
String test();
}
说白了 这个接口里的方法已经通过注解@FeignClient请求其他微服务已经实现了(接口里的方法其实就是被调用服务里的方法)
@FeignClient做的事相当于把这个接口里的方法以http请求的方式给实现了
所以直接调用接口就完事了
最后在Fegin写个controller类 验证一下
@RestController
public class TestController {
@Autowired
private ServiceFegin serviceFegin;//和平常写的service接口调用一样
@RequestMapping("/tes1")
public String tes1(){
return serviceFegin.test();
}
}
访问http://localhost:9002/tes1 就出现了 这个
说明Fegin项目(也可以说微服务)调用了eureka-clien项目的功能
这就是Fegin调用 ,其实就是把其他服务的controller当成一个service接口实现类
3.负载均衡ribbon和熔断器Hystrix
4.springcloud-config 和 zuul
3,4有空再写.喵