springCloud学习

nacos和eureka对比

eureka

1.依赖
eureka只支持springCloud,首先在父工程里设置springCloud的版本管理

			<dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

然后建立服务端和客户端
服务端依赖

 <!--<!‐‐eureka注册中心服务‐‐>-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

客户端依赖,中间的exclosions是因为,他在浏览器访问返回的是实体类,用这个能怼成json

		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>jackson‐dataformat‐xml</artifactId>
                    <groupId>com.fasterxml.jackson.dataformat</groupId>
                </exclusion>
            </exclusions>
        </dependency>

2.运行配置
服务端 记得注解不同

@SpringBootApplication
@EnableEurekaServer
public class Server {
    public static void main(String[] args) {
        SpringApplication.run(Server.class,args);
    }
}

消费者的运行配置,需要往容器里放一个restTemplate

@SpringBootApplication
@EnableDiscoveryClient
public class NacosCustomerApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosCustomerApplication.class);
    }

    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

controller

@RestController
public class NacosCustomerController {

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    DiscoveryClient discoveryClient;

    @GetMapping("getUser")
    public User getUser(){
        List<ServiceInstance> instances = discoveryClient.getInstances("nacos_provider");
        ServiceInstance serviceInstance = instances.get(0);
        int port = serviceInstance.getPort();
        String host = serviceInstance.getHost();
        String url = "http://"+host+":"+port+"/getUserInfo";
        User user = restTemplate.getForObject(url, User.class);
        return user;
    }
}

这样请求提供端就行了
3.配置文件
客户端

server:
  port: 10001
spring:
  application:
    name: eureka-provider
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
  instance:
    instance-id: ${spring.application.name}:${server.port}
    prefer-ip-address: true

服务端

server:
  port: 10086
##表示不给自己注册也不去拉别的服务,不加报错
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    ##默认端口是8671,不指定的话会一直报连不上8671的错误
    service-url:
      defaultZone: http://localhost:10086/eureka

Nacos

不需要整服务端,直接官网下载解压,然后双击运行,就可以在8848端口访问到服务端管理界面了,账号密码都是nacos
1.依赖
全局引入springCloud-alibaba的版本管理

 <!--2.2.1 最新版本-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

在客户端依赖

 		<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

2.运行配置
和eureka一样,但是不需要注解
3.配置文件

server:
  port: 7001
spring:
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
  application:
    name: nacos_customer

ribbon负载均衡

  • 面对启动多个相同的提供者,使用负载均衡可以减缓压力,提高效率
  • nacos已经集成了ribbon,默认轮训
  • 两种使用方式,一种代码,一种注解

与上面相比,把discoveryClient拿serviceInstance变成了loadBalanceClient拿serviceInstance。
1.代码方式

 @Autowired
    LoadBalancerClient loadBalancerClient;

    @GetMapping("getUser")
    public User getUser(){
        ServiceInstance serviceInstance = loadBalancerClient.choose("nacos_provider");
        int port = serviceInstance.getPort();
        String host = serviceInstance.getHost();
        String url = "http://"+host+":"+port+"/getUserInfo";
        User user = restTemplate.getForObject(url, User.class);
        return user;
    }

2.注解方式
直接在restTemplate对象往容器放的时候加一个 @LoadBalanced注解

  	@Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }

这个时候不需要获取serviceInstance,就只能写死路径了

   @GetMapping("getUser")
    public User getUser(){
        String url = "http://xxxx:xxxx/getUserInfo";
        User user = restTemplate.getForObject(url, User.class);
        return user;
    }

OpenFeign

写死路径就是ribbon的问题
1.抽出来一个工程,放接口
类上注解指定是注册中心里哪个组的接口,
组名字不能带_下划线
就当做这组controller层的接口,必须写上@getmapping这种匹配注解

@FeignClient("nacosProvider")//提供者组名,这个名字不能带_
public interface FeignApi {
    @GetMapping("getUserInfo")
    User getUserInfo();
}

这个工程依赖

		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

2.提供者引入这个工程,controller继承对应的openFeign接口

@RestController
public class NacosProviderController implements FeignApi {


    public User getUserInfo(){
        return new User().setAge(20).setName("xxx");
    }
}

3.消费者引入这个工程,改造
添加feign注解

@SpringBootApplication
@EnableFeignClients
public class NacosCustomerApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosCustomerApplication.class);
    }
}

autowrite对应的feign接口,直接调方法就行

@RestController
public class NacosCustomerController {

    @Autowired
    FeignApi feignApi;

    @GetMapping("getUser")
    public User getUser(){
        return feignApi.getUserInfo();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值